Augment user type registry (#1926)

* Augment user type registry

* Augment InterfaceRegistry

* Addition api.createType test

* Import '@polkadot/types/interfaceAugment'

* Skip CC on generated

* Move augmentation to full path imports

* Output to augment-*
This commit is contained in:
Jaco Greeff
2020-02-23 11:48:40 +01:00
committed by GitHub
parent 3d8d2e4988
commit a35da085d6
12 changed files with 1592 additions and 1578 deletions
+9 -4
View File
@@ -2,6 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import fs from 'fs';
import path from 'path';
import yargs from 'yargs';
import getWSClass from '@polkadot/rpc-provider/ws/getWSClass';
@@ -20,15 +21,19 @@ function generate (metaHex: string, pkg: string | undefined, output: string, isS
const extraTypes = pkg
? { [pkg]: require(path.join(process.cwd(), output, 'definitions')) }
: {};
const hasRegistry = fs.existsSync(path.join(process.cwd(), output, 'augment-registry.ts'));
generateConst(path.join(process.cwd(), output, 'augment.consts.ts'), metaHex, extraTypes, isStrict);
generateQuery(path.join(process.cwd(), output, 'augment.query.ts'), metaHex, extraTypes, isStrict);
generateTx(path.join(process.cwd(), output, 'augment.tx.ts'), metaHex, extraTypes, isStrict);
generateConst(path.join(process.cwd(), output, 'augment-consts.ts'), metaHex, extraTypes, isStrict);
generateQuery(path.join(process.cwd(), output, 'augment-query.ts'), metaHex, extraTypes, isStrict);
generateTx(path.join(process.cwd(), output, 'augment-tx.ts'), metaHex, extraTypes, isStrict);
writeFile(path.join(process.cwd(), output, 'augment.ts'), (): string =>
[
HEADER,
...['./augment.consts', './augment.query', './augment.tx', '@polkadot/api/types/augment/rpc'].map((path) => `import '${path}';\n`)
...[
'@polkadot/api/types/augment/rpc',
...['consts', 'query', hasRegistry ? 'registry' : '', 'tx'].filter((key) => !!key).map((key) => `./augment-${key}`)
].map((path) => `import '${path}';\n`)
].join('')
);
+1 -1
View File
@@ -49,5 +49,5 @@ export default function main (): void {
};
generateTsDef(allDefs, path.join(process.cwd(), input), pkg);
generateInterfaceRegistry(allDefs, path.join(process.cwd(), input, 'interfaceRegistry.ts'));
generateInterfaceRegistry({ [pkg]: userDefs }, path.join(process.cwd(), input, 'augment-registry.ts'), true);
}
@@ -14,14 +14,13 @@ const primitiveClasses = {
};
/** @internal */
export function generateInterfaceRegistry (importDefinitions: { [importPath: string]: object }, dest: string): void {
export function generateInterfaceRegistry (importDefinitions: { [importPath: string]: object }, dest: string, skipPrimitives?: boolean): void {
writeFile(dest, (): string => {
Object.entries(importDefinitions).reduce((acc, def) => Object.assign(acc, def), {} as object);
const imports = createImports(importDefinitions);
const definitions = imports.definitions;
const primitives = Object
const primitives = skipPrimitives ? '' : Object
.keys(primitiveClasses)
.filter((name): boolean => !!name.indexOf('Generic'))
.reduce((accumulator, primitiveName): string => {
@@ -29,7 +28,7 @@ export function generateInterfaceRegistry (importDefinitions: { [importPath: str
return [
accumulator,
getDerivedTypes(definitions, primitiveName, primitiveName, imports).map(indent(2)).join('\n')
getDerivedTypes(definitions, primitiveName, primitiveName, imports).map(indent(4)).join('\n')
].join('\n');
}, '');
@@ -40,7 +39,7 @@ export function generateInterfaceRegistry (importDefinitions: { [importPath: str
return [
accumulator,
...Object.keys(types).map((type): string =>
getDerivedTypes(definitions, type, types[type], imports).map(indent(2)).join('\n')
getDerivedTypes(definitions, type, types[type], imports).map(indent(4)).join('\n')
)
].join('\n');
}, '');
@@ -52,8 +51,8 @@ export function generateInterfaceRegistry (importDefinitions: { [importPath: str
}))
]);
const interfaceStart = 'export interface InterfaceRegistry {';
const interfaceEnd = '\n}';
const interfaceStart = "declare module '@polkadot/types/interfaceRegistry' {\n export interface InterfaceRegistry {";
const interfaceEnd = '\n }\n}';
return header
.concat(interfaceStart)
@@ -68,6 +67,6 @@ export function generateInterfaceRegistry (importDefinitions: { [importPath: str
export default function generateDefaultInterfaceRegistry (): void {
generateInterfaceRegistry(
{ '@polkadot/types/interfaces': defaultDefinitions },
'packages/types/src/interfaceRegistry.ts'
'packages/types/src/interfaceAugment.ts'
);
}