Skip to content

Commit

Permalink
fix(cli): warn on unknown commands
Browse files Browse the repository at this point in the history
Emit a warning and exit with status code 1.

Based on tj/commander.js#1088 (comment)
  • Loading branch information
malept committed Nov 27, 2019
1 parent 92817f4 commit 8c21409
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/api/cli/src/electron-forge.ts
Expand Up @@ -34,7 +34,13 @@ program
.command('make', 'Generate distributables for the current Electron application')
.command('start', 'Start the current Electron application')
.command('publish', 'Publish the current Electron application to GitHub')
.command('install', 'Install an Electron application from GitHub');
.command('install', 'Install an Electron application from GitHub')
.on('command:*', () => {
console.error();
console.error(`Unknown command "${program.args.join(' ')}".`.red);
console.error('See --help for a list of available commands.');
process.exit(1);
});

(async () => {
let goodSystem;
Expand Down
14 changes: 14 additions & 0 deletions packages/api/cli/test/cli_spec.ts
@@ -0,0 +1,14 @@
import chai, { expect } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import path from 'path';
import spawnPromise from 'cross-spawn-promise';

chai.use(chaiAsPromised);

describe('cli', () => {
it('should fail on unknown subcommands', async () => {
const error = await expect(spawnPromise(path.resolve(__dirname, '../../../../node_modules/.bin/ts-node'), [path.resolve(__dirname, '../src/electron-forge.ts'), 'nonexistent'])).to.eventually.be.rejected;
expect(error.exitStatus).to.equal(1);
expect(error.stderr.toString()).to.match(/Unknown command "nonexistent"/);
});
});

0 comments on commit 8c21409

Please sign in to comment.