Skip to content

chore: remove JS code generated from protos #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ jobs:
with:
node-version: ${{ matrix.node_version }}

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

- name: get yarn cache dir
id: yarn-cache-dir
Expand Down Expand Up @@ -93,5 +95,10 @@ jobs:
with:
go-version: '~${{ matrix.go_version }}'

- name: setup protoc
uses: arduino/setup-protoc@master
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

- name: build backend binary
run: make build
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# generated proxy commands
https.cert
https.key
# generated proto scripts
/app/src/types/generated

# local environment vars to ignore
.env*.local
Expand Down
3 changes: 2 additions & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"eject": "react-scripts eject",
"lint": "eslint --ext .ts,.tsx --ignore-path .eslintignore .",
"tsc": "tsc --noEmit",
"protos": "./scripts/build-protos.sh",
"postinstall": "yarn protos",
"protos": "node ./scripts/build-protos.js",
"storybook": "start-storybook -p 9009 -s public",
"build-storybook": "build-storybook -s public"
},
Expand Down
91 changes: 91 additions & 0 deletions app/scripts/build-protos.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const { promises: fs } = require('fs');
const { join, sep } = require('path');
const { platform } = require('os');
const appPath = join(__dirname, '..');

/** list of proto files and patches to apply */
const filePatches = {
lnd: 'lnrpc: {}',
loop: 'looprpc: {}',
'google/api/annotations': 'google: { api: {} }',
'google/api/http': 'google: { api: {} }',
};

/**
* Executes the `protoc` compiler to convert *.proto files into TS & JS code
*/
const generate = async () => {
console.log('Compiling protobuf definitions');
await fs.mkdir('./src/types/generated', { recursive: true });

const protocGen = join(
appPath,
'node_modules',
'.bin',
platform() === 'win32' ? 'protoc-gen-ts.cmd' : 'protoc-gen-ts',
);
const protocCmd = [
'protoc',
`--plugin=protoc-gen-ts=${protocGen}`,
'--proto_path=../proto',
'--js_out=import_style=commonjs,binary:./src/types/generated',
'--ts_out=service=grpc-web:./src/types/generated',
...Object.keys(filePatches).map(file => `../proto/${file}.proto`),
].join(' ');

console.log(protocCmd);
const { stdout, stderr } = await exec(protocCmd, { cwd: appPath });
if (stderr) {
throw new Error(`exec stderr:\n${stderr}`);
}
console.log(stdout);
};

/**
* Patches the generated JS files as they contain a type error that prevents webpack from bundling properly.
* The code below will prepend the necessary code to resolve the error.
* Example: prepends `var proto = { lnrpc: {} };` to lnd_pb.js
*/
const patch = async () => {
console.log('Patching generated JS files');

for (const filename of Object.keys(filePatches)) {
const patch = [
'/* eslint-disable */',
`var proto = { ${filePatches[filename]} };`,
'',
].join('\n');
const path = join(
appPath,
'src',
'types',
'generated',
`${filename.replace(/\//, sep)}_pb.js`,
);

console.log(` - ${path}`);
let content = await fs.readFile(path);
content = `${patch}\n${content}`;
await fs.writeFile(path, content);
}
};

/**
* An async wrapper with error handling around the two funcs above
*/
const main = async () => {
try {
await generate();
await patch();
} catch (error) {
console.error(error);
}
};

/**
* execute the main function
*/
main();
35 changes: 0 additions & 35 deletions app/scripts/build-protos.sh

This file was deleted.

9 changes: 0 additions & 9 deletions app/src/types/generated/google/api/annotations_pb.d.ts

This file was deleted.

48 changes: 0 additions & 48 deletions app/src/types/generated/google/api/annotations_pb.js

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions app/src/types/generated/google/api/annotations_pb_service.js

This file was deleted.

136 changes: 0 additions & 136 deletions app/src/types/generated/google/api/http_pb.d.ts

This file was deleted.

Loading