简体   繁体   中英

Electron's app.relaunch() loose stdio

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?

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:

Rendered process

I use electron-reload listening for changes only within the renderer folder:

require("electron-reload")(path.join(__dirname, "/renderer"));

Main process

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM