Skip to content

Commit ee40821

Browse files
committed
chore: remove JS code generated from protos
1 parent 12cac65 commit ee40821

21 files changed

+104
-52625
lines changed

.github/workflows/main.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ jobs:
3232
with:
3333
node-version: ${{ matrix.node_version }}
3434

35-
- name: install yarn
36-
run: npm install -g yarn
35+
- name: setup protoc
36+
uses: arduino/setup-protoc@master
37+
with:
38+
repo-token: ${{ secrets.GITHUB_TOKEN }}
3739

3840
- name: get yarn cache dir
3941
id: yarn-cache-dir
@@ -93,5 +95,10 @@ jobs:
9395
with:
9496
go-version: '~${{ matrix.go_version }}'
9597

98+
- name: setup protoc
99+
uses: arduino/setup-protoc@master
100+
with:
101+
repo-token: ${{ secrets.GITHUB_TOKEN }}
102+
96103
- name: build backend binary
97104
run: make build

.gitignore

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
# generated proxy commands
2-
https.cert
3-
https.key
1+
# generated proto scripts
2+
/app/src/types/generated
43

54
# local environment vars to ignore
65
.env*.local

app/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"eject": "react-scripts eject",
1313
"lint": "eslint --ext .ts,.tsx --ignore-path .eslintignore .",
1414
"tsc": "tsc --noEmit",
15-
"protos": "./scripts/build-protos.sh",
15+
"postinstall": "yarn protos",
16+
"protos": "node ./scripts/build-protos.js",
1617
"storybook": "start-storybook -p 9009 -s public",
1718
"build-storybook": "build-storybook -s public"
1819
},

app/scripts/build-protos.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/* eslint-disable @typescript-eslint/no-var-requires */
2+
const util = require('util');
3+
const exec = util.promisify(require('child_process').exec);
4+
const { promises: fs } = require('fs');
5+
const { join, sep } = require('path');
6+
const { platform } = require('os');
7+
const appPath = join(__dirname, '..');
8+
9+
/** list of proto files and patches to apply */
10+
const filePatches = {
11+
lnd: 'lnrpc: {}',
12+
loop: 'looprpc: {}',
13+
'google/api/annotations': 'google: { api: {} }',
14+
'google/api/http': 'google: { api: {} }',
15+
};
16+
17+
/**
18+
* Executes the `protoc` compiler to convert *.proto files into TS & JS code
19+
*/
20+
const generate = async () => {
21+
console.log('Compiling protobuf definitions');
22+
await fs.mkdir('./src/types/generated', { recursive: true });
23+
24+
const protocGen = join(
25+
appPath,
26+
'node_modules',
27+
'.bin',
28+
platform() === 'win32' ? 'protoc-gen-ts.cmd' : 'protoc-gen-ts',
29+
);
30+
const protocCmd = [
31+
'protoc',
32+
`--plugin=protoc-gen-ts=${protocGen}`,
33+
'--proto_path=../proto',
34+
'--js_out=import_style=commonjs,binary:./src/types/generated',
35+
'--ts_out=service=grpc-web:./src/types/generated',
36+
...Object.keys(filePatches).map(file => `../proto/${file}.proto`),
37+
].join(' ');
38+
39+
console.log(protocCmd);
40+
const { stdout, stderr } = await exec(protocCmd, { cwd: appPath });
41+
if (stderr) {
42+
throw new Error(`exec stderr:\n${stderr}`);
43+
}
44+
console.log(stdout);
45+
};
46+
47+
/**
48+
* Patches the generated JS files as they contain a type error that prevents webpack from bundling properly.
49+
* The code below will prepend the necessary code to resolve the error.
50+
* Example: prepends `var proto = { lnrpc: {} };` to lnd_pb.js
51+
*/
52+
const patch = async () => {
53+
console.log('Patching generated JS files');
54+
55+
for (const filename of Object.keys(filePatches)) {
56+
const patch = [
57+
'/* eslint-disable */',
58+
`var proto = { ${filePatches[filename]} };`,
59+
'',
60+
].join('\n');
61+
const path = join(
62+
appPath,
63+
'src',
64+
'types',
65+
'generated',
66+
`${filename.replace(/\//, sep)}_pb.js`,
67+
);
68+
69+
console.log(` - ${path}`);
70+
let content = await fs.readFile(path);
71+
content = `${patch}\n${content}`;
72+
await fs.writeFile(path, content);
73+
}
74+
};
75+
76+
/**
77+
* An async wrapper with error handling around the two funcs above
78+
*/
79+
const main = async () => {
80+
try {
81+
await generate();
82+
await patch();
83+
} catch (error) {
84+
console.error(error);
85+
}
86+
};
87+
88+
/**
89+
* execute the main function
90+
*/
91+
main();

app/scripts/build-protos.sh

Lines changed: 0 additions & 35 deletions
This file was deleted.

app/src/types/generated/google/api/annotations_pb.d.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

app/src/types/generated/google/api/annotations_pb.js

Lines changed: 0 additions & 48 deletions
This file was deleted.

app/src/types/generated/google/api/annotations_pb_service.d.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

app/src/types/generated/google/api/annotations_pb_service.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

app/src/types/generated/google/api/http_pb.d.ts

Lines changed: 0 additions & 136 deletions
This file was deleted.

0 commit comments

Comments
 (0)