@@ -10,37 +10,44 @@ import { Definitions } from '@polkadot/types/types';
1010import { formatNumber } from '@polkadot/util' ;
1111
1212import { generateDefaultConsts , generateDefaultErrors , generateDefaultEvents , generateDefaultQuery , generateDefaultRpc , generateDefaultTx } from './generate' ;
13- import { getMetadataViaWs , HEADER , writeFile } from './util' ;
13+ import { assertDir , assertFile , getMetadataViaWs , HEADER , writeFile } from './util' ;
1414
1515function generate ( metaHex : HexString , pkg : string | undefined , output : string , isStrict ?: boolean ) : void {
1616 console . log ( `Generating from metadata, ${ formatNumber ( ( metaHex . length - 2 ) / 2 ) } bytes` ) ;
1717
18- const base = path . join ( process . cwd ( ) , output ) ;
19- const extraTypes = pkg
20- // eslint-disable-next-line @typescript-eslint/no-var-requires
21- ? { [ pkg ] : require ( path . join ( base , 'definitions' ) ) as Record < string , any > }
22- : { } ;
18+ const outputPath = assertDir ( path . join ( process . cwd ( ) , output ) ) ;
19+ let extraTypes : Record < string , any > = { } ;
20+ let customLookupDefinitions : Definitions = { rpc : { } , types : { } } ;
2321
24- let customLookupDefinitions ;
22+ if ( pkg ) {
23+ try {
24+ extraTypes = {
25+ // eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-argument
26+ [ pkg ] : require ( assertFile ( path . join ( outputPath , 'definitions.ts' ) ) ) as Record < string , any >
27+ } ;
28+ } catch ( error ) {
29+ console . error ( 'ERROR: No custom definitions found:' , ( error as Error ) . message ) ;
30+ }
31+ }
2532
2633 try {
2734 customLookupDefinitions = {
2835 rpc : { } ,
2936 // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-var-requires
30- types : require ( path . join ( base , 'lookup.ts' ) ) . default
31- } as Definitions ;
37+ types : require ( assertFile ( path . join ( outputPath , 'lookup.ts' ) ) ) . default
38+ } ;
3239 } catch ( error ) {
33- console . log ( ' No custom definitions found.' ) ;
40+ console . error ( 'ERROR: No lookup definitions found:' , ( error as Error ) . message ) ;
3441 }
3542
36- generateDefaultConsts ( path . join ( base , 'augment-api-consts.ts' ) , metaHex , extraTypes , isStrict , customLookupDefinitions ) ;
37- generateDefaultErrors ( path . join ( base , 'augment-api-errors.ts' ) , metaHex , extraTypes , isStrict ) ;
38- generateDefaultEvents ( path . join ( base , 'augment-api-events.ts' ) , metaHex , extraTypes , isStrict , customLookupDefinitions ) ;
39- generateDefaultQuery ( path . join ( base , 'augment-api-query.ts' ) , metaHex , extraTypes , isStrict , customLookupDefinitions ) ;
40- generateDefaultRpc ( path . join ( base , 'augment-api-rpc.ts' ) , extraTypes ) ;
41- generateDefaultTx ( path . join ( base , 'augment-api-tx.ts' ) , metaHex , extraTypes , isStrict , customLookupDefinitions ) ;
43+ generateDefaultConsts ( path . join ( outputPath , 'augment-api-consts.ts' ) , metaHex , extraTypes , isStrict , customLookupDefinitions ) ;
44+ generateDefaultErrors ( path . join ( outputPath , 'augment-api-errors.ts' ) , metaHex , extraTypes , isStrict ) ;
45+ generateDefaultEvents ( path . join ( outputPath , 'augment-api-events.ts' ) , metaHex , extraTypes , isStrict , customLookupDefinitions ) ;
46+ generateDefaultQuery ( path . join ( outputPath , 'augment-api-query.ts' ) , metaHex , extraTypes , isStrict , customLookupDefinitions ) ;
47+ generateDefaultRpc ( path . join ( outputPath , 'augment-api-rpc.ts' ) , extraTypes ) ;
48+ generateDefaultTx ( path . join ( outputPath , 'augment-api-tx.ts' ) , metaHex , extraTypes , isStrict , customLookupDefinitions ) ;
4249
43- writeFile ( path . join ( base , 'augment-api.ts' ) , ( ) : string =>
50+ writeFile ( path . join ( outputPath , 'augment-api.ts' ) , ( ) : string =>
4451 [
4552 HEADER ( 'chain' ) ,
4653 ...[
@@ -82,6 +89,6 @@ export function main (): void {
8289 . catch ( ( ) => process . exit ( 1 ) ) ;
8390 } else {
8491 // eslint-disable-next-line @typescript-eslint/no-var-requires
85- generate ( ( require ( path . join ( process . cwd ( ) , endpoint ) ) as Record < string , HexString > ) . result , pkg , output , isStrict ) ;
92+ generate ( ( require ( assertFile ( path . join ( process . cwd ( ) , endpoint ) ) ) as Record < string , HexString > ) . result , pkg , output , isStrict ) ;
8693 }
8794}
0 commit comments