Skip to content

Commit

Permalink
feat: add nonZero to ensureExitCode (#599)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley authored Dec 4, 2023
1 parent 31f3b5a commit 862aedd
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/execCmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type BaseExecOptions = {
/**
* Throws if this exit code is not returned by the child process.
*/
ensureExitCode?: number;
ensureExitCode?: number | 'nonZero';

/**
* The executable that should be used for execCmd.
Expand Down Expand Up @@ -97,7 +97,7 @@ const addJsonOutput = <T>(cmd: string, result: ExecCmdResult<T>, file: string):
return result;
};

const getExitCodeError = (cmd: string, expectedCode: number, output: ShellString) => {
const getExitCodeError = (cmd: string, expectedCode: number | 'nonZero', output: ShellString) => {
const errorDetails =
output.stdout || output.stderr
? // return details if they exist
Expand Down Expand Up @@ -204,6 +204,10 @@ const execCmdSync = <T>(cmd: string, options?: ExecCmdOptions): ExecCmdResult<T>
throw getExitCodeError(cmd, cmdOptions.ensureExitCode, result.shellOutput);
}

if (cmdOptions.ensureExitCode === 'nonZero' && result.shellOutput.code === 0) {
throw getExitCodeError(cmd, cmdOptions.ensureExitCode, result.shellOutput);
}

const withJson = addJsonOutput<T>(cmd, result, stdoutFileLocation);
fs.rmSync(stderrFileLocation);
fs.rmSync(stdoutFileLocation);
Expand Down

0 comments on commit 862aedd

Please sign in to comment.