Skip to content

Commit 821fc53

Browse files
committed
copy script files on-demand
1 parent ea126a7 commit 821fc53

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

packages/build/src/extensions/python.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from "node:fs";
2-
import { $ } from "execa";
2+
import { $, execa } from "execa";
33
import { assert } from "@std/assert";
4+
import { additionalFiles } from "@trigger.dev/build/extensions/core";
45
import { BuildManifest } from "@trigger.dev/core/v3";
56
import { BuildContext, BuildExtension } from "@trigger.dev/core/v3/build";
67
import { logger } from "@trigger.dev/sdk/v3";
@@ -18,6 +19,13 @@ export type PythonOptions = {
1819
* Example: `/usr/bin/python3` or `C:\\Python39\\python.exe`
1920
*/
2021
pythonBinaryPath?: string;
22+
/**
23+
* An array of glob patterns that specify which Python scripts are allowed to be executed.
24+
*
25+
* @remarks
26+
* These scripts will be copied to the container during the build process.
27+
*/
28+
scripts?: string[];
2129
};
2230

2331
const splitAndCleanComments = (str: string) =>
@@ -47,6 +55,10 @@ class PythonExtension implements BuildExtension {
4755
}
4856

4957
async onBuildComplete(context: BuildContext, manifest: BuildManifest) {
58+
await additionalFiles({
59+
files: this.options.scripts ?? [],
60+
}).onBuildComplete!(context, manifest);
61+
5062
if (context.target === "dev") {
5163
if (this.options.pythonBinaryPath) {
5264
process.env.PYTHON_BIN_PATH = this.options.pythonBinaryPath;
@@ -110,10 +122,11 @@ export const run = async (scriptArgs: string[] = [], options: Parameters<typeof
110122
options
111123
);
112124

113-
const result = await $({
125+
const result = await execa({
114126
shell: true,
127+
verbose: (verboseLine, verboseObject) => logger.debug(verboseLine, verboseObject),
115128
...options,
116-
})(pythonBin, ...scriptArgs);
129+
})(pythonBin, scriptArgs);
117130

118131
try {
119132
assert(!result.failed, `Command failed: ${result.stderr}`);
@@ -126,9 +139,21 @@ export const run = async (scriptArgs: string[] = [], options: Parameters<typeof
126139
return result;
127140
};
128141

142+
export const runScript = (
143+
scriptPath: string,
144+
scriptArgs: string[] = [],
145+
options: Parameters<typeof $>[1] = {}
146+
) => {
147+
assert(scriptPath, "Script path is required");
148+
assert(fs.existsSync(scriptPath), `Script does not exist: ${scriptPath}`);
149+
150+
return run([scriptPath, ...scriptArgs], options);
151+
};
152+
129153
export const runInline = (scriptContent: string, options: Parameters<typeof $>[1] = {}) => {
130154
assert(scriptContent, "Script content is required");
155+
131156
return run([""], { input: scriptContent, ...options });
132157
};
133158

134-
export default { run, runInline };
159+
export default { run, runScript, runInline };

0 commit comments

Comments
 (0)