Skip to content

Commit

Permalink
feat(bin): exits with code 1 when it has failed to parse arguments an…
Browse files Browse the repository at this point in the history
…d help is shown

BREAKING CHANGE: bin exits with code 1 at arguments parsing failure
  • Loading branch information
rafamel committed Feb 3, 2019
1 parent 427f697 commit d5aee99
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/bin/exits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ const schema = {
}
};

(async () => {
main();
async function main(): Promise<void> {
const failedHelp = (): void => {
program.outputHelp();
process.exit(1);
};

const pkg = JSON.parse(
await pify(fs.readFile)(path.join(__dirname, '../package.json'))
);
Expand All @@ -53,18 +59,16 @@ const schema = {
)
.option(
'--log <level>',
`\n\tLogging level, one of trace, debug, info, warn, error, or silent.\n\tDefault: ${DEFAULT_LOG_LEVEL}\n\tExample: --logger info`
`\n\tLogging level, one of trace, debug, info, warn, error, or silent.\n\tDefault: ${DEFAULT_LOG_LEVEL}\n\tExample: --log info`
)
.option('--fail', `\n\tAlso exit with code 1 if the after command fails.`)
.parse(process.argv);

if (program.args.length < 1 || program.args.length > 2) {
return program.help();
}
if (program.args.length < 1 || program.args.length > 2) failedHelp();
const first: string[] = toArgv(program.args[0]);
const last: string[] = program.args[1] ? toArgv(program.args[1]) : [];

if (!first.length) return program.help();
if (!first.length) return failedHelp();

const stdio = program.stdio ? program.stdio.split(',') : ['inherit'];
const at = program.at
Expand All @@ -73,7 +77,7 @@ const schema = {
const log = program.log || DEFAULT_LOG_LEVEL;

const valid = ajv.validate(schema, { stdio, at, log });
if (!valid) return program.help();
if (!valid) return failedHelp();

options({ logger: log, spawned: { signals: 'none', wait: 'all' } });
attach();
Expand Down Expand Up @@ -109,4 +113,4 @@ const schema = {
else throw e;
}
}, 0);
})();
}

0 comments on commit d5aee99

Please sign in to comment.