Skip to content

Commit

Permalink
Merge pull request #159 from lightninglabs/update-proto-build
Browse files Browse the repository at this point in the history
Update protos build script
  • Loading branch information
guggero authored Dec 14, 2020
2 parents e1e7855 + 130bd40 commit 3d93358
Show file tree
Hide file tree
Showing 12 changed files with 1,395 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
# that, generated. This will make sure the JS (and TS types for that matter)
# will not show up in the repository's language statistics.

app/src/types/generated* linguist-generated
app/src/types/generated/** linguist-generated
10 changes: 0 additions & 10 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ jobs:
with:
node-version: ${{ matrix.node_version }}

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

- name: get yarn cache dir
id: yarn-cache-dir
run: echo "::set-output name=dir::$(yarn cache dir)"
Expand Down Expand Up @@ -115,11 +110,6 @@ 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

Expand Down
41 changes: 37 additions & 4 deletions app/scripts/build-protos.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const util = require('util');
const https = require('https');
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, '..');

/** Specify the versions of LND and Loop protos to download */
const LND_VERSION = 'v0.11.1-beta';
const LOOP_VERSION = 'v0.11.2-beta';

/** mapping of proto files to the github url to download each from */
const protoSources = {
lnd: `lightningnetwork/lnd/${LND_VERSION}/lnrpc/rpc.proto`,
loop: `lightninglabs/loop/${LOOP_VERSION}/looprpc/client.proto`,
};

/** list of proto files and patches to apply */
const filePatches = {
lnd: 'lnrpc: {}',
Expand All @@ -14,11 +25,33 @@ const filePatches = {
'google/api/http': 'google: { api: {} }',
};

/**
* Downloads the *.proto files into the `../proto` dir
*/
const download = async () => {
console.log('\nDownloading proto files...');
for ([name, urlPath] of Object.entries(protoSources)) {
const url = `https://raw.githubusercontent.com/${urlPath}`;
const filePath = join(appPath, '..', 'proto', `${name}.proto`);
console.log(`${url}`);
console.log(` -> ${filePath}`);
const content = await new Promise((resolve, reject) => {
https.get(url, res => {
let data = '';
res.on('data', chunk => (data += chunk));
res.on('error', err => reject(err));
res.on('end', () => resolve(data));
});
});
await fs.writeFile(filePath, content);
}
};

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

const protocGen = join(
Expand All @@ -37,11 +70,10 @@ const generate = async () => {
].join(' ');

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

/**
Expand All @@ -50,7 +82,7 @@ const generate = async () => {
* Example: prepends `var proto = { lnrpc: {} };` to lnd_pb.js
*/
const patch = async () => {
console.log('Patching generated JS files');
console.log('\nPatching generated JS files');

for (const filename of Object.keys(filePatches)) {
const patch = [
Expand Down Expand Up @@ -78,6 +110,7 @@ const patch = async () => {
*/
const main = async () => {
try {
await download();
await generate();
await patch();
} catch (error) {
Expand Down
131 changes: 131 additions & 0 deletions app/src/types/generated/lnd_pb.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3d93358

Please sign in to comment.