This repository has been archived by the owner on Mar 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
electron-watch.js
57 lines (50 loc) · 1.65 KB
/
electron-watch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
var chokidar = require("chokidar");
var electronConnect = require("electron-connect");
var once = require("lodash.once");
var webpack = require("webpack");
var webpackConfig = require("./webpack.config.js");
webpackConfig.entry.push("./src/browser/dev-mode.js");
webpackConfig.devtool = "source-map";
var compiler = webpack(webpackConfig);
var webpackWatcher = compiler.watch({}, function(err, stats){
if (err){
console.error("webpack error:", err);
} else if (stats.compilation.errors && stats.compilation.errors.length){
console.error("webpack errors:")
stats.compilation.errors.forEach(function(error){
console.error(error.message);
});
}else {
startAppOnFirstRun();
var elapsed = stats.endTime - stats.startTime;
console.info("\n\nwebpack success:", elapsed + "ms");
}
});
var startApp = function(){
var electron = electronConnect.server.create();
electron.start("--dev-mode");
var bundleWatcher = chokidar.watch("./browser-build/bundle.js");
bundleWatcher.on("change", function(){
console.info("Reloading renderer.")
electron.reload();
});
var mainProcessWatcher = chokidar.watch("./src/", {
depth: 0
});
mainProcessWatcher.on("change", function(){
console.info("Reloading main process.");
electron.restart("--dev-mode");
});
electron.on("exit", function(){
console.info("Stopping main process.");
electron.stop();
console.info("Stopping watchers.");
bundleWatcher.close();
mainProcessWatcher.close();
webpackWatcher.close(function(){
console.info("Watchers closed. Exiting.");
process.exit(0);
});
});
};
var startAppOnFirstRun = once(startApp);