Skip to content

Commit

Permalink
feat(app): Watch for changes on electron-preload and restart Electron…
Browse files Browse the repository at this point in the history
… process automatically
  • Loading branch information
rstoenescu committed Jan 14, 2020
1 parent f03287c commit 04af109
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions app/lib/electron/index.js
@@ -1,4 +1,7 @@
const webpack = require('webpack')
const chokidar = require('chokidar')
const fs = require('fs')
const debounce = require('lodash.debounce')

const logger = require('../helpers/logger')
const log = logger('app:electron')
Expand Down Expand Up @@ -35,6 +38,7 @@ class ElectronRunner {

return new Promise(resolve => {
log(`Building main Electron process...`)

this.watcher = compiler.watch({}, async (err, stats) => {
if (err) {
console.log(err)
Expand Down Expand Up @@ -62,6 +66,24 @@ class ElectronRunner {

resolve()
})

const preloadFile = appPaths.resolve.electron('main-process/electron-preload.js')

if (fs.existsSync(preloadFile)) {
// Start watching for electron-preload.js changes
this.preloadWatcher = chokidar
.watch(preloadFile, { watchers: { chokidar: { ignoreInitial: true } } })

this.preloadWatcher.on('change', debounce(async () => {
console.log()
log(`electron-preload.js changed`)

await this.__stopElectron()
this.__startElectron(argv._)

resolve()
}, 1000))
}
})
}

Expand Down Expand Up @@ -138,17 +160,29 @@ class ElectronRunner {

stop () {
return new Promise(resolve => {
let counter = 0
const maxCounter = (this.watcher ? 1 : 0) + (this.preloadWatcher ? 1 : 0)

const finalize = () => {
this.__stopElectron().then(resolve)
counter++
if (maxCounter <= counter) {
this.__stopElectron().then(resolve)
}
}

if (this.watcher) {
this.watcher.close(finalize)
this.watcher = null
return
}

finalize()
if (this.preloadWatcher) {
this.preloadWatcher.close().then(finalize)
this.preloadWatcher = null
}

if (maxCounter === 0) {
finalize()
}
})
}

Expand Down

0 comments on commit 04af109

Please sign in to comment.