-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.ts
More file actions
268 lines (213 loc) · 8.01 KB
/
Copy pathbuild.ts
File metadata and controls
268 lines (213 loc) · 8.01 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
import type { Diagnostic } from "typescript";
import * as tstl from "typescript-to-lua";
import { compile } from "@gwigz/jsx-inline";
import { watch, readFileSync, writeFileSync, unlinkSync, readdirSync, renameSync } from "node:fs";
import { execSync } from "node:child_process";
import { resolve, basename } from "node:path";
import * as constants from "./src/constants";
const WATCH = process.argv.includes("--watch");
const GENERATED_HEADER = "--[[ Generated with @gwigz/slua - https://github.com/gwigz/slua ]]";
/** All .tsx source files in src/patcher/ that compile to .ts at build time. */
const TSX_SOURCES = readdirSync(resolve("src/patcher"))
.filter((f) => f.endsWith(".tsx"))
.map((f) => resolve("src/patcher", f));
/** Generated .ts files that should not trigger rebuilds in watch mode. */
const GENERATED_FILES = TSX_SOURCES.map((f) => "patcher/" + basename(f, ".tsx") + ".ts");
/** Extracts the leading JSDoc block from a source file and converts it to a Lua multiline comment. */
function extractFileComment(sourcePath: string) {
const source = readFileSync(resolve(sourcePath), "utf8");
const match = source.match(/^\/\*\*\n([\s\S]*?)\s*\*\//);
if (!match) {
return "";
}
const body = match[1]
.split("\n")
.map((line) => line.replace(/^\s*\* ?/, ""))
.join("\n")
.trim();
return `--[[\n${body}\n]]`;
}
/** Extracts JSDoc comments paired with their export names from constants.ts. */
function getConstantComments(): Record<string, string> {
const source = readFileSync(resolve("src/constants.ts"), "utf8");
const comments: Record<string, string> = {};
const re = /\/\*\*\s*(.*?)\s*\*\/\s*\nexport const (\w+)/g;
let match;
while ((match = re.exec(source))) {
comments[match[2]] = match[1];
}
return comments;
}
/** Prepends file header comment and constants to a compiled .slua file. */
function injectConstants(filePath: string, content: string, sourcePath: string, comments: Record<string, string>) {
const header = extractFileComment(sourcePath);
const lines = Object.entries(constants)
.filter(([name]) => new RegExp(`\\b${name}\\b`).test(content))
.map(([name, value]) => {
const comment = comments[name];
const luaValue = typeof value === "string" ? JSON.stringify(value) : String(value);
return comment ? `--- ${comment}\nlocal ${name} = ${luaValue}` : `local ${name} = ${luaValue}`;
});
const parts: string[] = [];
if (header) {
parts.push(header);
}
if (lines.length > 0) {
parts.push(lines.join("\n\n"));
}
parts.push(GENERATED_HEADER);
writeFileSync(filePath, parts.join("\n\n") + "\n" + content);
}
/** Generates src/constants.d.ts from the exports in src/constants.ts. */
function generateConstantDeclarations(comments: Record<string, string>) {
const declarations = Object.entries(constants)
.map(([name, value]) => {
const comment = comments[name];
const decl = `declare const ${name}: ${typeof value};`;
return comment ? `/** ${comment} */\n${decl}` : decl;
})
.join("\n\n");
writeFileSync(
resolve("src/types/globals.d.ts"),
`// Auto-generated from constants.ts -- run \`bun dev\` to update\n\n${declarations}\n`,
);
}
function reportDiagnostics(diagnostics: readonly Diagnostic[]) {
let hasErrors = false;
for (const diagnostic of diagnostics) {
// TSTL warns about luaBundle + inline but it's harmless
if (String(diagnostic.messageText).includes("luaBundle")) {
continue;
}
const msg = String(diagnostic.messageText);
if (diagnostic.category === 0) {
console.error("error:", msg);
hasErrors = true;
} else if (diagnostic.category === 1) {
console.warn("warning:", msg);
}
}
return hasErrors;
}
async function build() {
let hasErrors = false;
// Step 1: Generate constant declarations + compile JSX templates
const comments = getConstantComments();
generateConstantDeclarations(comments);
await compile(TSX_SOURCES);
// Step 2: Patcher bundle, and export elimination
const patcherResult = tstl.transpileProject("tsconfig.patcher.json", {
noHeader: true,
luaBundle: "patcher.slua",
luaBundleEntry: resolve("src/patcher/index.ts"),
});
if (reportDiagnostics(patcherResult.diagnostics)) {
hasErrors = true;
}
const patcherPath = resolve("dist/patcher.slua");
// Bootstrap standalone
const bootstrapResult = tstl.transpileFiles(
[resolve("src/types/globals.d.ts"), resolve("src/patcher-bootstrap.ts")],
{
rootDir: resolve("src"),
outDir: resolve("dist"),
target: 99, // ESNext
module: 99, // ESNext
strict: true,
moduleDetection: 3, // Force
skipLibCheck: true,
types: ["@typescript-to-lua/language-extensions", "@gwigz/slua-types"],
luaTarget: tstl.LuaTarget.Luau,
luaLibImport: tstl.LuaLibImportKind.Inline,
extension: "slua",
noHeader: true,
noImplicitSelf: true,
luaPlugins: [{ name: "@gwigz/slua-tstl-plugin", optimize: true }, { name: "@gwigz/tstl-bundle-flatten" }],
} as tstl.CompilerOptions,
);
if (reportDiagnostics(bootstrapResult.diagnostics)) {
hasErrors = true;
}
const bootstrapPath = resolve("dist/patcher-bootstrap.slua");
// Worker standalone
const workerResult = tstl.transpileFiles([resolve("src/types/globals.d.ts"), resolve("src/worker.ts")], {
rootDir: resolve("src"),
outDir: resolve("dist"),
target: 99, // ESNext
module: 99, // ESNext
strict: true,
moduleDetection: 3, // Force
skipLibCheck: true,
types: ["@typescript-to-lua/language-extensions", "@gwigz/slua-types"],
luaTarget: tstl.LuaTarget.Luau,
luaLibImport: tstl.LuaLibImportKind.Inline,
extension: "slua",
noHeader: true,
noImplicitSelf: true,
luaPlugins: [{ name: "@gwigz/slua-tstl-plugin", optimize: true }, { name: "@gwigz/tstl-bundle-flatten" }],
} as tstl.CompilerOptions);
if (reportDiagnostics(workerResult.diagnostics)) {
hasErrors = true;
}
// Rename worker output to match expected inventory name
renameSync(resolve("dist/worker.slua"), resolve("dist/patcher-worker.slua"));
const workerPath = resolve("dist/patcher-worker.slua");
if (hasErrors) {
return false;
}
// Step 3: Inject constants at top of all .slua files
injectConstants(patcherPath, readFileSync(patcherPath, "utf8"), "src/patcher/index.ts", comments);
injectConstants(bootstrapPath, readFileSync(bootstrapPath, "utf8"), "src/patcher-bootstrap.ts", comments);
injectConstants(workerPath, readFileSync(workerPath, "utf8"), "src/worker.ts", comments);
// Step 4: Format .slua output with StyLua
try {
execSync("npx stylua --verify -- dist/patcher.slua dist/patcher-bootstrap.slua dist/patcher-worker.slua");
} catch (e: unknown) {
console.warn("warning: stylua formatting failed");
if (e instanceof Error && "stderr" in e) console.warn(String(e.stderr));
}
// Step 5: Clean up generated .ts files so the editor resolves to .tsx sources
for (const tsxPath of TSX_SOURCES) {
unlinkSync(tsxPath.replace(/\.tsx$/, ".ts"));
}
console.log("Built dist/patcher.slua + dist/patcher-bootstrap.slua + dist/patcher-worker.slua");
return true;
}
await build();
if (WATCH) {
console.log("Watching src/ for changes...");
let debounce: Timer | null = null;
let building = false;
let pending = false;
watch(resolve("src"), { recursive: true }, (_event: string, filename: string | null) => {
if (
!filename ||
!(filename.endsWith(".ts") || filename.endsWith(".tsx")) ||
GENERATED_FILES.some((f: string) => filename.endsWith(f))
) {
return;
}
if (debounce) {
clearTimeout(debounce);
}
debounce = setTimeout(async () => {
debounce = null;
if (building) {
pending = true;
return;
}
building = true;
try {
do {
pending = false;
console.log(`\nRebuilding...`);
await build();
} while (pending);
} catch (err) {
console.error("Build failed:", err);
} finally {
building = false;
}
}, 100);
});
}