From d0e437912917d6a66bb5128992fa2f566a5f830b Mon Sep 17 00:00:00 2001 From: Mael Le Guen Date: Wed, 5 Feb 2020 12:00:35 +0100 Subject: [PATCH] feat: display appropriate $0 for electron apps (#1536) Use process.defaultApp to detect built electron app --- docs/api.md | 3 ++- index.js | 3 ++- lib/process-argv.js | 21 +++++++++++++++++++++ test/yargs.js | 15 +++++++++++++++ yargs.js | 3 ++- 5 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 lib/process-argv.js diff --git a/docs/api.md b/docs/api.md index d5b8d2279..2c4fd19da 100644 --- a/docs/api.md +++ b/docs/api.md @@ -1273,7 +1273,8 @@ if (command === 'hello') { .scriptName($0) ------------------ -Set the name of your script ($0). Default is the base filename executed by node (`process.argv[1]`) +Set the name of your script ($0). Default is the base filename executed by node +(`process.argv[1]` or `process.argv[0]` for built electron apps) Example: diff --git a/index.js b/index.js index 2db543ed3..e1a897f8d 100644 --- a/index.js +++ b/index.js @@ -3,8 +3,9 @@ // without running as a singleton do: // require('yargs/yargs')(process.argv.slice(2)) const yargs = require('./yargs') +const processArgv = require('./lib/process-argv') -Argv(process.argv.slice(2)) +Argv(processArgv.getProcessArgvWithoutBin()) module.exports = Argv diff --git a/lib/process-argv.js b/lib/process-argv.js new file mode 100644 index 000000000..691a6933f --- /dev/null +++ b/lib/process-argv.js @@ -0,0 +1,21 @@ +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 + return 1 +} + +function getProcessArgvWithoutBin () { + return process.argv.slice(getProcessArgvBinIndex() + 1) +} + +function getProcessArgvBin () { + return process.argv[getProcessArgvBinIndex()] +} + +module.exports = { + getProcessArgvBin, + getProcessArgvWithoutBin +} diff --git a/test/yargs.js b/test/yargs.js index be7e4bba8..1ecea4c83 100644 --- a/test/yargs.js +++ b/test/yargs.js @@ -15,11 +15,17 @@ const noop = () => {} const implicationsFailedPattern = new RegExp(english['Implications failed:']) describe('yargs dsl tests', () => { + const oldProcess = {} + beforeEach(() => { + oldProcess.argv = process.argv + oldProcess.defaultApp = process.defaultApp yargs = require('../') }) afterEach(() => { + process.argv = oldProcess.argv + process.defaultApp = oldProcess.defaultApp delete require.cache[require.resolve('../')] }) @@ -32,6 +38,15 @@ describe('yargs dsl tests', () => { yargs.$0.should.equal('ndm') }) + it('should not remove the 1st argument of built electron apps', () => { + delete require.cache[require.resolve('../')] + process.argv = ['/usr/local/bin/app', '-f', 'toto'] + process.defaultApp = false + yargs = require('../') + const argv = yargs.parse() + argv['f'].should.equal('toto') + }) + it('accepts an object for aliases', () => { const argv = yargs([]) .alias({ diff --git a/yargs.js b/yargs.js index 78bfd5778..d9a30ac5b 100644 --- a/yargs.js +++ b/yargs.js @@ -18,6 +18,7 @@ const setBlocking = require('set-blocking') const applyExtends = require('./lib/apply-extends') const { globalMiddlewareFactory } = require('./lib/middleware') const YError = require('./lib/yerror') +const processArgv = require('./lib/process-argv') exports = module.exports = Yargs function Yargs (processArgs, cwd, parentRequire) { @@ -64,7 +65,7 @@ function Yargs (processArgs, cwd, parentRequire) { }) .join(' ').trim() - if (process.env._ !== undefined && process.argv[1] === process.env._) { + if (process.env._ !== undefined && processArgv.getProcessArgvBin() === process.env._) { self.$0 = process.env._.replace( `${path.dirname(process.execPath)}/`, '' )