Files
18xchat/Tools/Proto/build-proto.js
T
2025-08-13 17:23:54 +08:00

53 lines
1.5 KiB
JavaScript

// Tools/build-proto.js
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const SRC = path.join(__dirname, '..', '..', 'Proto'); // .proto 源文件目录
const OUT_JS = path.join(__dirname, '..', '..', 'assets', 'Scripts', 'proto', 'proto.pb.js');
const OUT_DTS = path.join(__dirname, '..', '..', 'assets', 'Scripts', 'proto', 'proto.pb.d.ts');
// 递归收集所有 .proto 文件路径
function walk(dir) {
const files = [];
for (const name of fs.readdirSync(dir)) {
const p = path.join(dir, name);
const stat = fs.statSync(p);
if (stat.isDirectory()) files.push(...walk(p));
else if (name.endsWith('.proto')) files.push(p);
}
return files;
}
const allProtoFiles = walk(SRC);
if (allProtoFiles.length === 0) {
console.error('没有找到任何 .proto 文件!');
process.exit(1);
}
// 确保输出目录存在
fs.mkdirSync(path.dirname(OUT_JS), { recursive: true });
// 1) 生成一个 JS 文件
execSync([
'pbjs',
'-t static-module',
'-w es6', // 生成 ESM 语法
'--dependency protobufjs/minimal.js',
`-o "${OUT_JS}"`,
`-p "${SRC}"`, // 让 pbjs 识别 proto import
...allProtoFiles.map(f => `"${f}"`) // 所有 proto 文件
].join(' '), { stdio: 'inherit' });
// 2) 生成一个 TS 声明文件
execSync([
'pbts',
'--main',
`-o "${OUT_DTS}"`,
`"${OUT_JS}"`
].join(' '), { stdio: 'inherit' });
console.log(`生成完成!
JS 文件: ${OUT_JS}
TS 文件: ${OUT_DTS}`);