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

43 lines
1.1 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 20:56:52 -05:00
`--plugin="protoc-gen-ts=./node_modules/.bin/protoc-gen-ts${executableFileExtension}"`,
'--ts_opt=esModuleInterop=true',
2024-09-29 22:09:43 -05:00
`--js_out="import_style=commonjs,binary:."`,
`--ts_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', ''))
.map((file) => `export * from './${file}_pb';`)
.join('\n'),
);