Created
January 3, 2019 10:15
-
-
Save Lokua/b88ffc4aa32398abe3cf8688444799ac to your computer and use it in GitHub Desktop.
starter script to restart electron on main process file change
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { spawn } = require('child_process') | |
const chokidar = require('chokidar') | |
const chalk = require('chalk') | |
const paths = ['./src/main.js'] | |
let child | |
let wait = false | |
spawnElectron() | |
chokidar.watch(paths).on('change', path => { | |
if (!wait) { | |
log(`${path} changed. restarting`) | |
child.kill() | |
wait = setTimeout(() => { | |
spawnElectron() | |
wait = null | |
}, 200) | |
} | |
}) | |
function spawnElectron() { | |
const args = ['electron', '.', ...process.argv.slice(2)] | |
log() | |
child = spawn('npx', args, { | |
cwd: __dirname, | |
stdio: 'inherit', | |
}) | |
} | |
function log(...args) { | |
console.info(chalk.magenta('[dev.js]'), ...args) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment