Skip to content

Commit

Permalink
Fix hard reset problem (fixes #57).
Browse files Browse the repository at this point in the history
Hard reset was not working when only applied to the main file.
  • Loading branch information
yan-foto committed Jul 9, 2019
1 parent 6ad1a18 commit 269f1d6
Showing 1 changed file with 5 additions and 17 deletions.
22 changes: 5 additions & 17 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const path = require('path')
const appPath = app.getAppPath()
const config = require(path.join(appPath, 'package.json'))
const mainFile = path.join(appPath, config.main || 'index.js')
const ignoredPaths = [mainFile, /node_modules|[/\\]\./]
const ignoredPaths = /node_modules|[/\\]\./

/**
* Creates a callback for hard resets.
Expand Down Expand Up @@ -40,23 +40,9 @@ const createHardresetHandler = (eXecutable, hardResetMethod, argv) =>
}
}

/**
* Creates main chokidar watcher for soft resets.
*
* @param {String|Array<String>} glob path, glob, or array to pass to chokidar
* @param {Object} options chokidar options
*/
const createWatcher = (glob, options = {}) => {
// Watch everything but the node_modules folder and main file
// main file changes are only effective if hard reset is possible
let opts = Object.assign({ ignored: ignoredPaths }, options)
return chokidar.watch(glob, opts)
}

module.exports = (glob, options = {}) => {
let browserWindows = []
let watcher = createWatcher(glob, options)
let hardWatcher = createWatcher(mainFile, options)
let watcher = chokidar.watch(glob, Object.assign({ ignored: [ignoredPaths, mainFile] }, options))

// Callback function to be executed:
// I) soft reset: reload browser windows
Expand All @@ -82,14 +68,16 @@ module.exports = (glob, options = {}) => {
// Preparing hard reset if electron executable is given in options
// A hard reset is only done when the main file has changed
if (eXecutable && fs.existsSync(eXecutable)) {
let hardWatcher = chokidar.watch(mainFile, Object.assign({ ignored: [ignoredPaths] }, options))

if (options.forceHardReset === true) {
// Watch every file for hard reset and not only the main file
hardWatcher.add(glob)
// Stop our default soft reset
watcher.close()
}

hardWatcher.on('change', hardResetHandler)
hardWatcher.once('change', hardResetHandler)
} else {
console.log('Electron could not be found. No hard resets for you!')
}
Expand Down

0 comments on commit 269f1d6

Please sign in to comment.