forked from frankhale/electron-with-express
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
137 lines (116 loc) · 3.55 KB
/
index.html
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Electron-With-Express</title>
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=VT323" />
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-T8Gy5hrqNKT+hzMclPo118YTQO6cYprQmhrYwIiQ/3axmI1hQomh7Ud2hPOy8SP1"
crossorigin="anonymous">
<style>
body {
overflow: hidden;
margin: 0;
padding: 0;
}
* {
font-family: 'VT323', sans-serif;
}
#loading {
position: absolute;
width: 100%;
top: 45%;
text-align: center;
}
#serverLog {
position: absolute;
width: 100%;
height: 100%;
display: none;
overflow: auto;
}
.expressApp {
display: flex !important;
position: absolute;
width: 100%;
height: 100%;
}
.expressAppHide {
flex: 0 1;
width: 0px;
height: 0px;
}
</style>
</head>
<body>
<div id="loading">
<i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i>
</div>
<div id="serverLog"></div>
<webview id="expressApp" class="expressApp"></webview>
<script>
window.$ = window.jQuery = require("./node_modules/jquery/dist/jquery.min.js");
const {
ipcRenderer
} = require('electron'),
expressAppUrl = "http://127.0.0.1:3000",
spawn = require("child_process").spawn,
// For electron-packager change cwd in spawn to app.getAppPath() and
// uncomment the app require below
//app = require('electron').remote.app,
node = spawn(".\\node.exe", ["./express-app/bin/www"], {
cwd: process.cwd()
}),
request = require("request"),
_ = require("lodash"),
key = require("keymaster"),
$serverLog = $("#serverLog"),
$expressApp = $("#expressApp"),
$loading = $("#loading");
ipcRenderer.on('stop-server', (event, data) => {
// This is okay for now but there is a better solution. We can use IPC to
// tell the server (the Express app itself) to gracefully shutdown. This
// would be much better especially if we had database connections or other
// resources we were using that needed to be cleaned up.
node.kill('SIGINT');
});
key("f1", () => {
if ($serverLog.css("display") === "none") {
$serverLog.css("display", "block");
$expressApp.addClass("expressAppHide");
} else {
$expressApp.removeClass("expressAppHide");
$serverLog.css("display", "none");
}
});
function strip(s) {
// regex from: http://stackoverflow.com/a/29497680/170217
return s.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
}
function redirectOutput(x) {
let lineBuffer = "";
x.on('data', function (data) {
lineBuffer += data.toString();
let lines = lineBuffer.split("\n");
_.forEach(lines, (l) => {
if (l !== "") {
$serverLog.append(strip(l) + "<br/>");
}
});
lineBuffer = lines[lines.length - 1];
});
}
redirectOutput(node.stdout);
redirectOutput(node.stderr);
let checkServerRunning = setInterval(() => {
request(expressAppUrl, (error, response, body) => {
if (!error && response.statusCode == 200) {
$expressApp.attr("src", expressAppUrl);
$loading.css("display", "none");
$expressApp.css("display", "block");
clearInterval(checkServerRunning);
}
});
}, 1000);
</script>
</body>
</html>