-
Notifications
You must be signed in to change notification settings - Fork 116
/
build.js
executable file
·193 lines (174 loc) · 5.88 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
#!/usr/bin/env node
const { build, cliopts } = require("estrella");
const fs = require('fs');
const process = require('process');
const { getDatabaseConfig, startSshTunnel, getOutputDir, setOutputDir } = require("./scripts/startup/buildUtil");
const { setClientRebuildInProgress, setServerRebuildInProgress, generateBuildId, startAutoRefreshServer, initiateRefresh, startLint } = require("./scripts/startup/autoRefreshServer");
/**
* This is used for clean exiting in Github workflows by the dev
* only route /api/quit
*/
process.on("SIGQUIT", () => process.exit(0));
const [opts, args] = cliopts.parse(
["production", "Run in production mode"],
["e2e", "Run in end-to-end testing mode"],
["settings", "A JSON config file for the server", "<file>"],
["db", "A path to a database connection config file", "<file>"],
["postgresUrl", "A postgresql connection connection string", "<url>"],
["postgresUrlFile", "The name of a text file which contains a postgresql URL for the database", "<file>"],
["shell", "Open an interactive shell instead of running a webserver"],
["command", "Run the given server shell command, then exit", "<string>"],
["lint", "Run the linter on site refresh"],
);
const defaultServerPort = 3000;
const getServerPort = () => {
if (opts.command) {
return 5001;
}
const port = parseInt(process.env.PORT ?? "");
return Number.isNaN(port) ? defaultServerPort : port;
}
let latestCompletedBuildId = generateBuildId();
let inProgressBuildId = null;
const serverPort = getServerPort();
const websocketPort = serverPort + 1;
setOutputDir(`./build${serverPort === defaultServerPort ? "" : serverPort}`);
// Two things this script should do, that it currently doesn't:
// * Provide a websocket server for signaling autorefresh
const isProduction = !!opts.production;
const isE2E = !!opts.e2e;
const settingsFile = opts.settings || "settings.json"
const databaseConfig = getDatabaseConfig(opts);
process.env.PG_URL = databaseConfig.postgresUrl;
if (databaseConfig.sshTunnelCommand) {
startSshTunnel(databaseConfig.sshTunnelCommand);
}
if (isProduction) {
process.env.NODE_ENV="production";
} else {
process.env.NODE_ENV="development";
}
const clientBundleBanner = `/*
* LessWrong 2.0 (client JS bundle)
* Copyright (c) 2022 the LessWrong development team. See https://github.com/ForumMagnum/ForumMagnum
* for source and license details.
*
* Includes CkEditor.
* Copyright (c) 2003-2022, 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,
"bundleIsE2E": isE2E,
"bundleIsMigrations": false,
"defaultSiteAbsoluteUrl": `\"${process.env.ROOT_URL || ""}\"`,
"buildId": `"${latestCompletedBuildId}"`,
"serverPort": getServerPort(),
"ddEnv": `\"${process.env.DD_ENV || "local"}\"`,
};
const clientBundleDefinitions = {
"bundleIsServer": false,
"global": "window",
}
const serverBundleDefinitions = {
"bundleIsServer": true,
"estrellaPid": process.pid,
}
const clientOutfilePath = `${getOutputDir()}/client/js/bundle.js`;
build({
entryPoints: ['./packages/lesswrong/client/clientStartup.ts'],
bundle: true,
target: "es6",
sourcemap: true,
metafile: true,
sourcesContent: true,
outfile: clientOutfilePath,
minify: isProduction,
banner: {
js: clientBundleBanner,
},
run: false,
onStart: (config, changedFiles, ctx) => {
setClientRebuildInProgress(true);
inProgressBuildId = generateBuildId();
config.define.buildId = `"${inProgressBuildId}"`;
if (opts.lint) {
startLint();
}
},
onEnd: (config, buildResult, ctx) => {
setClientRebuildInProgress(false);
if (buildResult?.errors?.length > 0) {
console.log("Skipping browser refresh notification because there were build errors");
} else {
latestCompletedBuildId = inProgressBuildId;
if (cliopts.watch) {
initiateRefresh({serverPort});
}
if (buildResult.metafile) {
fs.writeFile(
"client_meta.json",
JSON.stringify(buildResult.metafile, null, 2),
() => {},
);
}
}
inProgressBuildId = null;
},
define: {
...bundleDefinitions,
...clientBundleDefinitions,
},
external: [
"cheerio",
],
});
let serverCli = ["node", "-r", "source-map-support/register", "--", `${getOutputDir()}/server/js/serverBundle.js`, "--settings", settingsFile]
if (opts.shell)
serverCli.push("--shell");
if (opts.command) {
serverCli.push("--command");
serverCli.push(opts.command);
}
if (!isProduction)
serverCli.splice(1, 0, "--inspect");
build({
entryPoints: ['./packages/lesswrong/server/runServer.ts'],
bundle: true,
outfile: `${getOutputDir()}/server/js/serverBundle.js`,
platform: "node",
sourcemap: true,
sourcesContent: true,
minify: false,
run: cliopts.run && serverCli,
onStart: (config, changedFiles, ctx) => {
setServerRebuildInProgress(true);
if (opts.lint) {
startLint();
}
},
onEnd: () => {
setServerRebuildInProgress(false);
if (cliopts.watch) {
initiateRefresh({serverPort});
}
},
define: {
...bundleDefinitions,
...serverBundleDefinitions,
},
external: [
"akismet-api", "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", "csso", "io-ts", "fp-ts",
"bcrypt", "node-pre-gyp", "intercom-client", "node:*",
"fsevents", "chokidar", "auth0", "dd-trace", "pg-formatter",
"gpt-3-encoder", "@elastic/elasticsearch", "zod", "node-abort-controller",
"cheerio",
],
})
if (cliopts.watch && cliopts.run && !isProduction && !process.env.CI) {
startAutoRefreshServer({serverPort, websocketPort});
}