diff options
Diffstat (limited to 'js')
-rw-r--r-- | js/process.ts | 14 | ||||
-rw-r--r-- | js/process_test.ts | 19 |
2 files changed, 33 insertions, 0 deletions
diff --git a/js/process.ts b/js/process.ts index 495f6a9ce..c47a5b5dd 100644 --- a/js/process.ts +++ b/js/process.ts @@ -27,6 +27,7 @@ export type ProcessStdio = "inherit" | "piped" | "null"; export interface RunOptions { args: string[]; cwd?: string; + env?: { [key: string]: string }; stdout?: ProcessStdio; stderr?: ProcessStdio; stdin?: ProcessStdio; @@ -107,11 +108,24 @@ export function run(opt: RunOptions): Process { opt.args.map(a => builder.createString(a)) ); const cwdOffset = opt.cwd == null ? -1 : builder.createString(opt.cwd); + const kvOffset: flatbuffers.Offset[] = []; + if (opt.env) { + for (const [key, val] of Object.entries(opt.env)) { + const keyOffset = builder.createString(key); + const valOffset = builder.createString(String(val)); + msg.KeyValue.startKeyValue(builder); + msg.KeyValue.addKey(builder, keyOffset); + msg.KeyValue.addValue(builder, valOffset); + kvOffset.push(msg.KeyValue.endKeyValue(builder)); + } + } + const envOffset = msg.Run.createEnvVector(builder, kvOffset); msg.Run.startRun(builder); msg.Run.addArgs(builder, argsOffset); if (opt.cwd != null) { msg.Run.addCwd(builder, cwdOffset); } + msg.Run.addEnv(builder, envOffset); if (opt.stdin) { msg.Run.addStdin(builder, stdioMap(opt.stdin!)); } diff --git a/js/process_test.ts b/js/process_test.ts index e629dbe3d..9603fb1c8 100644 --- a/js/process_test.ts +++ b/js/process_test.ts @@ -186,3 +186,22 @@ testPerm({ run: true }, async function runOutput() { assertEqual(s, "hello"); p.close(); }); + +testPerm({ run: true }, async function runEnv() { + const p = run({ + args: [ + "python", + "-c", + "import os, sys; sys.stdout.write(os.environ.get('FOO', '') + os.environ.get('BAR', ''))" + ], + env: { + FOO: "0123", + BAR: "4567" + }, + stdout: "piped" + }); + const output = await p.output(); + const s = new TextDecoder().decode(output); + assertEqual(s, "01234567"); + p.close(); +}); |