Reputation: 2163
Using
chokidar.watch(mainFile).on('change', () => {
app.relaunch();
app.exit(0);
});
I can have my Electron app auto-reload on source change. However since the main process is killed, I loose the terminal output which is necessary for debugging. Is there a way to use app.relaunch()
retaining the stdio
of the parent process?
Upvotes: 3
Views: 1166
Reputation: 2163
Disclaimer: below I explain the setup I found as a decent workaround in 2016. Not sure if there is a better way in 2018.
Reloading of my Electron app is handled in two parts:
I use electron-reload listening for changes only within the renderer
folder:
require("electron-reload")(path.join(__dirname, "/renderer"));
Auto-reload handled "manually" via a Gulp script, as follows:
let child = null;
let start = () => {
// Was already running? Kill it and restart.
if (child) child.kill();
let env = process.env;
env.NODE_ENV = "dev";
child = proc.spawn(electron, ["./main/main.js"], { env: env });
function print(data) {
let str = data.toString().trim();
if (str) gutil.log(str);
}
child.stdout.on("data", print);
child.stderr.on("data", print);
};
// Run (dev mode with watch and autoreload)
gulp.task("dev", ["watch"], () => {
start();
});
// Watch and move to dist on the fly.
// Hard reset if main process source changes.
gulp.task("watch", () => {
// If ./renderer/**/*.js changes, electron-reload will handle reload of renderer process only.
// If ./main/**/*.js changes, we kill the Electron process and start a new one.
gulp.watch("./main/**/*.js", start);
});
Running the app with gulp run dev
will start a watcher and kill/restart the entire application on file change.
Upvotes: 0