-
Notifications
You must be signed in to change notification settings - Fork 81
/
build.js
92 lines (82 loc) · 2.3 KB
/
build.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const fs = require("fs");
const url = require("url");
const path = require("path");
const buble = require("rollup-plugin-buble");
const rollup = require("rollup");
const electron = require("electron");
const bundleSource = () => {
return new Promise((resolve, reject) => {
rollup.rollup({
entry: __dirname + "/src/index.js",
plugins: [ buble() ],
}).then((bundle) => {
const result = bundle.generate({
format: "cjs"
});
const code = "(function() { " + result.code + "})();";
fs.writeFileSync("static/bundle.js", code);
resolve();
}).catch((e) => {
reject(e);
});
});
};
// now open up electron with our fresh rolluped bundle
const initElectron = () => {
return new Promise((resolve) => {
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
let win = null;
const createWindow = () => {
win = new BrowserWindow({
width: 980,
height: 680,
titleBarStyle: "hidden",
icon: path.join(__dirname, "/static/assets/img/tree.png")
});
win.loadURL(url.format({
pathname: path.join(__dirname, "/static/index.html"),
protocol: "file:",
slashes: true
}));
win.setMenu(null);
//win.setFullScreen(true);
win.webContents.openDevTools();
win.on("closed", () => {
win = null;
});
resolve({ win, app });
};
app.on("ready", createWindow);
app.on("window-all-closed", () => {
if (process.platform !== "darwin") app.quit();
});
app.on("activate", () => {
if (win === null) createWindow();
});
});
};
const initializeStage = () => {
return new Promise((resolve) => {
bundleSource().then(() => {
initElectron().then(resolve);
}).catch((e) => {
throw new Error(e);
});
});
};
// simple live reload system
initializeStage().then((old) => {
const onRefresh = (e, file) => {
// prevent circular tracking
if (file === "bundle.js") return;
bundleSource().then(() => {
old.win.reload();
old.win.webContents.reloadIgnoringCache();
old.win.webContents.openDevTools();
console.log("Refreshed!", "#" + Date.now());
});
};
fs.watch("./src/", {recursive: true}, onRefresh);
fs.watch("./static/", {recursive: true}, onRefresh);
});