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

Use process.defaultApp to detect built electron app #1536

Merged
merged 2 commits into from
Feb 5, 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
3 changes: 2 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
mleguen marked this conversation as resolved.
Show resolved Hide resolved
// 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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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