forked from ForumMagnum/ForumMagnum
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.js
executable file
·198 lines (178 loc) · 5.72 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env node
const { build, cliopts } = require("estrella");
const fs = require('fs');
const WebSocket = require('ws');
const fetch = require("node-fetch");
const crypto = require('crypto');
let latestCompletedBuildId = generateBuildId();
let inProgressBuildId = null;
let clientRebuildInProgress = false;
let serverRebuildInProgress = false;
const serverPort = 3000;
const websocketPort = 3001;
const [opts, args] = cliopts.parse(
["production", "Run in production mode"],
["settings", "A JSON config file for the server", "<file>"],
["mongoUrl", "A mongoDB connection connection string", "<url>"],
["mongoUrlFile", "The name of a text file which contains a mongoDB URL for the database", "<file>"],
["shell", "Open an interactive shell instead of running a webserver"],
);
// Two things this script should do, that it currently doesn't:
// * Provide a websocket server for signaling autorefresh
// * Start a local mongodb server, if no mongo URL was provided
// https://github.com/shelfio/jest-mongodb
const isProduction = !!opts.production;
const settingsFile = opts.settings || "settings.json"
if (isProduction) {
process.env.NODE_ENV="production";
} else {
process.env.NODE_ENV="development";
}
if (opts.mongoUrl) {
process.env.MONGO_URL = opts.mongoUrl;
} else if (opts.mongoUrlFile) {
try {
process.env.MONGO_URL = fs.readFileSync(opts.mongoUrlFile, 'utf8').trim();
} catch(e) {
console.log(e);
process.exit(1);
}
}
const clientBundleBanner = `/*
* LessWrong 2.0 (client JS bundle)
* Copyright (c) 2020 the LessWrong development team. See https://github.com/LessWrong2/Lesswrong2
* for source and license details.
*
* Includes CkEditor.
* Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see https://github.com/ckeditor/ckeditor5/blob/master/LICENSE.md
*/`
const bundleDefinitions = {
"process.env.NODE_ENV": isProduction ? "\"production\"" : "\"development\"",
"bundleIsProduction": isProduction,
"bundleIsTest": false,
"defaultSiteAbsoluteUrl": `\"${process.env.ROOT_URL || ""}\"`,
"buildId": `"${latestCompletedBuildId}"`,
};
build({
entryPoints: ['./packages/lesswrong/client/clientStartup.ts'],
bundle: true,
target: "es6",
sourcemap: true,
outfile: "./build/client/js/bundle.js",
minify: isProduction,
banner: clientBundleBanner,
treeShaking: "ignore-annotations",
run: false,
onStart: (config, changedFiles, ctx, esbuildOptions) => {
clientRebuildInProgress = true;
inProgressBuildId = generateBuildId();
esbuildOptions.define.buildId = `"${inProgressBuildId}"`;
},
onEnd: (config, buildResult, ctx) => {
clientRebuildInProgress = false;
if (buildResult?.errors?.length > 0) {
console.log("Skipping browser refresh notification because there were build errors");
} else {
latestCompletedBuildId = inProgressBuildId;
initiateRefresh();
}
inProgressBuildId = null;
},
define: {
...bundleDefinitions,
"bundleIsServer": false,
"global": "window",
},
});
let serverCli = ["node", "-r", "source-map-support/register", "--", "./build/server/js/serverBundle.js", "--settings", settingsFile]
if (opts.shell)
serverCli.push("--shell");
build({
entryPoints: ['./packages/lesswrong/server/serverStartup.ts'],
bundle: true,
outfile: './build/server/js/serverBundle.js',
platform: "node",
sourcemap: true,
minify: false,
run: cliopts.run && serverCli,
onStart: (config, changedFiles, ctx, esbuildOptions) => {
serverRebuildInProgress = true;
},
onEnd: () => {
serverRebuildInProgress = false;
initiateRefresh();
},
define: {
...bundleDefinitions,
"bundleIsServer": true,
},
external: [
"akismet-api", "mongodb", "canvas", "express", "mz", "pg", "pg-promise",
"mathjax", "mathjax-node", "mathjax-node-page", "jsdom", "@sentry/node", "node-fetch", "later", "turndown",
"apollo-server", "apollo-server-express", "graphql",
"bcrypt", "node-pre-gyp", "@lesswrong", "intercom-client",
"fsevents", "chokidar",
],
})
const openWebsocketConnections = [];
async function isServerReady() {
try {
const response = await fetch(`http://localhost:${serverPort}/robots.txt`);
return response.ok;
} catch(e) {
return false;
}
}
async function waitForServerReady() {
while (!(await isServerReady())) {
await asyncSleep(100);
}
}
async function asyncSleep(durationMs) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(), durationMs);
});
}
function generateBuildId() {
return crypto.randomBytes(12).toString('base64');
}
let refreshIsPending = false;
async function initiateRefresh() {
if (!cliopts.watch) {
return;
}
if (refreshIsPending || clientRebuildInProgress || serverRebuildInProgress) {
return;
}
if (openWebsocketConnections.length > 0) {
refreshIsPending = true;
console.log("Initiated refresh; waiting for server to be ready");
await waitForServerReady();
console.log("Notifying connected browser windows to refresh");
for (let connection of openWebsocketConnections) {
connection.send(`{"latestBuildId": "${latestCompletedBuildId}"}`);
}
refreshIsPending = false;
}
}
function startWebsocketServer() {
const server = new WebSocket.Server({
port: websocketPort,
});
server.on('connection', (ws) => {
openWebsocketConnections.push(ws);
ws.on('message', (data) => {
});
ws.on('close', function close() {
const connectionIndex = openWebsocketConnections.indexOf(ws);
if (connectionIndex >= 0) {
openWebsocketConnections.splice(connectionIndex, 1);
}
});
ws.send(`{"latestBuildId": "${latestCompletedBuildId}"}`);
});
}
if (cliopts.watch && cliopts.run && !isProduction) {
startWebsocketServer();
}