Skip to content

Instantly share code, notes, and snippets.

@patbonecrusher
Created August 23, 2016 15:41
Show Gist options
  • Save patbonecrusher/4f8677992a5f1975aebcfff1944fea89 to your computer and use it in GitHub Desktop.
Save patbonecrusher/4f8677992a5f1975aebcfff1944fea89 to your computer and use it in GitHub Desktop.
how to auto restart electron browser process upon file changes
#!/usr/bin/env node
require('babel-register');
const electron = require('electron-prebuilt');
const proc = require('child_process');
const sleep = require('sleep');
const RxNode = require('rx-node');
const Rx = require('rx');
const fs = require('fs');
const chokidar = require('chokidar');
const readline = require('readline');
const {curry, append, remove, compose, replace, prop, map} = require('ramda');
var log = console.log.bind(console);
var fileWatcher = chokidar.watch('./src/browser', { ignored: /[\/\\]\./, persistent: true });
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
var restart = compose(start, stop, sleep.sleep);
var closeShop = compose(terminateApp, stop);
fileWatcher.on('raw', function(event, path, details) { restart(1); });
RxNode.fromReadLineStream(rl).subscribe((cmd) => { if (cmd === 'rs') {restart(0);} });
rl.on('SIGINT', (cmd) => closeShop());
var child;
function stop() {
console.log('Stopping app');
child.removeListener('exit', closeShop);
child.stdin.pause();
child.kill();
}
function start() {
console.log('');
console.log('v'.repeat(80));
console.log('Starting Electron App @ ' + proc.execSync('pwd',{encoding:'utf-8'}));
child = proc.spawn(electron,['.']);
child.stdout.pipe(process.stdout);
child.on('exit', closeShop);
}
function terminateApp() {
process.exit();
}
start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment