Skip to content

Commit

Permalink
fix(cli): handle non-Error errors better
Browse files Browse the repository at this point in the history
- append package-specific error to CLI output
- update snapshot test
  • Loading branch information
boneskull committed Oct 18, 2022
1 parent 38a2bd1 commit 00a094e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
22 changes: 18 additions & 4 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const yargs = require('yargs/yargs');
const {version} = require('../package.json');
const {Smoker, events} = require('./index.js');
const ora = require('ora');
const {blue, white} = require('chalk');
const {blue, white, red, yellow} = require('chalk');
const BEHAVIOR_GROUP = 'Behavior:';

/**
Expand Down Expand Up @@ -142,7 +142,8 @@ async function main(args) {
await smoker.smoke();
} else {
const spinner = ora();

/** @type {Events['RunScriptFailed'][]} */
const scriptFailedEvts = [];
smoker
.on(events.SMOKE_BEGIN, () => {
console.error(
Expand Down Expand Up @@ -207,7 +208,8 @@ async function main(args) {
.on(events.RUN_SCRIPT_BEGIN, ({current, total}) => {
spinner.text = `Running script ${current}/${total}...`;
})
.on(events.RUN_SCRIPT_FAILED, () => {
.on(events.RUN_SCRIPT_FAILED, (evt) => {
scriptFailedEvts.push(evt);
process.exitCode = 1;
})
.on(events.RUN_SCRIPTS_OK, ({total}) => {
Expand All @@ -222,7 +224,18 @@ async function main(args) {
process.exitCode = 1;
})
.on(events.SMOKE_FAILED, (err) => {
spinner.fail(err.message);
spinner.fail(err?.message ?? err);

if (scriptFailedEvts.length) {
for (const evt of scriptFailedEvts) {
console.error(
`\n${red('Error details')} for failed package ${yellow(
evt.pkgName
)}:\n`
);
console.error(evt.error.stderr);
}
}
process.exitCode = 1;
})
.on(events.SMOKE_OK, () => {
Expand All @@ -232,6 +245,7 @@ async function main(args) {
}
}
)
.showHelpOnFail(false)
.help()
.strict()
.parseAsync();
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class Smoker extends createStrictEventEmitterClass() {
await this.runScripts(packItems);
this.emit(SMOKE_OK);
} catch (err) {
this.emit(SMOKE_FAILED, /** @type {Error} */ (err));
this.emit(SMOKE_FAILED, /** @type {any} */ (err));
throw err;
} finally {
await this.cleanup();
Expand Down Expand Up @@ -504,7 +504,7 @@ class Smoker extends createStrictEventEmitterClass() {
results.push(result);
if (value.failed && this.#bail) {
if (/missing script:/i.test(value.stderr)) {
this.emit(RUN_SCRIPT_FAILED, {error: value, current, total});
this.emit(RUN_SCRIPT_FAILED, {error: value, current, total, pkgName});
return new Error(
`Script "${script}" in package "${pkgName}" failed; npm was unable to find this script`
);
Expand All @@ -514,7 +514,7 @@ class Smoker extends createStrictEventEmitterClass() {
`Script "${script}" in package "${pkgName}" failed with exit code ${value.exitCode}: ${value.all}`
);
} else if (value.failed) {
this.emit(RUN_SCRIPT_FAILED, {error: value, current, total});
this.emit(RUN_SCRIPT_FAILED, {error: value, current, total, pkgName});
debug(
`(runScripts) Script "%s" in package "%s" failed; continuing...`,
script,
Expand Down
1 change: 1 addition & 0 deletions src/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export interface Events {
};
RunScriptFailed: {
error: ExecaReturnValue<string> | ExecaError;
pkgName: string;
total: number;
current: number;
};
Expand Down
2 changes: 1 addition & 1 deletion test/cli.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('midnight-smoker CLI', function () {
this.timeout('5s');
const actual = await run(['--version']);
expect(actual, 'to equal snapshot', {
stdout: '1.0.1',
stdout: '1.1.1',
stderr: '',
exitCode: 0,
});
Expand Down

0 comments on commit 00a094e

Please sign in to comment.