1
1
/* eslint-disable @typescript-eslint/no-var-requires */
2
2
const util = require ( 'util' ) ;
3
+ const https = require ( 'https' ) ;
3
4
const exec = util . promisify ( require ( 'child_process' ) . exec ) ;
4
5
const { promises : fs } = require ( 'fs' ) ;
5
6
const { join, sep } = require ( 'path' ) ;
6
7
const { platform } = require ( 'os' ) ;
7
8
const appPath = join ( __dirname , '..' ) ;
8
9
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
+
9
20
/** list of proto files and patches to apply */
10
21
const filePatches = {
11
22
lnd : 'lnrpc: {}' ,
@@ -14,11 +25,33 @@ const filePatches = {
14
25
'google/api/http' : 'google: { api: {} }' ,
15
26
} ;
16
27
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
+
17
50
/**
18
51
* Executes the `protoc` compiler to convert *.proto files into TS & JS code
19
52
*/
20
53
const generate = async ( ) => {
21
- console . log ( 'Compiling protobuf definitions' ) ;
54
+ console . log ( '\nCompiling protobuf definitions... ' ) ;
22
55
await fs . mkdir ( './src/types/generated' , { recursive : true } ) ;
23
56
24
57
const protocGen = join (
@@ -37,11 +70,10 @@ const generate = async () => {
37
70
] . join ( ' ' ) ;
38
71
39
72
console . log ( protocCmd ) ;
40
- const { stdout , stderr } = await exec ( protocCmd , { cwd : appPath } ) ;
73
+ const { stderr } = await exec ( protocCmd , { cwd : appPath } ) ;
41
74
if ( stderr ) {
42
75
throw new Error ( `exec stderr:\n${ stderr } ` ) ;
43
76
}
44
- console . log ( stdout ) ;
45
77
} ;
46
78
47
79
/**
@@ -50,7 +82,7 @@ const generate = async () => {
50
82
* Example: prepends `var proto = { lnrpc: {} };` to lnd_pb.js
51
83
*/
52
84
const patch = async ( ) => {
53
- console . log ( 'Patching generated JS files' ) ;
85
+ console . log ( '\nPatching generated JS files' ) ;
54
86
55
87
for ( const filename of Object . keys ( filePatches ) ) {
56
88
const patch = [
@@ -78,6 +110,7 @@ const patch = async () => {
78
110
*/
79
111
const main = async ( ) => {
80
112
try {
113
+ await download ( ) ;
81
114
await generate ( ) ;
82
115
await patch ( ) ;
83
116
} catch ( error ) {
0 commit comments