-
Notifications
You must be signed in to change notification settings - Fork 768
Expand file tree
/
Copy pathHerebyfile.mjs
More file actions
301 lines (267 loc) · 8.5 KB
/
Copy pathHerebyfile.mjs
File metadata and controls
301 lines (267 loc) · 8.5 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// @ts-check
import esbuild from "esbuild";
import { task } from "hereby";
import { spawn } from "node:child_process";
import { existsSync } from "node:fs";
import { copyFile, readFile, rm, writeFile } from "node:fs/promises";
import { delimiter, isAbsolute, join, resolve } from "node:path";
/**
* @param {string} cmd
* @param {string[]} args
*/
function execa(cmd, args) {
return new Promise((resolve, reject) => {
if (!isAbsolute(cmd)) {
const pathDirs = ["node_modules/.bin"].concat(process.env.PATH?.split(delimiter) || []);
if (process.platform === "win32") {
const extensionsToCheck = (process.env.PATHEXT || [".EXE", ".CMD", ".BAT", ".COM"].join(delimiter))
.split(delimiter).concat([""]);
out: for (const dir of pathDirs) {
for (const ext of extensionsToCheck) {
if (existsSync(join(dir, cmd + ext))) {
cmd = join(dir, cmd + ext);
break out;
}
}
}
} else {
for (const dir of pathDirs) {
if (existsSync(join(dir, cmd))) {
cmd = join(dir, cmd);
break;
}
}
}
}
if (!existsSync(cmd)) {
throw new Error(`Failed to find ${cmd} in PATH or ${cmd} does not exist`);
}
const cp = spawn(cmd, args, {
stdio: "inherit",
// Windows is special. We need to allow shell commands here to run tsc
// as on Windows we get a tsc.cmd file
shell: process.platform === "win32",
});
cp.once("close", code => {
if (code === 0 || code == null) {
resolve(undefined);
} else {
reject(new Error(`${cmd} ${args.join(" ")} exited with code ${code}`));
}
});
cp.once("error", () => {
reject(new Error(`Failed to start ${cmd}`));
});
});
}
/**
* @param {Promise<esbuild.BuildContext>} contextP
* @param {boolean} watch
*/
async function esbuildTask(contextP, watch) {
const context = await contextP;
try {
await context.rebuild();
if (watch) {
await context.watch();
// Wait for Ctrl+C, this might be broken in Git Bash on Windows,
// PR welcome if this affects you!
await new Promise(resolve => process.once("SIGINT", resolve));
}
} finally {
await context.dispose();
}
}
function createJsBuild() {
return esbuild.context({
entryPoints: [
// library split
"src/lib/utils-common.ts",
"src/lib/models.ts",
"src/lib/node-utils.ts",
"src/lib/serialization.ts",
// entry points
"src/lib/browser-utils.ts",
"src/lib/index.ts",
"src/lib/cli.ts",
],
platform: "node",
bundle: true,
outdir: "dist",
logLevel: "info",
format: "esm",
target: "node18",
external: [
"typedoc",
// These imports shouldn't be bundled as they each have their own
// individual bundle which they'll be resolved to.
"#utils",
"#models",
"#serialization",
"#node-utils",
// Don't bundle third parties
"@gerrit0/mini-shiki",
"lunr",
"markdown-it",
"minimatch",
"yaml",
"typescript",
],
});
}
/** @param {boolean} watch */
function createBrowserBundleBuild(watch) {
// It's convenient to be able to build the themes in watch mode without rebuilding the whole docs
// to test some change to the frontend JS.
/** @type {esbuild.Plugin} */
const copyToDocsPlugin = {
name: "copyToDocs",
setup(build) {
if (watch) {
build.onEnd(async (result) => {
if (!result.errors.length) {
try {
await copyFile("static/main.js", "docs/assets/main.js");
} catch {
// Ignore, there isn't a docs folder
}
}
});
}
},
};
return esbuild.context({
entryPoints: ["src/frontend/bootstrap.ts"],
bundle: true,
minify: true,
outfile: "static/main.js",
logLevel: "info",
plugins: watch ? [copyToDocsPlugin] : [],
});
}
export const clean = task({
name: "clean",
run: async () => {
await rm("dist", { recursive: true, force: true });
},
});
export const buildJs = task({
name: "build:js",
run: () => esbuildTask(createJsBuild(), false),
});
export const buildBrowserTranslations = task({
name: "build:browser-translations",
dependencies: [buildJs],
run: () => execa(process.execPath, ["scripts/build_browser_translations.js"]),
});
export const buildBrowser = task({
name: "build:browser",
run: () => esbuildTask(createBrowserBundleBuild(false), false),
});
export const buildTypes = task({
name: "build:types",
run: () => execa("tsc", ["--build"]),
});
export const buildOptionsSchema = task({
name: "build:options-schema",
dependencies: [buildJs],
run: () => execa(process.execPath, ["scripts/generate_options_schema.js", "typedoc-config.schema.json"]),
});
export const build = task({
name: "build",
dependencies: [buildJs, buildBrowser, buildBrowserTranslations, buildTypes, buildOptionsSchema],
});
export const buildProd = task({
name: "build:prod",
dependencies: [build],
run: async () => {
const prodSwitchFile = "dist/types/utils-common/general.d.ts";
const content = await readFile(prodSwitchFile, "utf-8");
await writeFile(prodSwitchFile, content.replace(/type InternalOnly = .*/, "type InternalOnly = 0;"));
},
});
export const watchJs = task({
name: "watch:js",
run: () => esbuildTask(createJsBuild(), true),
});
export const watchBrowser = task({
name: "watch:browser",
run: () => esbuildTask(createBrowserBundleBuild(true), true),
});
export const watchBrowserTranslations = task({
name: "watch:browser-translations",
run: () => execa(process.execPath, ["scripts/build_browser_translations.js", "--watch"]),
});
export const watchTypes = task({
name: "watch:types",
run: () => execa("tsc", ["--build", "--watch", "--preserveWatchOutput"]),
});
export const watch = task({
name: "watch",
dependencies: [watchJs, watchBrowser, watchTypes, watchBrowserTranslations],
});
export const test = task({
name: "test",
run: () =>
execa("tsx", [
"--tsconfig=.config/tsconfig.mocha.json",
"node_modules/mocha/bin/mocha.js",
"--config",
".config/mocha.fast.json",
...process.argv.slice(process.argv.indexOf("test") + 1),
]),
});
export const testCov = task({
name: "test:cov",
run: () =>
execa("c8", [
"-r",
"lcov",
"tsx",
"--tsconfig=.config/tsconfig.mocha.json",
"node_modules/mocha/bin/mocha.js",
"--config",
".config/mocha.fast.json",
...process.argv.slice(process.argv.indexOf("test:cov") + 1),
]),
});
export const testFull = task({
name: "test:full",
run: () =>
execa("c8", [
"-r",
"lcov",
"tsx",
"--tsconfig=.config/tsconfig.mocha.json",
"node_modules/mocha/bin/mocha.js",
"--config",
".config/mocha.full.json",
...process.argv.slice(process.argv.indexOf("test:full") + 1),
]),
});
export const eslint = task({
name: "eslint",
run: () => execa("eslint", [".", "--max-warnings", "0"]),
});
export const dprint = task({
name: "dprint",
run: () => execa("dprint", ["check"]),
});
export const lint = task({
name: "lint",
dependencies: [eslint, dprint],
});
export const format = task({
name: "format",
run: () => execa("dprint", ["fmt"]),
});
export const rebuildSpecs = task({
name: "specs",
run: () => execa("tsx", ["scripts/rebuild_specs.js"]),
});
export const buildSite = task({
name: "build:site",
dependencies: [buildJs, buildBrowser],
run: () => execa("bash", ["scripts/build_site.sh"]),
});
export default build;