Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(yargs): correct support of bundled electron apps #1554

Merged
merged 1 commit into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 18 additions & 5 deletions lib/process-argv.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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