From a0b61ac21e2b554aa73dbf1a66d4a7af94047c2f Mon Sep 17 00:00:00 2001 From: Mael Le Guen Date: Thu, 20 Feb 2020 11:50:36 +0100 Subject: [PATCH] fix(yargs): correct support of bundled electron apps (#1554) --- lib/process-argv.js | 23 ++++++++++++++++++----- test/yargs.js | 29 +++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/lib/process-argv.js b/lib/process-argv.js index 691a6933f..bfe91534d 100644 --- a/lib/process-argv.js +++ b/lib/process-argv.js @@ -1,12 +1,25 @@ function getProcessArgvBinIndex () { - // Built Electron app: app argv1 argv2 ... argvn - // (process.defaultApp is set to false by electron for built app for this purpose, - // see https://github.com/electron/electron/issues/4690#issuecomment-217435222) - if (process.defaultApp === false) return 0 - // Default: node app.js argv1 argv2 ... argvn + // The binary name is the first command line argument for: + // - bundled Electron apps: bin argv1 argv2 ... argvn + if (isBundledElectronApp()) return 0 + // or the second one (default) for: + // - standard node apps: node bin.js argv1 argv2 ... argvn + // - unbundled Electron apps: electron bin.js argv1 arg2 ... argvn return 1 } +function isBundledElectronApp () { + // process.defaultApp is either set by electron in an electron unbundled app, or undefined + // see https://github.com/electron/electron/blob/master/docs/api/process.md#processdefaultapp-readonly + return isElectronApp() && !process.defaultApp +} + +function isElectronApp () { + // process.versions.electron is either set by electron, or undefined + // see https://github.com/electron/electron/blob/master/docs/api/process.md#processversionselectron-readonly + return !!process.versions.electron +} + function getProcessArgvWithoutBin () { return process.argv.slice(getProcessArgvBinIndex() + 1) } diff --git a/test/yargs.js b/test/yargs.js index 1c13791c6..a16946fe5 100644 --- a/test/yargs.js +++ b/test/yargs.js @@ -15,17 +15,19 @@ const noop = () => {} const implicationsFailedPattern = new RegExp(english['Implications failed:']) describe('yargs dsl tests', () => { - const oldProcess = {} + const oldProcess = { versions: {} } beforeEach(() => { oldProcess.argv = process.argv oldProcess.defaultApp = process.defaultApp + oldProcess.versions.electron = process.versions.electron yargs = require('../') }) afterEach(() => { process.argv = oldProcess.argv process.defaultApp = oldProcess.defaultApp + process.versions.electron = oldProcess.versions.electron delete require.cache[require.resolve('../')] }) @@ -38,13 +40,32 @@ describe('yargs dsl tests', () => { yargs.$0.should.equal('ndm') }) - it('should not remove the 1st argument of built electron apps', () => { + it('should not remove the 1st argument of bundled electron apps', () => { delete require.cache[require.resolve('../')] - process.argv = ['/usr/local/bin/app', '-f', 'toto'] - process.defaultApp = false + process.argv = ['/usr/local/bin/app', '-f', 'toto', 'tutu'] + process.versions.electron = '10.0.0-nightly.20200211' yargs = require('../') const argv = yargs.parse() + argv.should.have.property('f') argv.f.should.equal('toto') + argv._.should.deep.equal(['tutu']) + }) + + it('should remove the 1st argument of unbundled electron apps', () => { + delete require.cache[require.resolve('../')] + process.argv = ['/usr/local/bin/electron', 'app.js', '-f', 'toto', 'tutu'] + process.versions.electron = '10.0.0-nightly.20200211' + // Same syntax as in electron + Object.defineProperty(process, 'defaultApp', { + configurable: false, + enumerable: true, + value: true + }) + yargs = require('../') + const argv = yargs.parse() + argv.should.have.property('f') + argv.f.should.equal('toto') + argv._.should.deep.equal(['tutu']) }) it('accepts an object for aliases', () => {