Files
blitzkit/packages/scripts/src/protos.ts
T

67 lines
1.8 KiB
TypeScript
Raw Normal View History

2026-01-04 15:35:58 -06:00
import { execFile } from "child_process";
2025-09-12 13:29:10 -05:00
import { existsSync } from "fs";
2026-01-04 15:35:58 -06:00
import { mkdir, readdir, rm, writeFile } from "fs/promises";
2024-09-28 21:07:00 -05:00
const TS_PROTO_EXECUTABLE_LOCATIONS = [
"./node_modules/.bin/protoc-gen-ts_proto",
"../../node_modules/.bin/protoc-gen-ts_proto",
];
2026-05-15 22:30:52 -05:00
const ROOT = "../closed/protos";
2024-09-29 20:56:52 -05:00
let tsProtoExecutableLocation: string | undefined = undefined;
2024-09-29 20:56:52 -05:00
for (const location of TS_PROTO_EXECUTABLE_LOCATIONS) {
if (existsSync(location)) {
tsProtoExecutableLocation = location;
break;
} else if (existsSync(`${location}.exe`)) {
tsProtoExecutableLocation = `${location}.exe`;
break;
}
}
if (!tsProtoExecutableLocation) {
throw new Error(
`Could not find ts-proto executable. Checked locations: ${TS_PROTO_EXECUTABLE_LOCATIONS.join(
2026-03-31 13:35:47 -05:00
", ",
)}`,
);
2024-09-29 20:56:52 -05:00
}
2024-09-28 21:07:00 -05:00
2026-05-15 22:30:52 -05:00
await mkdir("../../temp", { recursive: true });
2026-01-04 15:35:58 -06:00
2026-04-09 21:59:58 -05:00
let args = "";
2026-01-04 15:35:58 -06:00
2026-04-09 21:59:58 -05:00
args += `--plugin=${tsProtoExecutableLocation}\n`;
args += "--ts_proto_opt=esModuleInterop=true\n";
args += "--ts_proto_opt=oneof=unions-value\n";
args += "--ts_proto_opt=unrecognizedEnum=false\n";
args += "--ts_proto_opt=snakeToCamel=false\n";
args += `--ts_proto_out=${ROOT}\n`;
2026-05-15 22:30:52 -05:00
args += `-I=${ROOT}\n`;
// for (const dir of await readdir(ROOT)) {
// args += `-I=${ROOT}/${dir}\n`;
// }
2026-04-09 21:59:58 -05:00
const filesRaw = await readdir(`${ROOT}`, { recursive: true });
const files: string[] = [];
for (const file of filesRaw) {
2026-05-15 22:30:52 -05:00
if (!file.startsWith("blitz/")) continue;
2026-04-09 21:59:58 -05:00
if (file.endsWith(".proto")) {
files.push(file);
args += `${ROOT}/${file}\n`;
2026-01-04 15:35:58 -06:00
}
2026-04-09 21:59:58 -05:00
if (file.endsWith(".ts")) await rm(`${ROOT}/${file}`);
2025-09-12 13:29:10 -05:00
}
2026-04-09 21:59:58 -05:00
2026-05-15 22:30:52 -05:00
await writeFile("../../temp/protoc.txt", args);
2026-04-09 21:59:58 -05:00
2026-05-15 22:30:52 -05:00
execFile("protoc", ["@../../temp/protoc.txt"], (error, stdout, stderr) => {
2026-04-09 21:59:58 -05:00
if (error) throw new Error(error.message);
if (stderr) console.error(stderr);
if (stdout) console.log(stdout);
});