From 9777da10eeb60274291c3104b07ee2d4a36385d3 Mon Sep 17 00:00:00 2001 From: Jaco Date: Mon, 9 May 2022 09:38:59 +0300 Subject: [PATCH 1/3] Allow for optional definitions in typegen --- CHANGELOG.md | 1 + packages/typegen/src/fromChain.ts | 41 +++++++++++++++++------------ packages/typegen/src/fromDefs.ts | 15 ++++++++--- packages/typegen/src/util/assert.ts | 24 +++++++++++++++++ packages/typegen/src/util/index.ts | 1 + 5 files changed, 61 insertions(+), 21 deletions(-) create mode 100644 packages/typegen/src/util/assert.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index f1617e6f37db..488cbdc82840 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Changes: - Added support for typegen over `Range/RangeInclusive` types - Add explicit support for `Call` typegen (inclusive of non-defaults) - Deupe `wss://` handling in `polkadot-types-from-{chain, defs}` +- Allow for optional `definitions.ts` in typegen (only use chain) ## 8.3.2 May 8, 2022 diff --git a/packages/typegen/src/fromChain.ts b/packages/typegen/src/fromChain.ts index 2b18807a9075..1083d61e8810 100644 --- a/packages/typegen/src/fromChain.ts +++ b/packages/typegen/src/fromChain.ts @@ -10,37 +10,44 @@ import { Definitions } from '@polkadot/types/types'; import { formatNumber } from '@polkadot/util'; import { generateDefaultConsts, generateDefaultErrors, generateDefaultEvents, generateDefaultQuery, generateDefaultRpc, generateDefaultTx } from './generate'; -import { getMetadataViaWs, HEADER, writeFile } from './util'; +import { assertDir, assertExist, assertFile, getMetadataViaWs, HEADER, writeFile } from './util'; function generate (metaHex: HexString, pkg: string | undefined, output: string, isStrict?: boolean): void { console.log(`Generating from metadata, ${formatNumber((metaHex.length - 2) / 2)} bytes`); - const base = path.join(process.cwd(), output); - const extraTypes = pkg - // eslint-disable-next-line @typescript-eslint/no-var-requires - ? { [pkg]: require(path.join(base, 'definitions')) as Record } - : {}; + const outputPath = assertDir(path.join(process.cwd(), output)); + let extraTypes: Record = {}; + let customLookupDefinitions: Definitions = { rpc: {}, types: {} }; - let customLookupDefinitions; + if (pkg) { + try { + extraTypes = { + // eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-argument + [pkg]: require(assertExist(path.join(outputPath, 'definitions'))) as Record + }; + } catch (error) { + console.error('ERROR: No custom definitions found:', (error as Error).message); + } + } try { customLookupDefinitions = { rpc: {}, // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-var-requires - types: require(path.join(base, 'lookup.ts')).default - } as Definitions; + types: require(assertFile(path.join(outputPath, 'lookup.ts'))).default + }; } catch (error) { - console.log('No custom definitions found.'); + console.error('ERROR: No lookup definitions found:', (error as Error).message); } - generateDefaultConsts(path.join(base, 'augment-api-consts.ts'), metaHex, extraTypes, isStrict, customLookupDefinitions); - generateDefaultErrors(path.join(base, 'augment-api-errors.ts'), metaHex, extraTypes, isStrict); - generateDefaultEvents(path.join(base, 'augment-api-events.ts'), metaHex, extraTypes, isStrict, customLookupDefinitions); - generateDefaultQuery(path.join(base, 'augment-api-query.ts'), metaHex, extraTypes, isStrict, customLookupDefinitions); - generateDefaultRpc(path.join(base, 'augment-api-rpc.ts'), extraTypes); - generateDefaultTx(path.join(base, 'augment-api-tx.ts'), metaHex, extraTypes, isStrict, customLookupDefinitions); + generateDefaultConsts(path.join(outputPath, 'augment-api-consts.ts'), metaHex, extraTypes, isStrict, customLookupDefinitions); + generateDefaultErrors(path.join(outputPath, 'augment-api-errors.ts'), metaHex, extraTypes, isStrict); + generateDefaultEvents(path.join(outputPath, 'augment-api-events.ts'), metaHex, extraTypes, isStrict, customLookupDefinitions); + generateDefaultQuery(path.join(outputPath, 'augment-api-query.ts'), metaHex, extraTypes, isStrict, customLookupDefinitions); + generateDefaultRpc(path.join(outputPath, 'augment-api-rpc.ts'), extraTypes); + generateDefaultTx(path.join(outputPath, 'augment-api-tx.ts'), metaHex, extraTypes, isStrict, customLookupDefinitions); - writeFile(path.join(base, 'augment-api.ts'), (): string => + writeFile(path.join(outputPath, 'augment-api.ts'), (): string => [ HEADER('chain'), ...[ diff --git a/packages/typegen/src/fromDefs.ts b/packages/typegen/src/fromDefs.ts index 14cb491ab485..45a593bdade6 100644 --- a/packages/typegen/src/fromDefs.ts +++ b/packages/typegen/src/fromDefs.ts @@ -11,7 +11,7 @@ import * as substrateDefs from '@polkadot/types/interfaces/definitions'; import { generateInterfaceTypes } from './generate/interfaceRegistry'; import { generateTsDef } from './generate/tsDef'; import { generateDefaultLookup } from './generate'; -import { getMetadataViaWs } from './util'; +import { assertDir, assertFile, getMetadataViaWs } from './util'; type ArgV = { input: string; package: string; endpoint?: string; }; @@ -33,8 +33,16 @@ export function main (): void { } }).argv as ArgV; - // eslint-disable-next-line @typescript-eslint/no-var-requires - const userDefs = require(path.join(process.cwd(), input, 'definitions.ts')) as Record; + const inputPath = assertDir(path.join(process.cwd(), input)); + let userDefs: Record = {}; + + try { + // eslint-disable-next-line @typescript-eslint/no-var-requires + userDefs = require(assertFile(path.join(inputPath, 'definitions.ts'))) as Record; + } catch (error) { + console.error('ERROR: Unable to load user definitions:', (error as Error).message); + } + const userKeys = Object.keys(userDefs); const filteredBase = Object .entries(substrateDefs as Record) @@ -57,7 +65,6 @@ export function main (): void { '@polkadot/types/interfaces': filteredBase, [pkg]: userDefs }; - const inputPath = path.join(process.cwd(), input); generateTsDef(allDefs, inputPath, pkg); generateInterfaceTypes(allDefs, path.join(inputPath, 'augment-types.ts')); diff --git a/packages/typegen/src/util/assert.ts b/packages/typegen/src/util/assert.ts new file mode 100644 index 000000000000..e6ddaf7c4c0b --- /dev/null +++ b/packages/typegen/src/util/assert.ts @@ -0,0 +1,24 @@ +// Copyright 2017-2022 @polkadot/typegen authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import fs from 'fs'; + +import { assert } from '@polkadot/util'; + +export function assertDir (path: string): string { + assert(fs.existsSync(path) && fs.lstatSync(path).isDirectory(), `${path} is not a directory`); + + return path; +} + +export function assertExist (path: string): string { + assert(fs.existsSync(path), `${path} does not exist`); + + return path; +} + +export function assertFile (path: string): string { + assert(fs.existsSync(path) && fs.lstatSync(path).isFile(), `${path} is not a file`); + + return path; +} diff --git a/packages/typegen/src/util/index.ts b/packages/typegen/src/util/index.ts index 4073ae3344c5..1aa0d7c593f1 100644 --- a/packages/typegen/src/util/index.ts +++ b/packages/typegen/src/util/index.ts @@ -1,6 +1,7 @@ // Copyright 2017-2022 @polkadot/typegen authors & contributors // SPDX-License-Identifier: Apache-2.0 +export * from './assert'; export * from './derived'; export * from './docs'; export * from './file'; From 4c9c4309a205b2cf67cee9f61ff94d4fc292e88a Mon Sep 17 00:00:00 2001 From: Jaco Date: Mon, 9 May 2022 09:43:34 +0300 Subject: [PATCH 2/3] Adjust --- packages/typegen/src/fromChain.ts | 4 ++-- packages/typegen/src/util/assert.ts | 6 ------ 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/packages/typegen/src/fromChain.ts b/packages/typegen/src/fromChain.ts index 1083d61e8810..a390b84831e6 100644 --- a/packages/typegen/src/fromChain.ts +++ b/packages/typegen/src/fromChain.ts @@ -10,7 +10,7 @@ import { Definitions } from '@polkadot/types/types'; import { formatNumber } from '@polkadot/util'; import { generateDefaultConsts, generateDefaultErrors, generateDefaultEvents, generateDefaultQuery, generateDefaultRpc, generateDefaultTx } from './generate'; -import { assertDir, assertExist, assertFile, getMetadataViaWs, HEADER, writeFile } from './util'; +import { assertDir, assertFile, getMetadataViaWs, HEADER, writeFile } from './util'; function generate (metaHex: HexString, pkg: string | undefined, output: string, isStrict?: boolean): void { console.log(`Generating from metadata, ${formatNumber((metaHex.length - 2) / 2)} bytes`); @@ -23,7 +23,7 @@ function generate (metaHex: HexString, pkg: string | undefined, output: string, try { extraTypes = { // eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-argument - [pkg]: require(assertExist(path.join(outputPath, 'definitions'))) as Record + [pkg]: require(assertFile(path.join(outputPath, 'definitions.ts'))) as Record }; } catch (error) { console.error('ERROR: No custom definitions found:', (error as Error).message); diff --git a/packages/typegen/src/util/assert.ts b/packages/typegen/src/util/assert.ts index e6ddaf7c4c0b..02bc065ab7dd 100644 --- a/packages/typegen/src/util/assert.ts +++ b/packages/typegen/src/util/assert.ts @@ -11,12 +11,6 @@ export function assertDir (path: string): string { return path; } -export function assertExist (path: string): string { - assert(fs.existsSync(path), `${path} does not exist`); - - return path; -} - export function assertFile (path: string): string { assert(fs.existsSync(path) && fs.lstatSync(path).isFile(), `${path} is not a file`); From 220b31dfcfd4aed4ed7093bf213a0ac9eedfce5d Mon Sep 17 00:00:00 2001 From: Jaco Date: Mon, 9 May 2022 09:45:46 +0300 Subject: [PATCH 3/3] Adjust --- packages/typegen/src/fromChain.ts | 2 +- packages/typegen/src/fromDefs.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/typegen/src/fromChain.ts b/packages/typegen/src/fromChain.ts index a390b84831e6..7ec75aedd804 100644 --- a/packages/typegen/src/fromChain.ts +++ b/packages/typegen/src/fromChain.ts @@ -89,6 +89,6 @@ export function main (): void { .catch(() => process.exit(1)); } else { // eslint-disable-next-line @typescript-eslint/no-var-requires - generate((require(path.join(process.cwd(), endpoint)) as Record).result, pkg, output, isStrict); + generate((require(assertFile(path.join(process.cwd(), endpoint))) as Record).result, pkg, output, isStrict); } } diff --git a/packages/typegen/src/fromDefs.ts b/packages/typegen/src/fromDefs.ts index 45a593bdade6..0796dfd120fb 100644 --- a/packages/typegen/src/fromDefs.ts +++ b/packages/typegen/src/fromDefs.ts @@ -76,7 +76,7 @@ export function main (): void { .catch(() => process.exit(1)); } else { // eslint-disable-next-line @typescript-eslint/no-var-requires - const metaHex = (require(path.join(process.cwd(), endpoint)) as Record).result; + const metaHex = (require(assertFile(path.join(process.cwd(), endpoint))) as Record).result; generateDefaultLookup(inputPath, metaHex); }