Skip to content

Commit dab5c2a

Browse files
committed
feat(serve): wait for electron exit before restart, fixes #641
1 parent 9061427 commit dab5c2a

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

index.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,15 @@ module.exports = (api, options) => {
319319
let child
320320
let firstBundleCompleted = false
321321
// Function to kill Electron process
322-
const killElectron = () => {
323-
if (!child) {
324-
return
322+
const killElectron = () => new Promise(resolve => {
323+
if (!child || child.killed) {
324+
return resolve()
325325
}
326326

327327
const currentChild = child
328+
currentChild.on('exit', () => {
329+
resolve()
330+
})
328331

329332
// Attempt to kill gracefully
330333
if (process.platform === 'win32') {
@@ -340,7 +343,7 @@ module.exports = (api, options) => {
340343
currentChild.kill('SIGKILL')
341344
}
342345
}, 2000)
343-
}
346+
})
344347

345348
// Initial start of Electron
346349
startElectron()
@@ -379,14 +382,19 @@ module.exports = (api, options) => {
379382
})
380383
}
381384

382-
function launchElectron () {
385+
async function launchElectron () {
383386
firstBundleCompleted = true
384387
// Don't exit process when electron is killed
385388
if (child) {
386389
child.removeListener('exit', onChildExit)
387390
}
388391
// Kill existing instances
389-
killElectron()
392+
let waitTimeout = setTimeout(() => {
393+
// If killing Electron takes over 500ms:
394+
info('Waiting for Electron to exit...')
395+
}, 500)
396+
await killElectron()
397+
clearTimeout(waitTimeout)
390398
// Don't launch if a new background file is being bundled
391399
queuedBuilds--
392400
if (queuedBuilds > 0) return

0 commit comments

Comments
 (0)