-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypebox-gen.ts
26 lines (20 loc) · 939 Bytes
/
typebox-gen.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
import { readdir } from "node:fs/promises";
import { resolve } from "path";
import * as Codegen from "@sinclair/typebox-codegen";
const TYPES_DIR = resolve(import.meta.dir, "src", "types");
const OUTPUT_DIR = resolve(import.meta.dir, "dist", "typebox");
// read all the files in the current directory
const files = await readdir(TYPES_DIR);
for (const file of files) {
if (!file.endsWith(".ts")) continue;
const openedFile = Bun.file(resolve(TYPES_DIR, file));
let content = await openedFile.text();
const imports = Array.from(content.matchAll(/import ([^;]+);/g));
for (const importLine of imports) {
content = content.replace(importLine[0], "");
}
let typeboxCode = Codegen.TypeScriptToTypeBox.Generate(content);
const importLines = imports.map((s) => s[0]).join("\n");
typeboxCode = importLines ? `${importLines}\n\n${typeboxCode}` : typeboxCode;
await Bun.write(resolve(OUTPUT_DIR, file), typeboxCode);
}