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

41 lines
1.0 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';
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 ROOT = 'packages/core/src/protos';
const exec = promisify(execSync);
let executableFileExtension: string;
if (existsSync('node_modules/.bin/protoc-gen-ts.exe')) {
executableFileExtension = '.exe';
} else {
executableFileExtension = '';
}
2024-09-28 21:07:00 -05:00
2024-09-29 22:24:35 -05:00
const filesRaw = await readdir(`${ROOT}`);
await Promise.all(
filesRaw.map(async (file) => {
if (!file.endsWith('.proto')) {
await rm(`${ROOT}/${file}`);
}
}),
);
const files = filesRaw.filter((file) => file.endsWith('.proto'));
2024-09-28 21:07:00 -05:00
const args = [
2024-09-29 22:42:15 -05:00
`--plugin=./node_modules/.bin/protoc-gen-ts_proto${executableFileExtension}`,
'--ts_proto_out=.',
2024-09-29 22:24:35 -05:00
...files.map((file) => `${ROOT}/${file}`),
2024-09-28 21:07:00 -05:00
];
2024-09-29 20:56:52 -05:00
await exec(`protoc ${args.join(' ')}`);
2024-09-29 22:24:35 -05:00
await writeFile(
`${ROOT}/index.ts`,
files
.map((file) => file.replace('.proto', ''))
2024-09-29 22:42:15 -05:00
.map((file) => `export * from './${file}.ts';`)
2024-09-29 22:24:35 -05:00
.join('\n'),
);