-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ts
48 lines (40 loc) · 1.5 KB
/
build.ts
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
import { write } from "bun";
import { copyFile } from "fs/promises";
import { unlink } from "fs/promises";
import path from "path";
// changes the extension of a path to `newExt`
function withExtension(filePath: string, newExt: string): string {
const dir = path.dirname(filePath);
const base = path.basename(filePath, path.extname(filePath));
const newFilePath = path.join(dir, `${base}.${newExt}`);
return newFilePath;
}
// extracts the Luau exports section from a given file path
async function extractLuauExports(file: string): Promise<string | null> {
const input = await Bun.file(file).text();
const regex = /\/\/ @@@@@@@@@ LUAU_START @@@@@@@@@([\s\S]*?)\/\/ @@@@@@@@@ LUAU_END @@@@@@@@@/;
const match = input.match(regex);
if (match) {
// remove leading comment specifier from each line from each line
return match[1]
.split("\n")
.map((line) => line.replace(/\/\/\s?/, ""))
.join("\n")
.trim();
}
return null;
}
const OUT_DIR = "./out";
const EXPORTS_TS_FILE = "./src/exports.ts";
const EXPORTS_LUAU_FILE = path.join(OUT_DIR, "init.luau");
await extractLuauExports(EXPORTS_TS_FILE)
.then(async (exports) => {
console.log("extract and export luau type thunk");
await write(EXPORTS_LUAU_FILE, exports!);
})
.finally(async () => {
console.log("export ts definition files");
const luauExportsFile = path.join(OUT_DIR, path.basename(withExtension(EXPORTS_TS_FILE, "d.ts")));
await write(path.join(OUT_DIR, "index.d.ts"), Bun.file(luauExportsFile));
});
export {}; // treat as esmodule