Skip to content

Commit 632b4c7

Browse files
committed
scripts: update build-protos script to download protos from github
1 parent ca81d2d commit 632b4c7

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

app/scripts/build-protos.js

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
/* eslint-disable @typescript-eslint/no-var-requires */
22
const util = require('util');
3+
const https = require('https');
34
const exec = util.promisify(require('child_process').exec);
45
const { promises: fs } = require('fs');
56
const { join, sep } = require('path');
67
const { platform } = require('os');
78
const appPath = join(__dirname, '..');
89

10+
/** Specify the versions of LND and Loop protos to download */
11+
const LND_VERSION = 'v0.11.1-beta';
12+
const LOOP_VERSION = 'v0.11.2-beta';
13+
14+
/** mapping of proto files to the github url to download each from */
15+
const protoSources = {
16+
lnd: `lightningnetwork/lnd/${LND_VERSION}/lnrpc/rpc.proto`,
17+
loop: `lightninglabs/loop/${LOOP_VERSION}/looprpc/client.proto`,
18+
};
19+
920
/** list of proto files and patches to apply */
1021
const filePatches = {
1122
lnd: 'lnrpc: {}',
@@ -14,11 +25,33 @@ const filePatches = {
1425
'google/api/http': 'google: { api: {} }',
1526
};
1627

28+
/**
29+
* Downloads the *.proto files into the `../proto` dir
30+
*/
31+
const download = async () => {
32+
console.log('\nDownloading proto files...');
33+
for ([name, urlPath] of Object.entries(protoSources)) {
34+
const url = `https://raw.githubusercontent.com/${urlPath}`;
35+
const filePath = join(appPath, '..', 'proto', `${name}.proto`);
36+
console.log(`${url}`);
37+
console.log(` -> ${filePath}`);
38+
const content = await new Promise((resolve, reject) => {
39+
https.get(url, res => {
40+
let data = '';
41+
res.on('data', chunk => (data += chunk));
42+
res.on('error', err => reject(err));
43+
res.on('end', () => resolve(data));
44+
});
45+
});
46+
await fs.writeFile(filePath, content);
47+
}
48+
};
49+
1750
/**
1851
* Executes the `protoc` compiler to convert *.proto files into TS & JS code
1952
*/
2053
const generate = async () => {
21-
console.log('Compiling protobuf definitions');
54+
console.log('\nCompiling protobuf definitions...');
2255
await fs.mkdir('./src/types/generated', { recursive: true });
2356

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

3972
console.log(protocCmd);
40-
const { stdout, stderr } = await exec(protocCmd, { cwd: appPath });
73+
const { stderr } = await exec(protocCmd, { cwd: appPath });
4174
if (stderr) {
4275
throw new Error(`exec stderr:\n${stderr}`);
4376
}
44-
console.log(stdout);
4577
};
4678

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

5587
for (const filename of Object.keys(filePatches)) {
5688
const patch = [
@@ -78,6 +110,7 @@ const patch = async () => {
78110
*/
79111
const main = async () => {
80112
try {
113+
await download();
81114
await generate();
82115
await patch();
83116
} catch (error) {

0 commit comments

Comments
 (0)