@@ -7,11 +7,11 @@ import path from 'path';
77import yargs from 'yargs' ;
88
99import * as substrateDefs from '@polkadot/types/interfaces/definitions' ;
10- import { WebSocket } from '@polkadot/x-ws' ;
1110
1211import { generateInterfaceTypes } from './generate/interfaceRegistry' ;
1312import { generateTsDef } from './generate/tsDef' ;
1413import { generateDefaultLookup } from './generate' ;
14+ import { getMetadataViaWs } from './util' ;
1515
1616type ArgV = { input : string ; package : string ; endpoint ?: string ; } ;
1717
@@ -57,42 +57,21 @@ export function main (): void {
5757 '@polkadot/types/interfaces' : filteredBase ,
5858 [ pkg ] : userDefs
5959 } ;
60+ const inputPath = path . join ( process . cwd ( ) , input ) ;
6061
61- generateTsDef ( allDefs , path . join ( process . cwd ( ) , input ) , pkg ) ;
62- generateInterfaceTypes ( allDefs , path . join ( process . cwd ( ) , input , 'augment-types.ts' ) ) ;
62+ generateTsDef ( allDefs , inputPath , pkg ) ;
63+ generateInterfaceTypes ( allDefs , path . join ( inputPath , 'augment-types.ts' ) ) ;
6364
6465 if ( endpoint ) {
6566 if ( endpoint . startsWith ( 'wss://' ) || endpoint . startsWith ( 'ws://' ) ) {
66- try {
67- const websocket = new WebSocket ( endpoint ) ;
68-
69- websocket . onclose = ( event : { code : number ; reason : string } ) : void => {
70- console . error ( `disconnected, code: '${ event . code } ' reason: '${ event . reason } '` ) ;
71- process . exit ( 1 ) ;
72- } ;
73-
74- websocket . onerror = ( event : unknown ) : void => {
75- console . error ( event ) ;
76- process . exit ( 1 ) ;
77- } ;
78-
79- websocket . onopen = ( ) : void => {
80- console . log ( 'connected' ) ;
81- websocket . send ( '{"id":"1","jsonrpc":"2.0","method":"state_getMetadata","params":[]}' ) ;
82- } ;
83-
84- websocket . onmessage = ( message : unknown ) : void => {
85- generateDefaultLookup ( path . join ( process . cwd ( ) , input ) , ( JSON . parse ( ( message as Record < string , string > ) . data ) as Record < string , HexString > ) . result ) ;
86- process . exit ( 0 ) ;
87- } ;
88- } catch ( error ) {
89- process . exit ( 1 ) ;
90- }
67+ getMetadataViaWs ( endpoint )
68+ . then ( ( metadata ) => generateDefaultLookup ( inputPath , metadata ) )
69+ . catch ( ( ) => process . exit ( 1 ) ) ;
9170 } else {
9271 // eslint-disable-next-line @typescript-eslint/no-var-requires
9372 const metaHex = ( require ( path . join ( process . cwd ( ) , endpoint ) ) as Record < string , HexString > ) . result ;
9473
95- generateDefaultLookup ( path . join ( process . cwd ( ) , input ) , metaHex ) ;
74+ generateDefaultLookup ( inputPath , metaHex ) ;
9675 }
9776 }
9877}
0 commit comments