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;
|
|
|
|
|
|
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
|
|
|
|
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}`,
|
2024-10-02 10:21:16 -05:00
|
|
|
'--ts_proto_opt=esModuleInterop=true',
|
2024-10-05 15:57:34 -05:00
|
|
|
'--ts_proto_opt=oneof=unions-value',
|
2024-10-02 10:21:16 -05:00
|
|
|
'--ts_proto_opt=removeEnumPrefix=true',
|
2024-10-06 00:02:50 -05:00
|
|
|
'--ts_proto_opt=unrecognizedEnum=false',
|
2024-09-29 22:42:15 -05:00
|
|
|
'--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'),
|
|
|
|
|
);
|