Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 25 additions & 18 deletions packages/typegen/src/fromChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, 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<string, any> }
: {};
const outputPath = assertDir(path.join(process.cwd(), output));
let extraTypes: Record<string, any> = {};
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(assertFile(path.join(outputPath, 'definitions.ts'))) as Record<string, any>
};
} 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'),
...[
Expand Down Expand Up @@ -82,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<string, HexString>).result, pkg, output, isStrict);
generate((require(assertFile(path.join(process.cwd(), endpoint))) as Record<string, HexString>).result, pkg, output, isStrict);
}
}
17 changes: 12 additions & 5 deletions packages/typegen/src/fromDefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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; };

Expand All @@ -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<string, any>;
const inputPath = assertDir(path.join(process.cwd(), input));
let userDefs: Record<string, any> = {};

try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
userDefs = require(assertFile(path.join(inputPath, 'definitions.ts'))) as Record<string, any>;
} 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<string, unknown>)
Expand All @@ -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'));
Expand All @@ -69,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<string, HexString>).result;
const metaHex = (require(assertFile(path.join(process.cwd(), endpoint))) as Record<string, HexString>).result;

generateDefaultLookup(inputPath, metaHex);
}
Expand Down
18 changes: 18 additions & 0 deletions packages/typegen/src/util/assert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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 assertFile (path: string): string {
assert(fs.existsSync(path) && fs.lstatSync(path).isFile(), `${path} is not a file`);

return path;
}
1 change: 1 addition & 0 deletions packages/typegen/src/util/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down