forked from reZach/secure-electron-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaunchDevServer.js
74 lines (64 loc) · 2.59 KB
/
launchDevServer.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
const fs = require("fs");
const {
exec
} = require("child_process");
const logFilePath = "./dev-scripts/webpack-dev-server.log";
const errorLogFilePath = "./dev-scripts/webpack-dev-server-error.log";
const interval = 100;
const showHint = 600 * 3; // show hint after 3 minutes (60 sec * 3)
let hintCounter = 1;
// Poll webpack-dev-server.log until the webpack bundle has compiled successfully
const intervalId = setInterval(function () {
try {
if (fs.existsSync(logFilePath)) {
const log = fs.readFileSync(logFilePath, {
encoding: "utf8"
});
// "compiled successfully" is the string we need to find
// to know that webpack is done bundling everything and we
// can load our Electron app with no issues. We split up the
// validation because the output contains non-standard characters.
const compiled = log.indexOf("compiled");
if (compiled >= 0 && log.indexOf("successfully", compiled) >= 0) {
console.log("Webpack development server is ready, launching Electron app.");
clearInterval(intervalId);
// Start our electron app
const electronProcess = exec("cross-env NODE_ENV=development electron .");
electronProcess.stdout.on("data", function(data) {
process.stdout.write(data);
});
electronProcess.stderr.on("data", function(data) {
process.stdout.write(data);
});
} else if (log.indexOf("Module build failed") >= 0) {
if (fs.existsSync(errorLogFilePath)) {
const errorLog = fs.readFileSync(errorLogFilePath, {
encoding: "utf8"
});
console.log(errorLog);
console.log(`Webpack failed to compile; this error has also been logged to '${errorLogFilePath}'.`);
clearInterval(intervalId);
return process.exit(1);
} else {
console.log("Webpack failed to compile, but the error is unknown.")
clearInterval(intervalId);
return process.exit(1);
}
} else {
hintCounter++;
// Show hint so user is not waiting/does not know where to
// look for an error if it has been thrown and/or we are stuck
if (hintCounter > showHint){
console.error(`Webpack is likely failing for an unknown reason, please check '${errorLogFilePath}' for more details.`);
clearInterval(intervalId);
return process.exit(1);
}
}
}
} catch (error) {
// Exit with an error code
console.error("Webpack or electron fatal error" + error);
clearInterval(intervalId);
return process.exit(1);
}
}, interval);