@halospv3/hce.shared-config
Version:
Automate commit message quality, changelogs, and CI/CD releases. Exports a semantic-release shareable configuration deserialized from this package's '.releaserc.yml'. Shared resources for .NET projects are also distributed with this package.
60 lines (57 loc) • 2.34 kB
JavaScript
import { type } from 'arktype';
import { exec } from 'node:child_process';
import { constants } from 'node:os';
import { promisify } from 'node:util';
import { isError } from './isError.mjs';
/* eslint-disable jsdoc/no-defaults */
/**
* 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 [setStderrAsCause=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, setStderrAsCause = false) {
return await promisify(exec)(command).catch(error => {
if (!isError(error)) throw new Error(JSON.stringify(error));
if (setStderrAsCause && 'stderr' in error && typeof error.stderr === 'string' && error.stderr !== '') error.cause ??= error.stderr;
if ('stdout' in error && typeof error.stdout === 'string') {
error.message += '\nSTDOUT:\n' + ` ${error.stdout.replaceAll('\n', '\n ')}`;
}
if ('stderr' in error && typeof error.stderr === 'string') {
error.message += '\nSTDERR:\n' + ` ${error.stderr.replaceAll('\n', '\n ')}`;
}
throw new ChildProcessSpawnException(error.message, error);
});
}
const T_ExecException = type('Error').and({
'cmd?': 'string | null',
'killed?': 'boolean | null',
'code?': 'number | null',
'signal?': type.null.or(Object.keys(constants.signals).map(v => type(`'${v}'`))
// eslint-disable-next-line unicorn/no-array-reduce
.reduce((previous, current) => previous.or(current))),
'stdout?': 'string',
'stderr?': 'string'
});
class ChildProcessSpawnException extends Error {
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;
}
cmd;
code;
killed;
signal;
stderr;
stdout;
}
export { ChildProcessSpawnException, execAsync };
//# sourceMappingURL=execAsync.mjs.map