-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use asynchronous version of the exec file function (#522)
* feat: add `exec` function Signed-off-by: Alfi Maulana <[email protected]> * feat: use `exec` function in `configureProject` and `buildProject` functions Signed-off-by: Alfi Maulana <[email protected]> --------- Signed-off-by: Alfi Maulana <[email protected]>
- Loading branch information
Showing
6 changed files
with
117 additions
and
48 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { exec } from "./exec.js"; | ||
|
||
describe("execute commands", () => { | ||
it("should successfully execute a command", async () => { | ||
await exec("node", ["--version"]); | ||
}); | ||
|
||
it("should fail to execute a command", async () => { | ||
await expect(exec("node", ["--invalid"])).rejects.toThrow( | ||
"Command exited with status code 9", | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
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"], | ||
}); | ||
proc.on("error", reject); | ||
proc.on("close", (code) => { | ||
if (code === 0) { | ||
resolve(); | ||
} else { | ||
reject(new Error(`Command exited with status code ${code}`)); | ||
} | ||
}); | ||
}); | ||
} |