From 8c214096957e714c638c31b9bb3e53b4dc81d1a7 Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Wed, 27 Nov 2019 01:11:17 -0800 Subject: [PATCH 1/3] fix(cli): warn on unknown commands Emit a warning and exit with status code 1. Based on https://github.com/tj/commander.js/issues/1088#issuecomment-549018472 --- packages/api/cli/src/electron-forge.ts | 8 +++++++- packages/api/cli/test/cli_spec.ts | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 packages/api/cli/test/cli_spec.ts diff --git a/packages/api/cli/src/electron-forge.ts b/packages/api/cli/src/electron-forge.ts index efb804114a..d78c3eb8f0 100755 --- a/packages/api/cli/src/electron-forge.ts +++ b/packages/api/cli/src/electron-forge.ts @@ -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; diff --git a/packages/api/cli/test/cli_spec.ts b/packages/api/cli/test/cli_spec.ts new file mode 100644 index 0000000000..b887ebedb8 --- /dev/null +++ b/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"/); + }); +}); From f33e4dccf757a7a5a77552e550d45a0d66f8dd3b Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Wed, 27 Nov 2019 01:24:07 -0800 Subject: [PATCH 2/3] Add packages to CLI package.json --- packages/api/cli/package.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/api/cli/package.json b/packages/api/cli/package.json index 3c5879f68a..b1859c6fcf 100644 --- a/packages/api/cli/package.json +++ b/packages/api/cli/package.json @@ -12,6 +12,8 @@ }, "devDependencies": { "chai": "4.2.0", + "chai-as-promised": "^7.0.0", + "cross-spawn-promise": "^0.10.1", "mocha": "^6.2.2" }, "dependencies": { From e2aabd20bca4a4c1b1f7a29ff29248f10f9ecb35 Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Wed, 27 Nov 2019 01:49:25 -0800 Subject: [PATCH 3/3] Fix test on Windows --- packages/api/cli/test/cli_spec.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/api/cli/test/cli_spec.ts b/packages/api/cli/test/cli_spec.ts index b887ebedb8..6633257431 100644 --- a/packages/api/cli/test/cli_spec.ts +++ b/packages/api/cli/test/cli_spec.ts @@ -5,9 +5,14 @@ import spawnPromise from 'cross-spawn-promise'; chai.use(chaiAsPromised); +function tsNodePath() { + const tsNode = path.resolve(__dirname, '../../../../node_modules/.bin/ts-node'); + return process.platform === 'win32' ? `${tsNode}.cmd` : tsNode; +} + 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; + const error = await expect(spawnPromise(tsNodePath(), [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"/); });