@@ -9,15 +9,26 @@ import fs from "node:fs";
9
9
import { Result , x , Options as XOptions } from "tinyexec" ;
10
10
import { createTempFileSync , withTempFile } from "./utils/tempFiles.js" ;
11
11
12
+ export type PythonExecOptions = Partial < XOptions > & {
13
+ env ?: { [ key : string ] : string | undefined } ;
14
+ } ;
15
+
12
16
export const python = {
13
- async run ( scriptArgs : string [ ] = [ ] , options : Partial < XOptions > = { } ) : Promise < Result > {
17
+ async run ( scriptArgs : string [ ] = [ ] , options : PythonExecOptions = { } ) : Promise < Result > {
14
18
const pythonBin = process . env . PYTHON_BIN_PATH || "python" ;
15
19
16
20
return await logger . trace (
17
21
"python.run()" ,
18
22
async ( span ) => {
19
23
const result = await x ( pythonBin , scriptArgs , {
20
24
...options ,
25
+ nodeOptions : {
26
+ ...( options . nodeOptions || { } ) ,
27
+ env : {
28
+ ...process . env ,
29
+ ...options . env ,
30
+ } ,
31
+ } ,
21
32
throwOnError : false , // Ensure errors are handled manually
22
33
} ) ;
23
34
@@ -48,7 +59,7 @@ export const python = {
48
59
async runScript (
49
60
scriptPath : string ,
50
61
scriptArgs : string [ ] = [ ] ,
51
- options : Partial < XOptions > = { }
62
+ options : PythonExecOptions = { }
52
63
) : Promise < Result > {
53
64
assert ( scriptPath , "Script path is required" ) ;
54
65
assert ( fs . existsSync ( scriptPath ) , `Script does not exist: ${ scriptPath } ` ) ;
@@ -63,6 +74,13 @@ export const python = {
63
74
[ scriptPath , ...scriptArgs ] ,
64
75
{
65
76
...options ,
77
+ nodeOptions : {
78
+ ...( options . nodeOptions || { } ) ,
79
+ env : {
80
+ ...process . env ,
81
+ ...options . env ,
82
+ } ,
83
+ } ,
66
84
throwOnError : false ,
67
85
}
68
86
) ;
@@ -92,7 +110,7 @@ export const python = {
92
110
) ;
93
111
} ,
94
112
95
- async runInline ( scriptContent : string , options : Partial < XOptions > = { } ) : Promise < Result > {
113
+ async runInline ( scriptContent : string , options : PythonExecOptions = { } ) : Promise < Result > {
96
114
assert ( scriptContent , "Script content is required" ) ;
97
115
98
116
return await logger . trace (
@@ -109,6 +127,13 @@ export const python = {
109
127
const pythonBin = process . env . PYTHON_BIN_PATH || "python" ;
110
128
const result = await x ( pythonBin , [ tempFilePath ] , {
111
129
...options ,
130
+ nodeOptions : {
131
+ ...( options . nodeOptions || { } ) ,
132
+ env : {
133
+ ...process . env ,
134
+ ...options . env ,
135
+ } ,
136
+ } ,
112
137
throwOnError : false ,
113
138
} ) ;
114
139
@@ -139,11 +164,18 @@ export const python = {
139
164
} ,
140
165
// Stream namespace for streaming functions
141
166
stream : {
142
- run ( scriptArgs : string [ ] = [ ] , options : Partial < XOptions > = { } ) : AsyncIterableStream < string > {
167
+ run ( scriptArgs : string [ ] = [ ] , options : PythonExecOptions = { } ) : AsyncIterableStream < string > {
143
168
const pythonBin = process . env . PYTHON_BIN_PATH || "python" ;
144
169
145
170
const pythonProcess = x ( pythonBin , scriptArgs , {
146
171
...options ,
172
+ nodeOptions : {
173
+ ...( options . nodeOptions || { } ) ,
174
+ env : {
175
+ ...process . env ,
176
+ ...options . env ,
177
+ } ,
178
+ } ,
147
179
throwOnError : false ,
148
180
} ) ;
149
181
@@ -167,7 +199,7 @@ export const python = {
167
199
runScript (
168
200
scriptPath : string ,
169
201
scriptArgs : string [ ] = [ ] ,
170
- options : Partial < XOptions > = { }
202
+ options : PythonExecOptions = { }
171
203
) : AsyncIterableStream < string > {
172
204
assert ( scriptPath , "Script path is required" ) ;
173
205
assert ( fs . existsSync ( scriptPath ) , `Script does not exist: ${ scriptPath } ` ) ;
@@ -176,6 +208,13 @@ export const python = {
176
208
177
209
const pythonProcess = x ( pythonBin , [ scriptPath , ...scriptArgs ] , {
178
210
...options ,
211
+ nodeOptions : {
212
+ ...( options . nodeOptions || { } ) ,
213
+ env : {
214
+ ...process . env ,
215
+ ...options . env ,
216
+ } ,
217
+ } ,
179
218
throwOnError : false ,
180
219
} ) ;
181
220
@@ -197,7 +236,7 @@ export const python = {
197
236
} ,
198
237
} ) ;
199
238
} ,
200
- runInline ( scriptContent : string , options : Partial < XOptions > = { } ) : AsyncIterableStream < string > {
239
+ runInline ( scriptContent : string , options : PythonExecOptions = { } ) : AsyncIterableStream < string > {
201
240
assert ( scriptContent , "Script content is required" ) ;
202
241
203
242
const pythonBin = process . env . PYTHON_BIN_PATH || "python" ;
@@ -206,6 +245,13 @@ export const python = {
206
245
207
246
const pythonProcess = x ( pythonBin , [ pythonScriptPath ] , {
208
247
...options ,
248
+ nodeOptions : {
249
+ ...( options . nodeOptions || { } ) ,
250
+ env : {
251
+ ...process . env ,
252
+ ...options . env ,
253
+ } ,
254
+ } ,
209
255
throwOnError : false ,
210
256
} ) ;
211
257
0 commit comments