cmake-action/src/exec.ts
Alfi Maulana a93af4bd1e
Some checks failed
Build / Build Package (push) Failing after 1s
Test / Test Action (ubuntu-22.04) (push) Failing after 30s
Test / Test Action With Specified Directories (push) Failing after 35s
Check / Check Package (push) Failing after 1s
Test / Test Package (push) Failing after 1s
Test / Test Action Without Run Build (push) Failing after 36s
Test / Test Action With Additional Options (push) Failing after 3s
Test / Test Action With Custom Generator (push) Failing after 2s
Test / Test Action (macos-14) (push) Has been cancelled
Test / Test Action (windows-2022) (push) Has been cancelled
feat: log executed command (#524)
* feat: log executed command

Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>

* feat: log command based on spawned process arguments

Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>

---------

Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>
2024-11-24 23:34:31 +08:00

29 lines
999 B
TypeScript

import { logCommand } from "gha-utils";
import { spawn } from "node:child_process";
/**
* Executes a command with the given arguments.
*
* The command is executed with `stdin` ignored and both `stdout` and `stderr` inherited by the parent process.
*
* @param command The command to execute.
* @param args The arguments to pass to the command.
* @returns A promise that resolves when the command exits successfully or rejects if it exits with a non-zero status code or encounters an error.
*/
export async function exec(command: string, args: string[]): Promise<void> {
return new Promise<void>((resolve, reject) => {
const proc = spawn(command, args, {
stdio: ["ignore", "inherit", "inherit"],
});
logCommand(proc.spawnfile, ...proc.spawnargs.splice(1));
proc.on("error", reject);
proc.on("close", (code) => {
if (code === 0) {
resolve();
} else {
reject(new Error(`Command exited with status code ${code}`));
}
});
});
}