Skip to content

Commit

Permalink
feat(tasks): add briefError option to exec task
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Mar 29, 2021
1 parent 870ed67 commit 166e37d
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/tasks/process/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@ import { into } from 'pipettes';
import transform from 'prefix-stream';
import execa from 'execa';

export interface ExecOptions extends execa.Options {
/**
* Produces a brief error message with only the exit code.
*/
briefError: boolean;
}

/**
* Spawns a process.
* @returns Task
*/
export function exec(
file: string,
args?: string[] | Empty,
options?: execa.Options | Empty,
options?: ExecOptions | Empty,
cb?: (ps: execa.ExecaChildProcess) => void
): Task.Async {
const opts = Object.assign({ extendEnv: true }, options || undefined);
Expand Down Expand Up @@ -66,9 +73,20 @@ export function exec(
ps.cancel();
});

let exitCode: number | null = null;
ps.on('exit', (code) => (exitCode = code));

return ps.then(
() => undefined,
(err) => (cancelled ? undefined : Promise.reject(err))
(err) => {
if (cancelled) return undefined;

if (opts.briefError) {
err.message =
`Command failed` + (exitCode ? ` with exit code ${exitCode}` : '');
}
return Promise.reject(err);
}
);
};
}

0 comments on commit 166e37d

Please sign in to comment.