UNPKG

@halospv3/hce.shared-config

Version:

Automate commit message quality, changelogs, and CI/CD releases. Its `main` entry point is a Semantic Release config. Functions and classes are exposed for customization. An ESLint config, a Commitlint config, and addl. resources for .NET projects are als

59 lines (58 loc) 2.25 kB
import { isError } from "./isError.mjs"; import { promisify } from "node:util"; import { type } from "arktype"; import { constants } from "node:os"; import { exec } from "node:child_process"; //#region src/utils/execAsync.ts /** * A `promisify(exec)` wrapper to optionally assign the child process's STDERR as the {@link Error.prototype.cause}. * @see {@link promisify}, {@link exec} * @param command The command to run, with space-separated arguments. * @param [shouldSetStderrAsCause=false] If true and the child process's stderr is available, the thrown Error's {@link Error.prototype.cause} is assigned the stderr string. * @returns A promise of the child process's STDOUT and STDERR streams as strings * @throws {Error | ChildProcessSpawnException} */ async function execAsync(command, shouldSetStderrAsCause = false) { try { return await promisify(exec)(command); } catch (error) { if (!isError(error)) throw new Error(JSON.stringify(error), { cause: error }); if (shouldSetStderrAsCause && "stderr" in error && typeof error.stderr === "string" && error.stderr !== "") error.cause ??= error.stderr; if ("stdout" in error && typeof error.stdout === "string") error.message += ` STDOUT: ${error.stdout.replaceAll("\n", "\n ")}`; if ("stderr" in error && typeof error.stderr === "string") error.message += ` STDERR: ${error.stderr.replaceAll("\n", "\n ")}`; throw new ChildProcessSpawnException(error.message, error); } } const T_ExecException = type.instanceOf(Error).and({ "cmd?": "string", "code?": "number", "killed?": "boolean", "signal?": type(Object.keys(constants.signals).map((v) => type(`'${v}'`)).reduce((previous, current) => previous.or(current))).or("null"), "stdout?": "string", "stderr?": "string" }); var ChildProcessSpawnException = class extends Error { cmd; code; killed; signal; stderr; stdout; constructor(message, options) { options = T_ExecException.from(options); super(message, options); this.cmd = options.cmd; this.code = options.code; this.killed = options.killed; this.signal = options.signal; this.stderr = options.stderr; this.stdout = options.stdout; } }; //#endregion export { ChildProcessSpawnException, execAsync }; //# sourceMappingURL=execAsync.mjs.map