pvpokemon/src/scripts/generatePogoProtocolBuffer.ts
2019-03-19 14:40:48 -04:00

46 lines
1.3 KiB
TypeScript

// based on pogo-protos/proto/compile.js
let fs = require('fs').promises;
let path = require('path');
let pathToModule = path.dirname(require.resolve('pogo-protos'));
let args = process.argv.slice(2);
let output = args[0]; // first argument is the path/name of the output file
let imports = args.slice(1); // the rest of the arguments are the protobufs to import
async function parseDir(directory : string, rel : string) {
let content = '';
for (let child of await fs.readdir(directory)) {
let sub = path.join(directory, child);
let stat = await fs.stat(sub);
if (stat.isDirectory()) {
content += await parseDir(sub, `${rel}/${child}`);
} else if (child.endsWith('.proto')) {
if (imports.indexOf(child) === -1) {
continue;
}
content += `import public "${pathToModule}/${rel}/${child}";\n`;
}
}
return content;
}
async function build() {
let content = '';
content += 'syntax = "proto3";\n';
content += 'package POGOProtos;\n\n';
for (let child of await fs.readdir(pathToModule)) {
let sub = path.join(pathToModule, child);
let stat = await fs.stat(sub);
if (stat.isDirectory()) {
content += await parseDir(sub, child);
}
}
await fs.writeFile(path.resolve(output), content);
}
build();