1
1
import fs from "node:fs" ;
2
- import { $ } from "execa" ;
2
+ import { $ , execa } from "execa" ;
3
3
import { assert } from "@std/assert" ;
4
+ import { additionalFiles } from "@trigger.dev/build/extensions/core" ;
4
5
import { BuildManifest } from "@trigger.dev/core/v3" ;
5
6
import { BuildContext , BuildExtension } from "@trigger.dev/core/v3/build" ;
6
7
import { logger } from "@trigger.dev/sdk/v3" ;
@@ -18,6 +19,13 @@ export type PythonOptions = {
18
19
* Example: `/usr/bin/python3` or `C:\\Python39\\python.exe`
19
20
*/
20
21
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 [ ] ;
21
29
} ;
22
30
23
31
const splitAndCleanComments = ( str : string ) =>
@@ -47,6 +55,10 @@ class PythonExtension implements BuildExtension {
47
55
}
48
56
49
57
async onBuildComplete ( context : BuildContext , manifest : BuildManifest ) {
58
+ await additionalFiles ( {
59
+ files : this . options . scripts ?? [ ] ,
60
+ } ) . onBuildComplete ! ( context , manifest ) ;
61
+
50
62
if ( context . target === "dev" ) {
51
63
if ( this . options . pythonBinaryPath ) {
52
64
process . env . PYTHON_BIN_PATH = this . options . pythonBinaryPath ;
@@ -110,10 +122,11 @@ export const run = async (scriptArgs: string[] = [], options: Parameters<typeof
110
122
options
111
123
) ;
112
124
113
- const result = await $ ( {
125
+ const result = await execa ( {
114
126
shell : true ,
127
+ verbose : ( verboseLine , verboseObject ) => logger . debug ( verboseLine , verboseObject ) ,
115
128
...options ,
116
- } ) ( pythonBin , ... scriptArgs ) ;
129
+ } ) ( pythonBin , scriptArgs ) ;
117
130
118
131
try {
119
132
assert ( ! result . failed , `Command failed: ${ result . stderr } ` ) ;
@@ -126,9 +139,21 @@ export const run = async (scriptArgs: string[] = [], options: Parameters<typeof
126
139
return result ;
127
140
} ;
128
141
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
+
129
153
export const runInline = ( scriptContent : string , options : Parameters < typeof $ > [ 1 ] = { } ) => {
130
154
assert ( scriptContent , "Script content is required" ) ;
155
+
131
156
return run ( [ "" ] , { input : scriptContent , ...options } ) ;
132
157
} ;
133
158
134
- export default { run, runInline } ;
159
+ export default { run, runScript , runInline } ;
0 commit comments