Skip to content

Commit 79a1be9

Browse files
committed
fix(build): allow passthrough of --mode argument
1 parent 82cf832 commit 79a1be9

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

__tests__/commands.spec.js

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ const fs = require('fs-extra')
99
const execa = require('execa')
1010
const portfinder = require('portfinder')
1111
const Application = require('spectron').Application
12+
// const {command} = require('yargs')
13+
const mockYargsParse = jest.fn()
14+
const mockYargsCommand = jest.fn(() => ({ parse: mockYargsParse }))
15+
jest.mock('yargs', () => ({ command: mockYargsCommand }))
1216
jest.mock('@vue/cli-service/lib/commands/build')
1317
jest.mock('fs-extra')
1418
jest.mock('electron-builder')
@@ -35,7 +39,7 @@ console.log = jest.fn()
3539
beforeEach(() => {
3640
jest.clearAllMocks()
3741
})
38-
const runCommand = async (command, options = {}, args = {}) => {
42+
const runCommand = async (command, options = {}, args = {}, rawArgs = []) => {
3943
if (!args._) args._ = []
4044
const renderConfig = new Config()
4145
// Command expects define plugin to exist
@@ -53,7 +57,8 @@ const runCommand = async (command, options = {}, args = {}) => {
5357
resolve: jest.fn(path => 'projectPath/' + path)
5458
}
5559
pluginIndex(api, options)
56-
await commands[command](args, [])
60+
61+
await commands[command](args, rawArgs)
5762
}
5863

5964
describe('build:electron', () => {
@@ -182,6 +187,44 @@ describe('build:electron', () => {
182187
// Both .js and .ts are resolved
183188
expect(mainConfig.resolve.extensions).toEqual(['.js', '.ts'])
184189
})
190+
test('--mode argument is removed from electron-builder args', async () => {
191+
await runCommand('build:electron', {}, {}, [
192+
'--keep1',
193+
'--mode',
194+
'buildMode',
195+
'--keep2'
196+
])
197+
// --mode and buildMode should have been removed
198+
expect(mockYargsParse).toBeCalledWith(['--keep1', '--keep2'])
199+
mockYargsParse.mockClear()
200+
201+
await runCommand('build:electron', {}, {}, [
202+
'--mode',
203+
'buildMode',
204+
'--keep2'
205+
])
206+
// --mode and buildMode should have been removed
207+
expect(mockYargsParse).toBeCalledWith(['--keep2'])
208+
mockYargsParse.mockClear()
209+
210+
await runCommand('build:electron', {}, {}, [
211+
'--keep1',
212+
'--mode',
213+
'buildMode'
214+
])
215+
// --mode and buildMode should have been removed
216+
expect(mockYargsParse).toBeCalledWith(['--keep1'])
217+
mockYargsParse.mockClear()
218+
219+
await runCommand('build:electron', {}, {}, ['--mode', 'buildMode'])
220+
// --mode and buildMode should have been removed
221+
expect(mockYargsParse).toBeCalledWith([])
222+
mockYargsParse.mockClear()
223+
224+
await runCommand('build:electron', {}, {}, ['--keep1', '--keep2'])
225+
// --mode and buildMode should have been removed
226+
expect(mockYargsParse).toBeCalledWith(['--keep1', '--keep2'])
227+
})
185228
})
186229

187230
describe('serve:electron', () => {

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ module.exports = (api, options) => {
4040
// Import the yargs options from electron-builder
4141
const configureBuildCommand = require('electron-builder/out/builder')
4242
.configureBuildCommand
43+
// Prevent mode arg from interfering with electron-builder
44+
const modeIndex = rawArgs.indexOf('--mode')
45+
if (modeIndex !== -1) rawArgs.splice(modeIndex, 2)
4346
// Parse the raw arguments using electron-builder yargs config
4447
const builderArgs = yargs
4548
.command(['build', '*'], 'Build', configureBuildCommand)

0 commit comments

Comments
 (0)