From 269f1d6d14fd6e0581ef9e7989ed0a3418da48ae Mon Sep 17 00:00:00 2001 From: Yan Foto Date: Tue, 9 Jul 2019 12:03:07 +0200 Subject: [PATCH] Fix hard reset problem (fixes #57). Hard reset was not working when only applied to the main file. --- main.js | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/main.js b/main.js index 482317c..cf0ed72 100644 --- a/main.js +++ b/main.js @@ -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. @@ -40,23 +40,9 @@ const createHardresetHandler = (eXecutable, hardResetMethod, argv) => } } -/** - * Creates main chokidar watcher for soft resets. - * - * @param {String|Array} 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 @@ -82,6 +68,8 @@ 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) @@ -89,7 +77,7 @@ module.exports = (glob, options = {}) => { watcher.close() } - hardWatcher.on('change', hardResetHandler) + hardWatcher.once('change', hardResetHandler) } else { console.log('Electron could not be found. No hard resets for you!') }