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

62 lines
1.5 KiB
TypeScript
Raw Normal View History

2025-09-12 13:29:10 -05:00
import { exec as execSync } from "child_process";
import { existsSync } from "fs";
2025-09-12 13:42:33 -05:00
import { readdir, rm } from "fs/promises";
2025-09-12 13:29:10 -05:00
import { promisify } from "util";
2024-09-28 21:07:00 -05:00
2025-09-12 13:29:10 -05:00
const MAX_COMMAND_LENGTH = 2 ** 11;
2025-11-13 12:53:19 -06:00
const TS_PROTO_EXECUTABLE = "./node_modules/.bin/protoc-gen-ts_proto";
2025-09-12 13:29:10 -05:00
const roots = [
2025-10-13 16:53:37 -05:00
// "../../packages/core/src/protos",
"../../packages/closed/protos",
2025-09-12 13:29:10 -05:00
];
2024-09-29 20:56:52 -05:00
const exec = promisify(execSync);
let executableFileExtension: string;
2025-11-13 12:53:19 -06:00
if (existsSync(`${TS_PROTO_EXECUTABLE}.exe`)) {
2025-09-12 13:29:10 -05:00
executableFileExtension = ".exe";
2024-09-29 20:56:52 -05:00
} else {
2025-09-12 13:29:10 -05:00
executableFileExtension = "";
2024-09-29 20:56:52 -05:00
}
2024-09-28 21:07:00 -05:00
2025-09-12 13:29:10 -05:00
for (const root of roots) {
const filesRaw = await readdir(`${root}`);
const files: string[] = [];
for (const file of filesRaw) {
if (file.endsWith(".proto")) files.push(file);
if (file.endsWith(".ts")) await rm(`${root}/${file}`);
}
while (files.length > 0) {
let command = [
"protoc",
2025-11-13 12:53:19 -06:00
`--plugin=${TS_PROTO_EXECUTABLE}${executableFileExtension}`,
2025-09-12 13:29:10 -05:00
"--ts_proto_opt=esModuleInterop=true",
"--ts_proto_opt=oneof=unions-value",
// "--ts_proto_opt=removeEnumPrefix=true",
"--ts_proto_opt=unrecognizedEnum=false",
"--ts_proto_opt=snakeToCamel=false",
`--ts_proto_out=${root}`,
`-I=${root}`,
].join(" ");
while (true) {
if (files.length === 0) break;
const file = files.at(-1);
const newLine = ` ${root}/${file}`;
if (command.length + newLine.length < MAX_COMMAND_LENGTH) {
command += newLine;
files.pop();
} else {
break;
}
2024-09-29 22:24:35 -05:00
}
2025-09-12 13:29:10 -05:00
await exec(command);
}
}