feat(process): support env overrides in exec

This commit is contained in:
Peter Steinberger
2026-01-01 22:55:21 +01:00
parent 1a3323a261
commit fbf5efb570
2 changed files with 25 additions and 1 deletions

22
src/process/exec.test.ts Normal file
View File

@@ -0,0 +1,22 @@
import { describe, expect, it } from "vitest";
import { runCommandWithTimeout } from "./exec.js";
describe("runCommandWithTimeout", () => {
it("passes env overrides to child", async () => {
const result = await runCommandWithTimeout(
[
process.execPath,
"-e",
'process.stdout.write(process.env.CLAWDIS_TEST_ENV ?? "")',
],
{
timeoutMs: 5_000,
env: { CLAWDIS_TEST_ENV: "ok" },
},
);
expect(result.code).toBe(0);
expect(result.stdout).toBe("ok");
});
});

View File

@@ -47,6 +47,7 @@ export type CommandOptions = {
timeoutMs: number;
cwd?: string;
input?: string;
env?: NodeJS.ProcessEnv;
};
export async function runCommandWithTimeout(
@@ -57,13 +58,14 @@ export async function runCommandWithTimeout(
typeof optionsOrTimeout === "number"
? { timeoutMs: optionsOrTimeout }
: optionsOrTimeout;
const { timeoutMs, cwd, input } = options;
const { timeoutMs, cwd, input, env } = options;
// Spawn with inherited stdin (TTY) so tools like `pi` stay interactive when needed.
return await new Promise((resolve, reject) => {
const child = spawn(argv[0], argv.slice(1), {
stdio: [input ? "pipe" : "inherit", "pipe", "pipe"],
cwd,
env: env ? { ...process.env, ...env } : process.env,
});
let stdout = "";
let stderr = "";