chore(build): move exports.d.ts to index.d.ts in build step

This commit is contained in:
Erica Marigold 2024-12-27 11:42:34 +00:00
parent e08f13897a
commit 8de63be6ba
Signed by: DevComp
GPG key ID: 429EF1C337871656
3 changed files with 30 additions and 4 deletions

View file

@ -1,3 +1,17 @@
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();
@ -15,10 +29,20 @@ async function extractLuauExports(file: string): Promise<string | null> {
return null;
}
const OUT_DIR = "./out";
const EXPORTS_TS_FILE = "./src/exports.ts";
const EXPORTS_LUAU_FILE = "./out/init.luau";
const EXPORTS_LUAU_FILE = path.join(OUT_DIR, "init.luau");
console.log("extract and export Luau type thunk");
await extractLuauExports(EXPORTS_TS_FILE).then((exports) => Bun.write(EXPORTS_LUAU_FILE, exports!));
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

View file

@ -25,6 +25,7 @@
"types": "out/index.d.ts",
"files": [
"out",
"!out/exports.d.ts",
"!**/*.tsbuildinfo"
],
"publishConfig": {

View file

@ -25,5 +25,6 @@
"declaration": true
},
"include": ["src/**/*.ts"]
// exclude build script
"exclude": ["build.ts"]
}