Skip to content

Commit f9f8b47

Browse files
committed
fix(generator): run install-app-deps on postuninstall, fixes #168
1 parent 1c46737 commit f9f8b47

File tree

2 files changed

+35
-34
lines changed

2 files changed

+35
-34
lines changed

__tests__/generator.spec.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ describe('background.js', () => {
126126
fs.readFileSync.mockImplementation((path, encoding) => {
127127
// Check that utf8 encoding is set
128128
expect(encoding).toBe('utf8')
129-
// Mock existing postinstall script in app's package.json
129+
// Mock existing scripts in app's package.json
130130
if (path === 'apiResolve_./package.json') {
131131
return JSON.stringify({
132132
scripts: {}
@@ -144,22 +144,20 @@ describe('background.js', () => {
144144
})
145145
})
146146

147-
describe('package.json', () => {
148-
test('Adds electron-builder install-app-deps to postInstall', () => {
147+
describe.each(['postinstall', 'postuninstall'])('package.json (%s)', script => {
148+
test(`Adds electron-builder install-app-deps to ${script}`, () => {
149149
generator(mockApi)
150150
completionCb()
151-
expect(pkg.scripts.postinstall).toBe('electron-builder install-app-deps')
151+
expect(pkg.scripts[script]).toBe('electron-builder install-app-deps')
152152
})
153153

154-
test('Adds on to existing postinstall script instead of replacing', () => {
154+
test(`Adds on to existing ${script} script instead of replacing`, () => {
155155
fs.readFileSync.mockImplementation((path, encoding) => {
156156
// Check that utf8 encoding is set
157157
expect(encoding).toBe('utf8')
158-
// Mock existing postinstall script in app's package.json
158+
// Mock existing script in app's package.json
159159
if (path === 'apiResolve_./package.json') {
160-
return JSON.stringify({
161-
scripts: { postinstall: 'existingTask' }
162-
})
160+
return `{"scripts": { "${script}": "existingTask" }}`
163161
}
164162
// return mock content
165163
return 'existing_content'
@@ -168,7 +166,7 @@ describe('package.json', () => {
168166
generator(mockApi)
169167
completionCb()
170168

171-
expect(pkg.scripts.postinstall).toBe(
169+
expect(pkg.scripts[script]).toBe(
172170
'existingTask && electron-builder install-app-deps'
173171
)
174172
})
@@ -177,13 +175,13 @@ describe('package.json', () => {
177175
fs.readFileSync.mockImplementation((path, encoding) => {
178176
// Check that utf8 encoding is set
179177
expect(encoding).toBe('utf8')
180-
// Mock existing postinstall script in app's package.json
178+
// Mock existing script in app's package.json
181179
if (path === 'apiResolve_./package.json') {
182-
return JSON.stringify({
183-
scripts: {
184-
postinstall: 'existingTask && electron-builder install-app-deps'
180+
return `{
181+
"scripts": {
182+
"${script}": "existingTask && electron-builder install-app-deps"
185183
}
186-
})
184+
}`
187185
}
188186
// return mock content
189187
return 'existing_content'
@@ -192,7 +190,7 @@ describe('package.json', () => {
192190
generator(mockApi)
193191
completionCb()
194192

195-
expect(pkg.scripts.postinstall).toBe(
193+
expect(pkg.scripts[script]).toBe(
196194
'existingTask && electron-builder install-app-deps'
197195
)
198196
})

generator/index.js

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,35 +59,38 @@ module.exports = (api, options = {}) => {
5959
}
6060
})
6161

62-
// Add electron-builder install-app-deps to postinstall
63-
let postinstallScript
62+
// Add electron-builder install-app-deps to postinstall and postuninstall
63+
const scripts = {
64+
'electron:build': 'vue-cli-service electron:build',
65+
'electron:serve': 'vue-cli-service electron:serve'
66+
}
6467
let pkg = fs.readFileSync(api.resolve('./package.json'), 'utf8')
6568
pkg = JSON.parse(pkg)
66-
// Add on to existing script if it exists
67-
if (pkg.scripts && pkg.scripts.postinstall) {
68-
// Don't re-add script
69-
if (!pkg.scripts.postinstall.match('electron-builder install-app-deps')) {
70-
postinstallScript =
71-
pkg.scripts.postinstall + ' && electron-builder install-app-deps'
69+
const addScript = (name, command) => {
70+
// Add on to existing script if it exists
71+
if (pkg.scripts && pkg.scripts[name]) {
72+
// Don't re-add script
73+
if (!pkg.scripts[name].match(command)) {
74+
// add command to existing script
75+
scripts[name] = pkg.scripts[name] + ` && ${command}`
76+
} else {
77+
// command already exists, don't change it
78+
scripts[name] = pkg.scripts[name]
79+
}
7280
} else {
73-
// electron-builder install-app-deps already exists
74-
postinstallScript = pkg.scripts.postinstall
81+
// Create new postinstall script
82+
scripts[name] = command
7583
}
76-
} else {
77-
// Create new postinstall script
78-
postinstallScript = 'electron-builder install-app-deps'
7984
}
85+
addScript('postinstall', 'electron-builder install-app-deps')
86+
addScript('postuninstall', 'electron-builder install-app-deps')
8087
const devDependencies = {}
8188
if (options.electronBuilder.electronVersion) {
8289
// Use provided electron version
8390
devDependencies.electron = options.electronBuilder.electronVersion
8491
}
8592
api.extendPackage({
86-
scripts: {
87-
'electron:build': 'vue-cli-service electron:build',
88-
'electron:serve': 'vue-cli-service electron:serve',
89-
postinstall: postinstallScript
90-
},
93+
scripts,
9194
devDependencies,
9295
main: 'background.js'
9396
})

0 commit comments

Comments
 (0)