Skip to content

Instantly share code, notes, and snippets.

@Lokua
Created January 3, 2019 10:15
Show Gist options
  • Save Lokua/b88ffc4aa32398abe3cf8688444799ac to your computer and use it in GitHub Desktop.
Save Lokua/b88ffc4aa32398abe3cf8688444799ac to your computer and use it in GitHub Desktop.
starter script to restart electron on main process file change
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