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

feat: add app.commandLine.hasSwitch() / app.commandLine.getSwitchValue() #16282

Merged
merged 5 commits into from Jan 7, 2019
Merged
Changes from 2 commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -1159,6 +1159,20 @@ correctly.

**Note:** This will not affect `process.argv`.

### `app.commandLine.hasSwitch(switch)`

* `switch` String - A command-line switch

Returns `Boolean` - Whether the command-line switch is present.

### `app.commandLine.getSwitchValue(switch)`

* `switch` String - A command-line switch

Returns `String` - The command-line switch value.

**Note:** When the switch is not present, it returns empty string.

This comment has been minimized.

Copy link
@alexeykuzmin

alexeykuzmin Jan 7, 2019

Contributor

This is sad.

This comment has been minimized.

Copy link
@miniak

miniak Jan 7, 2019

Author Contributor

that's how the chromium API behaves


### `app.enableSandbox()` _Experimental_ _macOS_ _Windows_

Enables full sandbox mode on the app.
@@ -25,6 +25,7 @@ Object.assign(app, {
return Menu.getApplicationMenu()
},
commandLine: {
...process.atomBinding('command_line'),
appendSwitch (...args) {
This conversation was marked as resolved by miniak

This comment has been minimized.

Copy link
@deepak1556

deepak1556 Jan 7, 2019

Member

You could move these methods implemented via app module into command_line module in a followup PR.

This comment has been minimized.

Copy link
@miniak

miniak Jan 7, 2019

Author Contributor

done in this PR

const castedArgs = args.map((arg) => {
return typeof arg !== 'string' ? `${arg}` : arg
@@ -6,6 +6,7 @@ const https = require('https')
const net = require('net')
const fs = require('fs')
const path = require('path')
const cp = require('child_process')
const { ipcRenderer, remote } = require('electron')
const { emittedOnce } = require('./events-helpers')
const { closeWindow } = require('./window-helpers')
@@ -1091,4 +1092,73 @@ describe('app module', () => {
return expect(app.whenReady()).to.be.eventually.fulfilled
})
})

describe('commandLine.hasSwitch', () => {
it('returns true when present', () => {
app.commandLine.appendSwitch('foobar1')
expect(app.commandLine.hasSwitch('foobar1')).to.be.true()
})

it('returns false when not present', () => {
expect(app.commandLine.hasSwitch('foobar2')).to.be.false()
})
})

describe('commandLine.hasSwitch (existing argv)', () => {
it('returns true when present', async () => {
const { hasSwitch } = await runCommandLineTestApp('--foobar')
expect(hasSwitch).to.be.true()
})

it('returns false when not present', async () => {
const { hasSwitch } = await runCommandLineTestApp()
expect(hasSwitch).to.be.false()
})
})

describe('commandLine.getSwitchValue', () => {
it('returns the value when present', () => {
app.commandLine.appendSwitch('foobar', 'test')
expect(app.commandLine.getSwitchValue('foobar')).to.equal('test')
})

it('returns an empty string when present without value', () => {
app.commandLine.appendSwitch('foobar1')
expect(app.commandLine.getSwitchValue('foobar1')).to.equal('')
})

it('returns an empty string when not present', () => {
expect(app.commandLine.getSwitchValue('foobar2')).to.equal('')
})
})

describe('commandLine.getSwitchValue (existing argv)', () => {
it('returns the value when present', async () => {
const { getSwitchValue } = await runCommandLineTestApp('--foobar=test')
expect(getSwitchValue).to.equal('test')
})

it('returns an empty string when present without value', async () => {
const { getSwitchValue } = await runCommandLineTestApp('--foobar')
expect(getSwitchValue).to.equal('')
})

it('returns an empty string when not present', async () => {
const { getSwitchValue } = await runCommandLineTestApp()
expect(getSwitchValue).to.equal('')
})
})

async function runCommandLineTestApp (...args) {
const appPath = path.join(__dirname, 'fixtures', 'api', 'command-line')
const electronPath = remote.getGlobal('process').execPath
const appProcess = cp.spawn(electronPath, [appPath, ...args])

let output = ''
appProcess.stdout.on('data', (data) => { output += data })

await emittedOnce(appProcess.stdout, 'end')

return JSON.parse(output)
}
})
@@ -0,0 +1,15 @@
const { app } = require('electron')

app.on('ready', () => {
const payload = {
hasSwitch: app.commandLine.hasSwitch('foobar'),
getSwitchValue: app.commandLine.getSwitchValue('foobar')
}

process.stdout.write(JSON.stringify(payload))
process.stdout.end()

setImmediate(() => {
app.quit()
})
})
@@ -0,0 +1,4 @@
{
"name": "command-line",
"main": "main.js"
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.