-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerate-protobuf.js
70 lines (60 loc) · 2.05 KB
/
generate-protobuf.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const { execSync } = require('child_process');
const { readdirSync } = require('fs');
const mkdirp = require('mkdirp');
const path = require('path');
const yargs = require('yargs');
const inputBasePath = '/defs';
const outDir = '/defs/gen/pb-js';
const pbjsPath = path.resolve(path.join(__dirname, 'node_modules/.bin/pbjs'));
const pbtsPath = path.resolve(path.join(__dirname, 'node_modules/.bin/pbts'));
const execOptions = {
cwd: inputBasePath
};
const argv = yargs
.options({
'd': {
alias: 'dir',
array: true,
default: [],
describe: 'Causes all .proto files in the specified directory to be processed'
},
// 'f': {
// alias: 'file',
// array: true,
// describe: 'Indicates file(s) to be processed'
// },
'module-name': {
demandOption: true,
describe: 'The name to be used for the output JS/TS module files',
type: 'string'
}
})
.argv;
const {
moduleName,
_: untouchedArgs, // Args on the other side of -- will be passed directly to pbjs
dir: processDirectories
} = argv;
const jsOutputPath = `${outDir}/${moduleName}.js`;
const tsOutputPath = `${outDir}/${moduleName}.d.ts`;
// Collect .proto files from specified directories
const isProtoFile = fileName => (fileName.substring(fileName.length - 6) === '.proto');
const fileList = processDirectories.reduce((out, dirName) => {
const finalDir = path.resolve(path.join(inputBasePath,dirName));
const files = readdirSync(finalDir)
.filter(isProtoFile)
.map(f => `${finalDir}/${f}`);
return out.concat(files);
},[]);
// Ensure our output directory
mkdirp.sync(outDir);
const pbjsArgs = [
`-o ${outDir}/${moduleName}.js`,
...untouchedArgs,
...fileList,
].join(' ');
console.log('Generating JS module...');
execSync(`${pbjsPath} ${pbjsArgs}`, execOptions);
const pbtsArgs = [`-o ${tsOutputPath}`, jsOutputPath].join(' ');
console.log('Generating TypeScript defintions...');
execSync(`${pbtsPath} ${pbtsArgs}`);