diff --git a/package.json b/package.json index 3a4b1f7..187a4a0 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,8 @@ "format": "prettier --write \"**/*.{js,ts,vue,scss,html,md,json}\" --ignore-path .gitignore", "test": "echo \"No test specified\" && exit 0", "dev": "quasar dev", - "build": "quasar build" + "build": "quasar build", + "protoc": "node scripts/proto.cjs" }, "dependencies": { "@quasar/extras": "^1.16.4", diff --git a/scripts/proto.cjs b/scripts/proto.cjs new file mode 100644 index 0000000..12d7e9a --- /dev/null +++ b/scripts/proto.cjs @@ -0,0 +1,91 @@ +/** + * 将proto文件编译到 src/proto/ + */ +const { readdirSync } = require('fs'); +const { resolve } = require('path'); +const os = require('os'); + +const { exec } = require('child_process'); + +const messageDir = resolve(__dirname, '../rtss-proto-msg'); +const protoDir = resolve(messageDir, 'src'); +const destDir = resolve(__dirname, '../src/protos'); + +/** + * 递归处理所有proto文件生成 + * @param {*} file 文件 + * @param {*} path 目录 + */ +function recursiveGenerate(file, path = [], cmds = []) { + if (file.isFile()) { + // 文件,生成 + if (file.name.endsWith('.proto')) { + cmds.push(buildGenerateCmd(file.name, path)); + } else { + console.warn('不是proto文件', file.name); + } + } else if (file.isDirectory()) { + // 文件夹,递归 + readdirSync(resolve(protoDir, ...path, file.name), { + withFileTypes: true, + }).forEach((f) => { + const subPath = [...path, file.name]; + recursiveGenerate(f, subPath, cmds); + }); + } +} + +const isLinux = os.type().toLowerCase().includes('linux'); + +function buildGenerateCmd(name, path = []) { + const protoPath = resolve(protoDir, ...path); + const tsPath = resolve(destDir, ...path); + + let cmd = ['protoc', `-I=${protoPath}`, `--ts_out=${tsPath}`, `${name}`]; + let cmdStr = cmd.join(' '); + return cmdStr; +} + +function main() { + const protocDir = resolve(messageDir, 'protoc-27.4'); + const protocBin = resolve( + protocDir, + `bin/${isLinux ? 'linux-x86_64' : 'win64'}` + ); + const prepareCmds = []; + const setPathCmd = isLinux + ? ['export', `PATH=${protocBin}:${protocDir}:"$PATH"`].join(' ') + : ['set', `PATH=${protocBin};${protocDir};%PATH%`].join(' '); + const protocVersionCmd = ['protoc', '--version'].join(' '); + prepareCmds.push(setPathCmd); + prepareCmds.push(protocVersionCmd); + + readdirSync(protoDir, { + withFileTypes: true, + }).forEach((f) => { + recursiveGenerate(f, [], prepareCmds); + }); + + // console.log(prepareCmds); + + exec( + prepareCmds.join(' && '), + { + maxBuffer: 1024 * 2000, + }, + (err, stdout, stderr) => { + if (err) { + console.error(err); + throw err; + } else if (stderr.length > 0) { + console.error(stderr.toString()); + throw new Error(stderr.toString()); + } else { + console.log(stdout); + } + } + ); +} + +// 执行 +main();