Skip to content

Commit

Permalink
fix: set createHardresetHandler default args
Browse files Browse the repository at this point in the history
fixes: yan-foto#65
&
fixes: yan-foto#61
  • Loading branch information
josheleonard committed Apr 28, 2020
1 parent 9a5989a commit 22d4bf2
Showing 1 changed file with 37 additions and 37 deletions.
74 changes: 37 additions & 37 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const { app } = require('electron')
const chokidar = require('chokidar')
const fs = require('fs')
const { spawn } = require('child_process')
const { app } = require("electron");
const chokidar = require("chokidar");
const fs = require("fs");
const { spawn } = require("child_process");

const appPath = app.getAppPath()
const ignoredPaths = /node_modules|[/\\]\./
const appPath = app.getAppPath();
const ignoredPaths = /node_modules|[/\\]\./;

/**
* Creates a callback for hard resets.
Expand All @@ -17,23 +17,23 @@ const ignoredPaths = /node_modules|[/\\]\./
const createHardresetHandler = (eXecutable, hardResetMethod, argv) => () => {
// Detaching child is useful when in Windows to let child
// live after the parent is killed
const args = [appPath].concat(argv)
const args = [appPath].concat(argv || []);
const child = spawn(eXecutable, args, {
detached: true,
stdio: 'inherit'
})
child.unref()
stdio: "inherit",
});
child.unref();
// Kamikaze!

// In cases where an app overrides the default closing or quiting actions
// firing an `app.quit()` may not actually quit the app. In these cases
// you can use `app.exit()` to gracefully close the app.
if (hardResetMethod === 'exit') {
app.exit()
if (hardResetMethod === "exit") {
app.exit();
} else {
app.quit()
app.quit();
}
}
};

/**
* @param {string | string[]} glob
Expand All @@ -51,7 +51,7 @@ module.exports = (
cwd: undefined,
depth: undefined,
disableGlobbing: undefined,
electron: '',
electron: "",
followSymlinks: undefined,
forceHardReset: undefined,
hardResetMethod: undefined,
Expand All @@ -62,66 +62,66 @@ module.exports = (
mainFile: module.parent.filename,
persistent: undefined,
useFsEvents: undefined,
usePolling: undefined
usePolling: undefined,
}
) => {
// Main file poses a special case, as its changes are
// only effective when the process is restarted (hard reset)
// We assume that electron-reload is required by the main
// file of the electron application if a path is not provided
const mainFile = options.mainFile
const browserWindows = []
const mainFile = options.mainFile;
const browserWindows = [];
const watcher = chokidar.watch(
glob,
Object.assign({ ignored: [ignoredPaths, mainFile] }, options)
)
);

// Callback function to be executed:
// I) soft reset: reload browser windows
const softResetHandler = () =>
browserWindows.forEach((bw) => bw.webContents.reloadIgnoringCache())
browserWindows.forEach((bw) => bw.webContents.reloadIgnoringCache());
// II) hard reset: restart the whole electron process
const eXecutable = options.electron
const eXecutable = options.electron;
const hardResetHandler = createHardresetHandler(
eXecutable,
options.hardResetMethod,
options.argv
)
);

// Add each created BrowserWindow to list of maintained items
app.on('browser-window-created', (_e, bw) => {
browserWindows.push(bw)
app.on("browser-window-created", (_e, bw) => {
browserWindows.push(bw);

// Remove closed windows from list of maintained items
bw.on('closed', function () {
const i = browserWindows.indexOf(bw) // Must use current index
browserWindows.splice(i, 1)
})
})
bw.on("closed", function () {
const i = browserWindows.indexOf(bw); // Must use current index
browserWindows.splice(i, 1);
});
});

// Enable default soft reset
watcher.on('change', softResetHandler)
watcher.on("change", softResetHandler);

// Preparing hard reset if electron executable is given in options
// A hard reset is only done when the main file has changed
if (typeof eXecutable === 'string' && fs.existsSync(eXecutable)) {
if (typeof eXecutable === "string" && fs.existsSync(eXecutable)) {
const 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)
hardWatcher.add(glob);
// Stop our default soft reset
watcher.close()
watcher.close();
}

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

/**
* @typedef Options
Expand Down

0 comments on commit 22d4bf2

Please sign in to comment.