Navigation Menu

Skip to content

Commit

Permalink
fix(yargs): correct support of bundled electron apps (#1554)
Browse files Browse the repository at this point in the history
  • Loading branch information
mleguen committed Feb 20, 2020
1 parent 94fa38c commit a0b61ac
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
23 changes: 18 additions & 5 deletions 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)
}
Expand Down
29 changes: 25 additions & 4 deletions test/yargs.js
Expand Up @@ -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('../')]
})

Expand All @@ -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', () => {
Expand Down

0 comments on commit a0b61ac

Please sign in to comment.