-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathHashReader.mts
More file actions
107 lines (96 loc) · 3.49 KB
/
HashReader.mts
File metadata and controls
107 lines (96 loc) · 3.49 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
// Copyright (C) 2025 Bryan A. Jones.
//
// This file is part of the CodeChat Editor. The CodeChat Editor is free
// software: you can redistribute it and/or modify it under the terms of the GNU
// General Public License as published by the Free Software Foundation, either
// version 3 of the License, or (at your option) any later version.
//
// The CodeChat Editor is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// the CodeChat Editor. If not, see
// [http://www.gnu.org/licenses](http://www.gnu.org/licenses).
//
// `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 metafile.
const data = await fs.readFile("meta.json", { encoding: "utf8" });
// Interpret it as JSON.
const metafile: Metafile = JSON.parse(data);
// Walk the file, looking for the names of specific entry points. Transform
// those into paths used to import these files.
const outputContents: Record<string, string> = {};
let num_found = 0;
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;
break;
case "src/css/CodeChatEditorProject.css":
outputContents["CodeChatEditorProject.css"] = output;
++num_found;
break;
}
}
console.assert(num_found === 4);
// Write this to disk.
await fs.writeFile(
"../server/hashLocations.json",
JSON.stringify(outputContents),
);
console.log("Wrote hashLocations.json");