Skip to content

Commit 0e2b395

Browse files
committed
feat(serve): pass command line arguments to electron
1 parent 793805e commit 0e2b395

File tree

2 files changed

+76
-11
lines changed

2 files changed

+76
-11
lines changed

__tests__/commands.spec.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,61 @@ describe('electron:serve', () => {
614614
'projectPath/outputDir/index.js'
615615
)
616616
})
617+
618+
test.each([['--dashboard'], ['--debug'], ['--headless']])(
619+
'%s argument is not passed to electron',
620+
async (...args) => {
621+
await runCommand('electron:serve', {}, {}, [
622+
'--keep1',
623+
...args,
624+
'--keep2'
625+
])
626+
// Custom args should have been removed, and other args kept
627+
let calledArgs = execa.mock.calls[0][1]
628+
// Remove dist_electron
629+
calledArgs.shift()
630+
expect(calledArgs).toEqual(['--keep1', '--keep2'])
631+
execa.mockClear()
632+
633+
await runCommand('electron:serve', {}, {}, [...args, '--keep2'])
634+
// Custom args should have been removed, and other args kept
635+
calledArgs = execa.mock.calls[0][1]
636+
// Remove dist_electron
637+
calledArgs.shift()
638+
expect(calledArgs).toEqual(['--keep2'])
639+
execa.mockClear()
640+
641+
await runCommand('electron:serve', {}, {}, ['--keep1', ...args])
642+
// Custom args should have been removed, and other args kept
643+
calledArgs = execa.mock.calls[0][1]
644+
// Remove dist_electron
645+
calledArgs.shift()
646+
expect(calledArgs).toEqual(['--keep1'])
647+
execa.mockClear()
648+
649+
await runCommand('electron:serve', {}, {}, args)
650+
// Custom args should have been removed
651+
calledArgs = execa.mock.calls[0][1]
652+
// Remove dist_electron
653+
calledArgs.shift()
654+
expect(calledArgs).toEqual([])
655+
execa.mockClear()
656+
657+
await runCommand('electron:serve', {}, {}, ['--keep1', '--keep2'])
658+
// Nothing should be removed
659+
calledArgs = execa.mock.calls[0][1]
660+
// Remove dist_electron
661+
calledArgs.shift()
662+
expect(calledArgs).toEqual(['--keep1', '--keep2'])
663+
execa.mockClear()
664+
}
665+
)
666+
667+
test('Electron is launched with arguments', async () => {
668+
await runCommand('electron:serve', {}, {}, ['--expected'])
669+
const args = execa.mock.calls[0][1]
670+
expect(args).toContain('--expected')
671+
})
617672
})
618673

619674
describe('Custom webpack chain', () => {

index.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ module.exports = (api, options) => {
3737
? true
3838
: pluginOptions.bundleMainProcess
3939

40+
const removeArg = (arg, count, rawArgs) => {
41+
const index = rawArgs.indexOf(arg)
42+
if (index !== -1) rawArgs.splice(index, count)
43+
}
44+
4045
// Apply custom webpack config
4146
api.chainWebpack(async config => {
4247
chainWebpack(api, pluginOptions, config)
@@ -62,15 +67,11 @@ module.exports = (api, options) => {
6267
const configureBuildCommand = require('electron-builder/out/builder')
6368
.configureBuildCommand
6469
// Prevent custom args from interfering with electron-builder
65-
const removeArg = (arg, count) => {
66-
const index = rawArgs.indexOf(arg)
67-
if (index !== -1) rawArgs.splice(index, count)
68-
}
69-
removeArg('--mode', 2)
70-
removeArg('--dest', 2)
71-
removeArg('--legacy', 1)
72-
removeArg('--dashboard', 1)
73-
removeArg('--skipBundle', 1)
70+
removeArg('--mode', 2, rawArgs)
71+
removeArg('--dest', 2, rawArgs)
72+
removeArg('--legacy', 1, rawArgs)
73+
removeArg('--dashboard', 1, rawArgs)
74+
removeArg('--skipBundle', 1, rawArgs)
7475
// Parse the raw arguments using electron-builder yargs config
7576
const builderArgs = yargs
7677
.command(['build', '*'], 'Build', configureBuildCommand)
@@ -200,7 +201,7 @@ module.exports = (api, options) => {
200201
usage: 'vue-cli-service serve:electron',
201202
details: `See https://nklayman.github.io/vue-cli-plugin-electron-builder/ for more details about this plugin.`
202203
},
203-
async args => {
204+
async (args, rawArgs) => {
204205
// Use custom config for webpack
205206
process.env.IS_ELECTRON = true
206207
const execa = require('execa')
@@ -210,6 +211,11 @@ module.exports = (api, options) => {
210211
]
211212
const mainProcessArgs = pluginOptions.mainProcessArgs || []
212213

214+
// Don't pass command args to electron
215+
removeArg('--dashboard', 1, rawArgs)
216+
removeArg('--debug', 1, rawArgs)
217+
removeArg('--headless', 1, rawArgs)
218+
213219
// Run the serve command
214220
const server = await api.service.run('serve', {
215221
_: [],
@@ -358,6 +364,8 @@ module.exports = (api, options) => {
358364
info(
359365
'Launching Electron with arguments: "' +
360366
mainProcessArgs.join(' ') +
367+
' ' +
368+
rawArgs.join(' ') +
361369
'" ...'
362370
)
363371
} else {
@@ -378,7 +386,9 @@ module.exports = (api, options) => {
378386
// Have it load the main process file built with webpack
379387
outputDir,
380388
// Append other arguments specified in plugin options
381-
...mainProcessArgs
389+
...mainProcessArgs,
390+
// Append args passed to command
391+
...rawArgs
382392
],
383393
{
384394
cwd: api.resolve('.'),

0 commit comments

Comments
 (0)