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

55 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-09-29 20:56:52 -05:00
import { exec as execSync } from 'child_process';
import { existsSync } from 'fs';
2024-09-29 22:24:35 -05:00
import { readdir, rm, writeFile } from 'fs/promises';
2025-05-24 12:24:53 -05:00
import { chunk } from 'lodash-es';
2024-09-29 20:56:52 -05:00
import { promisify } from 'util';
2024-09-28 21:07:00 -05:00
2024-09-29 20:56:52 -05:00
const exec = promisify(execSync);
let executableFileExtension: string;
2024-10-02 10:21:16 -05:00
if (existsSync('node_modules/.bin/protoc-gen-ts_proto.exe')) {
2024-09-29 20:56:52 -05:00
executableFileExtension = '.exe';
} else {
executableFileExtension = '';
}
2024-09-28 21:07:00 -05:00
2025-05-24 12:24:53 -05:00
await compile('packages/core/src/protos');
2025-05-24 12:28:57 -05:00
await compile('submodules/blitzkit-closed/src/protos');
2024-09-29 22:24:35 -05:00
2025-05-24 12:24:53 -05:00
async function compile(root: string) {
const filesRaw = await readdir(root);
2024-09-28 21:07:00 -05:00
2025-05-24 12:24:53 -05:00
for (const file of filesRaw) {
if (!file.endsWith('.ts')) continue;
await rm(`${root}/${file}`);
}
const allFiles = filesRaw.filter((file) => file.endsWith('.proto'));
for (const files of chunk(allFiles, 32)) {
const args = [
`-I=${root}`,
`--plugin=./node_modules/.bin/protoc-gen-ts_proto${executableFileExtension}`,
'--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}`,
...files.map((file) => `${root}/${file}`),
];
await exec(`protoc ${args.join(' ')}`);
}
await writeFile(
`${root}/index.ts`,
'// @ts-nocheck\n' +
allFiles
.sort()
.map((file) => file.replace('.proto', ''))
.map((file) => `export * from './${file}.ts';`)
.join('\n'),
);
}