Skip to content

Commit 0f00891

Browse files
committed
Add proto defintion files gen script
* Add node script to read the proto files on an specific directory and generate the JS code and TS definitions associated. * Add an example proto which includes a general microservices network structure. * Add some needed dev dependencies for the node script to work properly. This fixes #28
1 parent 0ad9ff3 commit 0f00891

File tree

4 files changed

+435
-53
lines changed

4 files changed

+435
-53
lines changed

build-scripts/gen-proto-code.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#! /usr/bin/env node
2+
const package = require('../package.json');
3+
const path = require('path');
4+
const fs = require('fs');
5+
const yargs = require('yargs');
6+
const shell = require('shelljs');
7+
const ora = require('ora');
8+
9+
// Set the directory from where we will be working
10+
const workingDir = path.join(__dirname, '..');
11+
// Add node_modules exec path
12+
process.env.PATH += (path.delimiter + path.join(workingDir, 'node_modules', '.bin'));
13+
14+
// Directory where the proto files are stored (could be changed by params)
15+
let originProtoPath = path.join(workingDir, 'proto');
16+
// Generated JS code and TS definitions directory (could be changed by params)
17+
let destCodePath = path.join(workingDir, 'src', 'app', 'shared', 'proto');
18+
19+
// Reading params
20+
const argv = yargs
21+
.usage('gen-proto-code\n\nUsage: $0 [options]')
22+
.help('help').alias('help', 'h')
23+
.version(package.version).alias('version', 'v')
24+
.options({
25+
input: {
26+
alias: ['i'],
27+
description: '<input-proto-path>',
28+
requiresArg: true,
29+
default: originProtoPath,
30+
string: true,
31+
normalize: true,
32+
coerce: arg => {
33+
if (fs.existsSync(arg)) {
34+
return arg;
35+
}
36+
else {
37+
throw new Error(`Error: ${arg} is not a valid directory path`);
38+
}
39+
}
40+
},
41+
output: {
42+
alias: 'o',
43+
description: '<output-destination-path>',
44+
requiresArg: true,
45+
default: destCodePath,
46+
string: true,
47+
normalize: true,
48+
coerce: arg => {
49+
if (fs.existsSync(arg)) {
50+
return arg;
51+
}
52+
else {
53+
throw new Error(`Error: ${arg} is not a valid directory path`);
54+
}
55+
}
56+
}
57+
})
58+
.fail((msg, err, yargs) => {
59+
shell.echo(msg);
60+
process.exit(1);
61+
})
62+
.wrap(Math.min(120, yargs.terminalWidth()))
63+
.argv;
64+
65+
originProtoPath = argv.input;
66+
destCodePath = argv.output;
67+
68+
69+
const funcList = shell.ls('-l', path.join(originProtoPath, '*.proto'))
70+
.map(file => {
71+
72+
const protoname = path.basename(file.name, path.extname(file.name));
73+
74+
const jsCodeFilePath = path.join(destCodePath, `${protoname}.js`);
75+
const tsCodeFilePath = path.join(destCodePath, `${protoname}.d.ts`);
76+
77+
const execCommand = `pbjs -t static-module -o ${jsCodeFilePath} -w commonjs ${file.name} &
78+
pbts -o ${tsCodeFilePath} ${jsCodeFilePath}`;
79+
80+
const processProto = () => {
81+
return new Promise((resolve, reject) => {
82+
const spinner = new ora(`Processing ${protoname}`).start();
83+
shell.exec(execCommand, { async: true }, code => code ? reject(spinner.fail()) : resolve(spinner.succeed()));
84+
});
85+
};
86+
87+
return processProto;
88+
89+
});
90+
91+
// This function is needed to control the async code of each func of funcList
92+
async function run () {
93+
shell.echo(`Analyzing ${path.join(originProtoPath)} directory`);
94+
if (funcList.length > 0) {
95+
shell.echo(`${funcList.length} proto files found`);
96+
for (const func of funcList) {
97+
await func();
98+
}
99+
shell.echo(`Generated code stored in ${path.join(destCodePath)}`);
100+
}
101+
else {
102+
shell.echo('No proto files found');
103+
}
104+
}
105+
106+
run();

0 commit comments

Comments
 (0)