dsa-connectors-2.0/scripts/tests/command.ts

34 lines
681 B
TypeScript
Raw Normal View History

2023-12-15 22:50:02 +00:00
import { execFile, spawn } from "child_process";
interface ICommand {
readonly cmd: string;
readonly args: string[];
readonly env: {
[param: string]: string;
};
}
export async function execScript(input: ICommand): Promise<number> {
return new Promise((resolve, reject) => {
let cmdEnv = Object.create(process.env);
for (let param in input.env) {
cmdEnv[param] = input.env[param];
}
const proc = spawn(input.cmd, [...input.args], {
env: cmdEnv,
shell: true,
stdio: "inherit",
});
proc.on("exit", (code) => {
if (code !== 0) {
reject(code);
return;
}
resolve(code);
});
});
}