Skip to content

Commit a726ec9

Browse files
committed
fix(serve): move install-app-deps to postinstall
1 parent f164f52 commit a726ec9

File tree

4 files changed

+59
-40
lines changed

4 files changed

+59
-40
lines changed

__tests__/commands.spec.js

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const fs = require('fs-extra')
77
const execa = require('execa')
88
const portfinder = require('portfinder')
99
const Application = require('spectron').Application
10-
const { chainWebpack, getExternals } = require('../lib/webpackConfig')
10+
const { chainWebpack } = require('../lib/webpackConfig')
1111
// #endregion
1212

1313
// #region Mocks
@@ -419,29 +419,6 @@ describe('serve:electron', () => {
419419
// Electron was re-launched
420420
expect(execa).toHaveBeenCalledTimes(3)
421421
})
422-
423-
test('Native deps are installed if detected', async () => {
424-
getExternals.mockReturnValueOnce({
425-
someDep: {
426-
commonjs: 'someDep',
427-
commonjs2: 'someDep'
428-
}
429-
})
430-
await runCommand('serve:electron')
431-
432-
// electron-builder's install-app-deps function was called
433-
expect(mockInstallAppDeps.mock.calls[0][0]).toEqual({
434-
platform: process.platform
435-
})
436-
})
437-
438-
test('Native deps are not installed if there are none detected', async () => {
439-
getExternals.mockReturnValueOnce({})
440-
await runCommand('serve:electron')
441-
442-
// electron-builder's install-app-deps function was not called
443-
expect(mockInstallAppDeps).not.toBeCalled()
444-
})
445422
})
446423

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

__tests__/generator.spec.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ const fs = require('fs')
33
jest.mock('fs')
44

55
// Mock the generator api
6+
let pkg = {}
67
let completionCb
78
const mockApi = {
89
render: jest.fn(),
910
onCreateComplete: jest.fn(cb => {
1011
completionCb = cb
1112
}),
12-
extendPackage: jest.fn(),
13+
extendPackage: jest.fn(newPkg => {
14+
pkg = newPkg
15+
}),
1316
// Make sure api.resolve(path) is used
1417
resolve: jest.fn(path => 'apiResolve_' + path),
1518
hasPlugin: jest.fn()
@@ -23,6 +26,9 @@ test('extends gitignore if it exists', () => {
2326
fs.readFileSync.mockImplementation((path, encoding) => {
2427
// Check that utf8 encoding is set
2528
expect(encoding).toBe('utf8')
29+
if (path === 'apiResolve_./package.json') {
30+
return JSON.stringify({ scripts: {} })
31+
}
2632
// return mock content
2733
return 'existing_content'
2834
})
@@ -51,7 +57,34 @@ test("doesn't modify .gitignore if it doesn't exist", () => {
5157
'existing_content\n#Electron-builder output\n/dist_electron'
5258
)
5359
expect(fs.readFileSync).not.toBeCalledWith('apiResolve_./.gitignore', 'utf8')
54-
// Only index should have been read/written
60+
// Only index should have been written
5561
expect(fs.writeFileSync).toHaveBeenCalledTimes(1)
56-
expect(fs.readFileSync).toHaveBeenCalledTimes(1)
62+
})
63+
64+
test('Adds electron-builder install-app-deps to postInstall', () => {
65+
generator(mockApi)
66+
completionCb()
67+
expect(pkg.scripts.postinstall).toBe('electron-builder install-app-deps')
68+
})
69+
70+
test('Adds on to existing postinstall script instead of replacing', () => {
71+
fs.readFileSync.mockImplementation((path, encoding) => {
72+
// Check that utf8 encoding is set
73+
expect(encoding).toBe('utf8')
74+
// Mock existing postinstall script in app's package.json
75+
if (path === 'apiResolve_./package.json') {
76+
return JSON.stringify({
77+
scripts: { postinstall: 'existingTask' }
78+
})
79+
}
80+
// return mock content
81+
return 'existing_content'
82+
})
83+
84+
generator(mockApi)
85+
completionCb()
86+
87+
expect(pkg.scripts.postinstall).toBe(
88+
'existingTask && electron-builder install-app-deps'
89+
)
5790
})

generator/index.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@ module.exports = api => {
99
index = index.replace(
1010
/^\s*?<head.*?>\s*?$/m,
1111
`<head>\n <% if (BASE_URL === './') { %><base href="app://./" /><% } %>
12-
<% if (VUE_APP_NODE_MODULES_PATH !== "false") { %><script>require('module').globalPaths.push('<%= VUE_APP_NODE_MODULES_PATH %>')</script><% } %>`
12+
<% if (VUE_APP_NODE_MODULES_PATH !== "false") { %><script>require('module').globalPaths.push('<%= VUE_APP_NODE_MODULES_PATH %>')</script><% } %>`
1313
)
1414
// Write updated index.html
1515
fs.writeFileSync(api.resolve('./public/index.html'), index)
16+
1617
// Update .gitignore if it exists
1718
if (fs.existsSync(api.resolve('./.gitignore'))) {
1819
let gitignore = fs.readFileSync(api.resolve('./.gitignore'), 'utf8')
1920
// Add /dist_electron to gitignore
2021
gitignore = gitignore + '\n#Electron-builder output\n/dist_electron'
2122
fs.writeFileSync(api.resolve('./.gitignore'), gitignore)
2223
}
24+
2325
if (api.hasPlugin('typescript')) {
2426
let background
2527
if (fs.existsSync(api.resolve('./src/background.js'))) {
@@ -36,10 +38,26 @@ module.exports = api => {
3638
fs.writeFileSync(api.resolve('./src/background.ts'), background)
3739
}
3840
})
41+
42+
// Add electron-builder install-app-deps to postinstall
43+
let postinstallScript
44+
let pkg = fs.readFileSync(api.resolve('./package.json'), 'utf8')
45+
pkg = JSON.parse(pkg)
46+
if (pkg.scripts && pkg.scripts.postinstall) {
47+
// Add on to existing script if it exists
48+
postinstallScript = `${
49+
// Existing script
50+
pkg.scripts.postinstall
51+
} && electron-builder install-app-deps`
52+
} else {
53+
// Create new postinstall script
54+
postinstallScript = 'electron-builder install-app-deps'
55+
}
3956
api.extendPackage({
4057
scripts: {
4158
'build:electron': 'vue-cli-service build:electron',
42-
'serve:electron': 'vue-cli-service serve:electron'
59+
'serve:electron': 'vue-cli-service serve:electron',
60+
postinstall: postinstallScript
4361
},
4462
devDependencies: {
4563
electron: '^2.0.2'

index.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ module.exports = (api, options) => {
221221
.plugin('env')
222222
.use(webpack.EnvironmentPlugin, [{ NODE_ENV: 'development' }])
223223
mainConfig.entry('background').add(api.resolve(mainProcessFile))
224+
// Set external (native) deps
225+
mainConfig.externals(getExternals(api, pluginOptions))
224226
if (usesTypescript) {
225227
mainConfig.resolve.extensions.merge(['.js', '.ts'])
226228
mainConfig.module
@@ -231,17 +233,6 @@ module.exports = (api, options) => {
231233
.options({ transpileOnly: !mainProcessTypeChecking })
232234
}
233235

234-
// Build native deps if needed
235-
const externals = getExternals(api, pluginOptions)
236-
if (externals && Object.keys(externals).length > 0) {
237-
// Set externals in main process webpack config
238-
mainConfig.externals(externals)
239-
console.log('Installing native deps with electron-builder:')
240-
await require('electron-builder/out/cli/install-app-deps.js').installAppDeps(
241-
{ platform: process.platform }
242-
)
243-
}
244-
245236
console.log('\nStarting development server:\n')
246237
// Run the serve command
247238
const server = await api.service.run('serve', {

0 commit comments

Comments
 (0)