-
Notifications
You must be signed in to change notification settings - Fork 8
/
HashReader.mts
90 lines (81 loc) · 2.84 KB
/
HashReader.mts
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
// # `HashReader.mts` -- post-process esbuild output
//
// This script reads the output produced by esbuild to determine the location of
// the bundled files, which have hashes in their file names. It writes these
// results to a simple JSON file, which the CodeChat Editor Server reads.
import fs from "node:fs/promises";
// Copied from the [esbuild docs](https://esbuild.github.io/api/#metafile).
interface Metafile {
inputs: {
[path: string]: {
bytes: number;
imports: {
path: string;
kind: string;
external?: boolean;
original?: string;
with?: Record<string, string>;
}[];
format?: string;
with?: Record<string, string>;
};
};
outputs: {
[path: string]: {
bytes: number;
inputs: {
[path: string]: {
bytesInOutput: number;
};
};
imports: {
path: string;
kind: string;
external?: boolean;
}[];
exports: string[];
entryPoint?: string;
cssBundle?: string;
};
};
}
// Load the esbuild metafiles.
const data_client_framework = await fs.readFile("metaClientFramework.json", {
encoding: "utf8",
});
const data_client = await fs.readFile("metaClient.json", { encoding: "utf8" });
// Interpret it as JSON.
const metafile_client_framework: Metafile = JSON.parse(data_client_framework);
const metafile_client: Metafile = JSON.parse(data_client);
// Walk the file, looking for the names of specific entry points. Transform
// those into paths used to import these files.
let outputContents: Record<string, string> = {};
let num_found = 0;
for (const metafile of [metafile_client_framework, metafile_client]) {
for (const output in metafile.outputs) {
const outputInfo = metafile.outputs[output];
switch (outputInfo.entryPoint) {
case "src/CodeChatEditorFramework.mts":
outputContents["CodeChatEditorFramework.js"] = output;
++num_found;
break;
case "src/CodeChatEditor.mts":
outputContents["CodeChatEditor.js"] = output;
outputContents["CodeChatEditor.css"] = outputInfo.cssBundle!;
++num_found;
break;
case "src/CodeChatEditor-test.mts":
outputContents["CodeChatEditor-test.js"] = output;
outputContents["CodeChatEditor-test.css"] =
outputInfo.cssBundle!;
++num_found;
}
}
}
console.assert(num_found === 3);
// Write this to disk.
await fs.writeFile(
"../server/hashLocations.json",
JSON.stringify(outputContents),
);
console.log("Wrote hashLocations.json");