Skip to content

Commit

Permalink
feat: display appropriate $0 for electron apps (#1536)
Browse files Browse the repository at this point in the history
Use process.defaultApp to detect built electron app
  • Loading branch information
mleguen authored and bcoe committed Feb 5, 2020
1 parent 7293ad5 commit d0e4379
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
3 changes: 2 additions & 1 deletion docs/api.md
Expand Up @@ -1273,7 +1273,8 @@ if (command === 'hello') {
<a name="scriptName"></a>.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:

Expand Down
3 changes: 2 additions & 1 deletion index.js
Expand Up @@ -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

Expand Down
21 changes: 21 additions & 0 deletions 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
}
15 changes: 15 additions & 0 deletions test/yargs.js
Expand Up @@ -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('../')]
})

Expand All @@ -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({
Expand Down
3 changes: 2 additions & 1 deletion yargs.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)}/`, ''
)
Expand Down

0 comments on commit d0e4379

Please sign in to comment.