make ts generate script more reusable (#1610)
* make ts generate script more reusable * avoid use global definitions * fix error * fix formatting * seems working
This commit is contained in:
committed by
Jaco Greeff
parent
09a72c0f31
commit
762b16ea13
@@ -3,6 +3,7 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { ModuleMetadataLatest } from '../../interfaces/metadata';
|
||||
import * as definitions from '../../interfaces/definitions';
|
||||
|
||||
import fs from 'fs';
|
||||
import staticData from '@polkadot/metadata/Metadata/static';
|
||||
@@ -17,14 +18,14 @@ function generateModule (modul: ModuleMetadataLatest, imports: TypeImports): str
|
||||
return [];
|
||||
}
|
||||
|
||||
setImports(imports, ['Codec']);
|
||||
setImports(definitions, imports, ['Codec']);
|
||||
|
||||
return [indent(4)(`${stringCamelCase(modul.name.toString())}: {`)]
|
||||
.concat(indent(6)('[index: string]: Codec;'))
|
||||
.concat(
|
||||
modul.constants
|
||||
.map((constant): string => {
|
||||
setImports(imports, [constant.type.toString()]);
|
||||
setImports(definitions, imports, [constant.type.toString()]);
|
||||
|
||||
return indent(6)(`${stringCamelCase(constant.name.toString())}: ${constant.type} & ConstantCodec;`);
|
||||
})
|
||||
@@ -32,12 +33,12 @@ function generateModule (modul: ModuleMetadataLatest, imports: TypeImports): str
|
||||
.concat([indent(4)('};')]);
|
||||
}
|
||||
|
||||
// Generate `packages/types-jsonrpc/src/jsonrpc.types.ts` for a particular
|
||||
// Generate `packages/api/src/consts.types.ts` for a particular
|
||||
// metadata
|
||||
function generateForMeta (meta: Metadata): void {
|
||||
console.log('Writing packages/api/src/consts.types.ts');
|
||||
|
||||
const imports = createImports(); // Will hold all needed imports
|
||||
const imports = createImports({ '@polkadot/types/interfaces': definitions }); // Will hold all needed imports
|
||||
|
||||
const body = meta.asLatest.modules.reduce((acc, modul): string[] => {
|
||||
const storageEntries = generateModule(modul, imports);
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
import fs from 'fs';
|
||||
|
||||
import * as definitions from '../../interfaces/definitions';
|
||||
import * as defaultDefinitions from '../../interfaces/definitions';
|
||||
import * as primitiveClasses from '../../primitive';
|
||||
import {
|
||||
createImportCode, createImports,
|
||||
@@ -15,32 +15,34 @@ import {
|
||||
setImports
|
||||
} from '../util';
|
||||
|
||||
// Generate `packages/types/src/interfaceRegistry.ts`, the registry of all interfaces
|
||||
export default function generateInterfaceRegistry (): void {
|
||||
console.log('Writing interfaceRegistry.ts');
|
||||
export function generateInterfaceRegistry (importDefinitions: { [importPath: string]: object }, output: string): void {
|
||||
console.log(`Writing ${output}`);
|
||||
|
||||
const imports = createImports();
|
||||
Object.entries(importDefinitions).reduce((acc, def) => Object.assign(acc, def), {} as object);
|
||||
|
||||
const imports = createImports(importDefinitions);
|
||||
const definitions = imports.definitions;
|
||||
|
||||
const primitives = Object
|
||||
.keys(primitiveClasses)
|
||||
.filter((name): boolean => !!name.indexOf('Generic'))
|
||||
.reduce((accumulator, primitiveName): string => {
|
||||
setImports(imports, [primitiveName]);
|
||||
setImports(definitions, imports, [primitiveName]);
|
||||
|
||||
return [
|
||||
accumulator,
|
||||
getDerivedTypes(primitiveName, primitiveName, imports).map(indent(2)).join('\n')
|
||||
getDerivedTypes(definitions, primitiveName, primitiveName, imports).map(indent(2)).join('\n')
|
||||
].join('\n');
|
||||
}, '');
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const srml = Object.entries(definitions).reduce((accumulator, [_defName, { types }]): string => {
|
||||
setImports(imports, Object.keys(types));
|
||||
setImports(definitions, imports, Object.keys(types));
|
||||
|
||||
return [
|
||||
accumulator,
|
||||
...Object.keys(types).map((type): string =>
|
||||
getDerivedTypes(type, (types as any)[type], imports).map(indent(2)).join('\n')
|
||||
getDerivedTypes(definitions, type, types[type], imports).map(indent(2)).join('\n')
|
||||
)
|
||||
].join('\n');
|
||||
}, '');
|
||||
@@ -55,7 +57,7 @@ export default function generateInterfaceRegistry (): void {
|
||||
types: Object.keys(imports.primitiveTypes)
|
||||
},
|
||||
...Object.keys(imports.localTypes).map((moduleName): { file: string; types: string[] } => ({
|
||||
file: `@polkadot/types/interfaces/${moduleName}`,
|
||||
file: `${imports.moduleToPackage[moduleName]}/${moduleName}`,
|
||||
types: Object.keys(imports.localTypes[moduleName])
|
||||
}))
|
||||
]);
|
||||
@@ -64,8 +66,18 @@ export default function generateInterfaceRegistry (): void {
|
||||
const interfaceEnd = '\n}';
|
||||
|
||||
fs.writeFileSync(
|
||||
'packages/types/src/interfaceRegistry.ts',
|
||||
output,
|
||||
header.concat(interfaceStart).concat(primitives).concat(srml).concat(interfaceEnd).concat(FOOTER)
|
||||
, { flag: 'w' }
|
||||
);
|
||||
}
|
||||
|
||||
// Generate `packages/types/src/interfaceRegistry.ts`, the registry of all interfaces
|
||||
export default function generateDefaultInterfaceRegistry (): void {
|
||||
generateInterfaceRegistry(
|
||||
{
|
||||
'@polkadot/types/interfaces': defaultDefinitions
|
||||
},
|
||||
'packages/types/src/interfaceRegistry.ts'
|
||||
);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
import { ModuleMetadataLatest, StorageEntryMetadataLatest } from '../../interfaces/metadata';
|
||||
import { Registry } from '../../types';
|
||||
import * as defaultDefinitions from '../../interfaces/definitions';
|
||||
|
||||
import fs from 'fs';
|
||||
import staticData from '@polkadot/metadata/Metadata/static';
|
||||
@@ -14,41 +15,41 @@ import { TypeRegistry } from '../../codec';
|
||||
import { createImportCode, createImports, FOOTER, formatType, getSimilarTypes, HEADER, indent, setImports, TypeImports } from '../util';
|
||||
|
||||
// From a storage entry metadata, we return [args, returnType]
|
||||
function entrySignature (registry: Registry, storageEntry: StorageEntryMetadataLatest, imports: TypeImports): [string, string] {
|
||||
function entrySignature (definitions: object, registry: Registry, storageEntry: StorageEntryMetadataLatest, imports: TypeImports): [string, string] {
|
||||
if (storageEntry.type.isPlain) {
|
||||
setImports(imports, [storageEntry.type.asPlain.toString()]);
|
||||
setImports(definitions, imports, [storageEntry.type.asPlain.toString()]);
|
||||
|
||||
return ['', formatType(storageEntry.type.asPlain.toString(), imports)];
|
||||
return ['', formatType(definitions, storageEntry.type.asPlain.toString(), imports)];
|
||||
} else if (storageEntry.type.isMap) {
|
||||
// Find similar types of the `key` type
|
||||
const similarTypes = getSimilarTypes(registry, storageEntry.type.asMap.key.toString(), imports);
|
||||
const similarTypes = getSimilarTypes(definitions, registry, storageEntry.type.asMap.key.toString(), imports);
|
||||
|
||||
setImports(imports, [
|
||||
setImports(definitions, imports, [
|
||||
...similarTypes,
|
||||
storageEntry.type.asMap.value.toString()
|
||||
]);
|
||||
|
||||
return [
|
||||
`arg: ${similarTypes.map((type) => formatType(type, imports)).join(' | ')}`,
|
||||
formatType(storageEntry.type.asMap.value.toString(), imports)
|
||||
`arg: ${similarTypes.map((type) => formatType(definitions, type, imports)).join(' | ')}`,
|
||||
formatType(definitions, storageEntry.type.asMap.value.toString(), imports)
|
||||
];
|
||||
} else if (storageEntry.type.isDoubleMap) {
|
||||
// Find similartypes of `key1` and `key2` types
|
||||
const similarTypes1 = getSimilarTypes(registry, storageEntry.type.asDoubleMap.key1.toString(), imports);
|
||||
const similarTypes2 = getSimilarTypes(registry, storageEntry.type.asDoubleMap.key2.toString(), imports);
|
||||
const similarTypes1 = getSimilarTypes(definitions, registry, storageEntry.type.asDoubleMap.key1.toString(), imports);
|
||||
const similarTypes2 = getSimilarTypes(definitions, registry, storageEntry.type.asDoubleMap.key2.toString(), imports);
|
||||
|
||||
setImports(imports, [
|
||||
setImports(definitions, imports, [
|
||||
...similarTypes1,
|
||||
...similarTypes2,
|
||||
storageEntry.type.asDoubleMap.value.toString()
|
||||
]);
|
||||
|
||||
const key1Types = similarTypes1.map((type) => formatType(type, imports)).join(' | ');
|
||||
const key2Types = similarTypes2.map((type) => formatType(type, imports)).join(' | ');
|
||||
const key1Types = similarTypes1.map((type) => formatType(definitions, type, imports)).join(' | ');
|
||||
const key2Types = similarTypes2.map((type) => formatType(definitions, type, imports)).join(' | ');
|
||||
|
||||
return [
|
||||
`key1: ${key1Types}, key2: ${key2Types}`,
|
||||
formatType(storageEntry.type.asDoubleMap.value.toString(), imports)
|
||||
formatType(definitions, storageEntry.type.asDoubleMap.value.toString(), imports)
|
||||
];
|
||||
}
|
||||
|
||||
@@ -56,8 +57,8 @@ function entrySignature (registry: Registry, storageEntry: StorageEntryMetadataL
|
||||
}
|
||||
|
||||
// Generate types for one storage entry in a module
|
||||
function generateEntry (registry: Registry, storageEntry: StorageEntryMetadataLatest, imports: TypeImports): string[] {
|
||||
const [args, returnType] = entrySignature(registry, storageEntry, imports);
|
||||
function generateEntry (definitions: object, registry: Registry, storageEntry: StorageEntryMetadataLatest, imports: TypeImports): string[] {
|
||||
const [args, returnType] = entrySignature(definitions, registry, storageEntry, imports);
|
||||
|
||||
return [
|
||||
`${stringLowerFirst(storageEntry.name.toString())}: StorageEntryExact<ApiType, (${args}) => Observable<${returnType}>> & QueryableStorageEntry<ApiType>;`
|
||||
@@ -65,7 +66,7 @@ function generateEntry (registry: Registry, storageEntry: StorageEntryMetadataLa
|
||||
}
|
||||
|
||||
// Generate types for one module
|
||||
function generateModule (registry: Registry, modul: ModuleMetadataLatest, imports: TypeImports): string[] {
|
||||
function generateModule (definitions: object, registry: Registry, modul: ModuleMetadataLatest, imports: TypeImports): string[] {
|
||||
if (modul.storage.isNone) {
|
||||
return [];
|
||||
}
|
||||
@@ -75,22 +76,22 @@ function generateModule (registry: Registry, modul: ModuleMetadataLatest, import
|
||||
.concat(
|
||||
modul.storage.unwrap().items
|
||||
.reduce((acc, storageEntry): string[] => {
|
||||
return acc.concat(generateEntry(registry, storageEntry, imports).map(indent(6)));
|
||||
return acc.concat(generateEntry(definitions, registry, storageEntry, imports).map(indent(6)));
|
||||
}, [] as string[])
|
||||
.join('\n')
|
||||
)
|
||||
.concat([indent(4)('};')]);
|
||||
}
|
||||
|
||||
// Generate `packages/types-jsonrpc/src/jsonrpc.types.ts` for a particular
|
||||
// Generate `packages/api/src/query.types.ts` for a particular
|
||||
// metadata
|
||||
function generateForMeta (registry: Registry, meta: Metadata): void {
|
||||
function generateForMeta (definitions: object, registry: Registry, meta: Metadata): void {
|
||||
console.log('Writing packages/api/src/query.types.ts');
|
||||
|
||||
const imports = createImports(); // Will hold all needed imports
|
||||
const imports = createImports({ '@polkadot/types/interfaces': definitions }); // Will hold all needed imports
|
||||
|
||||
const body = meta.asLatest.modules.reduce((acc, modul): string[] => {
|
||||
const storageEntries = generateModule(registry, modul, imports);
|
||||
const storageEntries = generateModule(definitions, registry, modul, imports);
|
||||
|
||||
return acc.concat(storageEntries);
|
||||
}, [] as string[]);
|
||||
@@ -140,5 +141,5 @@ function generateForMeta (registry: Registry, meta: Metadata): void {
|
||||
export default function generateQuery (): void {
|
||||
const registry = new TypeRegistry();
|
||||
|
||||
return generateForMeta(registry, new Metadata(registry, staticData));
|
||||
return generateForMeta(defaultDefinitions, registry, new Metadata(registry, staticData));
|
||||
}
|
||||
|
||||
@@ -5,26 +5,27 @@
|
||||
import fs from 'fs';
|
||||
|
||||
import interfaces from '../../../../type-jsonrpc/src';
|
||||
import * as definitions from '../../interfaces/definitions';
|
||||
import { TypeRegistry } from '../../codec/create';
|
||||
import { createImportCode, createImports, FOOTER, getSimilarTypes, HEADER, setImports } from '../util';
|
||||
|
||||
// Generate `packages/types-jsonrpc/src/jsonrpc.types.ts`
|
||||
// Generate `packages/rpc-core/jsonrpc.types.ts`
|
||||
export default function generateRpcTypes (): void {
|
||||
console.log('Writing packages/rpc-core/jsonrpc.types.ts');
|
||||
|
||||
const registry = new TypeRegistry();
|
||||
const imports = createImports();
|
||||
const imports = createImports({ '@polkadot/types/interfaces': definitions });
|
||||
|
||||
const body = Object.keys(interfaces).sort().reduce<string[]>((allSections, section): string[] => {
|
||||
const allMethods = Object.keys(interfaces[section].methods).sort().map((key): string => {
|
||||
const method = interfaces[section].methods[key];
|
||||
|
||||
setImports(imports, [method.type]);
|
||||
setImports(definitions, imports, [method.type]);
|
||||
|
||||
// FIXME These 2 are too hard to type, I give up
|
||||
if (section === 'state') {
|
||||
if (method.method === 'getStorage') {
|
||||
setImports(imports, ['Codec']);
|
||||
setImports(definitions, imports, ['Codec']);
|
||||
return ' getStorage<T = Codec>(key: any, block?: Hash | Uint8Array | string): Observable<T>;';
|
||||
} else if (method.method === 'subscribeStorage') {
|
||||
return ' subscribeStorage<T = Codec[]>(keys: any[]): Observable<T>;';
|
||||
@@ -32,8 +33,8 @@ export default function generateRpcTypes (): void {
|
||||
}
|
||||
|
||||
const args = method.params.map((param): string => {
|
||||
const similarTypes = getSimilarTypes(registry, param.type, imports);
|
||||
setImports(imports, [param.type, ...similarTypes]);
|
||||
const similarTypes = getSimilarTypes(definitions, registry, param.type, imports);
|
||||
setImports(definitions, imports, [param.type, ...similarTypes]);
|
||||
|
||||
return `${param.name}${param.isOptional ? '?' : ''}: ${similarTypes.join(' | ')}`;
|
||||
});
|
||||
|
||||
@@ -5,59 +5,47 @@
|
||||
import { TypeDef, TypeDefInfo, TypeDefExtVecFixed } from '../../codec/types';
|
||||
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { isString, stringCamelCase, stringUpperFirst } from '@polkadot/util';
|
||||
|
||||
import { getTypeDef } from '../../codec/create';
|
||||
import * as definitions from '../../interfaces/definitions';
|
||||
import { createImportCode, createImports, exportInterface, exportType, formatBTreeMap, formatCompact, formatOption, formatResult, formatTuple, formatVec, FOOTER, HEADER, setImports, TypeImports } from '../util';
|
||||
import * as defaultDefinitions from '../../interfaces/definitions';
|
||||
import { createImportCode, createImports, exportInterface, exportType, formatType, FOOTER, HEADER, setImports, TypeImports } from '../util';
|
||||
|
||||
interface Imports extends TypeImports {
|
||||
interfaces: [string, string][];
|
||||
}
|
||||
|
||||
// helper to generate a `readonly <Name>: <Type>;` getter
|
||||
export function createGetter (name = '', type: string, imports: TypeImports, doc?: string): string {
|
||||
setImports(imports, [type]);
|
||||
export function createGetter (definitions: object, name = '', type: string, imports: TypeImports, doc?: string): string {
|
||||
setImports(definitions, imports, [type]);
|
||||
return ` /** ${doc || type} */\n readonly ${name}: ${type};\n`;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
function errorUnhandled (def: TypeDef, imports: TypeImports): string {
|
||||
function errorUnhandled (definitions: object, def: TypeDef, imports: TypeImports): string {
|
||||
throw new Error(`Generate: ${name}: Unhandled type ${TypeDefInfo[def.info]}`);
|
||||
}
|
||||
|
||||
function tsBTreeMap ({ name: resultName, sub, type }: TypeDef, imports: TypeImports): string {
|
||||
const [keyDef, valDef] = (sub as TypeDef[]);
|
||||
|
||||
setImports(imports, [type]);
|
||||
|
||||
return exportInterface(resultName, formatBTreeMap(keyDef.type, valDef.type));
|
||||
function tsExport (definitions: object, def: TypeDef, imports: TypeImports): string {
|
||||
return exportInterface(def.name, formatType(definitions, def, imports));
|
||||
}
|
||||
|
||||
function tsCompact ({ name: compactName, sub }: TypeDef, imports: TypeImports): string {
|
||||
const def = (sub as TypeDef);
|
||||
const tsBTreeMap = tsExport;
|
||||
const tsCompact = tsExport;
|
||||
const tsOption = tsExport;
|
||||
const tsPlain = tsExport;
|
||||
const tsTuple = tsExport;
|
||||
|
||||
setImports(imports, ['Compact']);
|
||||
|
||||
switch (def.info) {
|
||||
case TypeDefInfo.Plain:
|
||||
setImports(imports, [def.type]);
|
||||
return exportType(compactName, formatCompact(def.type));
|
||||
|
||||
default:
|
||||
throw new Error(`Enum: ${compactName}: Unhandled type ${TypeDefInfo[def.info]}`);
|
||||
}
|
||||
}
|
||||
|
||||
function tsEnum ({ name: enumName, sub }: TypeDef, imports: TypeImports): string {
|
||||
setImports(imports, ['Enum']);
|
||||
function tsEnum (definitions: object, { name: enumName, sub }: TypeDef, imports: TypeImports): string {
|
||||
setImports(definitions, imports, ['Enum']);
|
||||
|
||||
const keys = (sub as TypeDef[]).map(({ info, name = '', type }, index): string => {
|
||||
const getter = stringUpperFirst(stringCamelCase(name.replace(' ', '_')));
|
||||
const [enumType, asGetter] = type === 'Null'
|
||||
? ['', '']
|
||||
: [`(${type})`, createGetter(`as${getter}`, type, imports)];
|
||||
const isGetter = createGetter(`is${getter}`, 'boolean', imports, `${index}:: ${name}${enumType}`);
|
||||
: [`(${type})`, createGetter(definitions, `as${getter}`, type, imports)];
|
||||
const isGetter = createGetter(definitions, `is${getter}`, 'boolean', imports, `${index}:: ${name}${enumType}`);
|
||||
|
||||
switch (info) {
|
||||
case TypeDefInfo.Plain:
|
||||
@@ -72,32 +60,11 @@ function tsEnum ({ name: enumName, sub }: TypeDef, imports: TypeImports): string
|
||||
return exportInterface(enumName, 'Enum', keys.join(''));
|
||||
}
|
||||
|
||||
function tsOption ({ name: optionName, sub }: TypeDef, imports: TypeImports): string {
|
||||
const def = (sub as TypeDef);
|
||||
|
||||
setImports(imports, ['Option']);
|
||||
|
||||
switch (def.info) {
|
||||
case TypeDefInfo.Plain:
|
||||
setImports(imports, [def.type]);
|
||||
return exportType(optionName, formatOption(def.type));
|
||||
|
||||
default:
|
||||
throw new Error(`Enum: ${optionName}: Unhandled type ${TypeDefInfo[def.info]}`);
|
||||
}
|
||||
}
|
||||
|
||||
function tsPlain ({ name: plainName, type }: TypeDef, imports: TypeImports): string {
|
||||
setImports(imports, [type]);
|
||||
|
||||
return exportType(plainName, type);
|
||||
}
|
||||
|
||||
function tsResultGetter (resultName = '', getter: 'Ok' | 'Error', { info, name = '', type }: TypeDef, imports: TypeImports): string {
|
||||
function tsResultGetter (definitions: object, resultName = '', getter: 'Ok' | 'Error', { info, name = '', type }: TypeDef, imports: TypeImports): string {
|
||||
const [resultType, asGetter] = type === 'Null'
|
||||
? ['', '']
|
||||
: [`(${type})`, createGetter(`as${getter}`, type, imports)];
|
||||
const isGetter = createGetter(`is${getter}`, 'boolean', imports, `${getter}:: ${name}${resultType}`);
|
||||
: [`(${type})`, createGetter(definitions, `as${getter}`, type, imports)];
|
||||
const isGetter = createGetter(definitions, `is${getter}`, 'boolean', imports, `${getter}:: ${name}${resultType}`);
|
||||
|
||||
switch (info) {
|
||||
case TypeDefInfo.Plain:
|
||||
@@ -109,119 +76,57 @@ function tsResultGetter (resultName = '', getter: 'Ok' | 'Error', { info, name =
|
||||
}
|
||||
}
|
||||
|
||||
function tsResult ({ name: resultName, sub, type }: TypeDef, imports: TypeImports): string {
|
||||
const [okDef, errorDef] = (sub as TypeDef[]);
|
||||
function tsResult (definitions: object, def: TypeDef, imports: TypeImports): string {
|
||||
const [okDef, errorDef] = (def.sub as TypeDef[]);
|
||||
const inner = [
|
||||
tsResultGetter(resultName, 'Error', errorDef, imports),
|
||||
tsResultGetter(resultName, 'Ok', okDef, imports)
|
||||
tsResultGetter(definitions, def.name, 'Error', errorDef, imports),
|
||||
tsResultGetter(definitions, def.name, 'Ok', okDef, imports)
|
||||
].join('');
|
||||
|
||||
setImports(imports, [type]);
|
||||
setImports(definitions, imports, [def.type]);
|
||||
|
||||
return exportInterface(resultName, formatResult(okDef.type, errorDef.type), inner);
|
||||
return exportInterface(def.name, formatType(definitions, def, imports), inner);
|
||||
}
|
||||
|
||||
function _tsStructGetterType (structName: string | undefined, { info, sub, type }: TypeDef, imports: TypeImports): [string, string] {
|
||||
let _type;
|
||||
|
||||
switch (info) {
|
||||
case TypeDefInfo.Compact:
|
||||
_type = (sub as TypeDef).type;
|
||||
|
||||
setImports(imports, ['Compact']);
|
||||
|
||||
return [_type, formatCompact(_type)];
|
||||
|
||||
case TypeDefInfo.Option:
|
||||
_type = (sub as TypeDef).type;
|
||||
|
||||
setImports(imports, ['Option']);
|
||||
|
||||
return [_type, formatOption(_type)];
|
||||
|
||||
case TypeDefInfo.Plain:
|
||||
return [type, type];
|
||||
|
||||
case TypeDefInfo.Vec:
|
||||
_type = (sub as TypeDef).type;
|
||||
|
||||
setImports(imports, ['Vec']);
|
||||
|
||||
return [_type, formatVec(_type)];
|
||||
|
||||
default:
|
||||
throw new Error(`Struct: ${structName}: Unhandled type ${TypeDefInfo[info]}`);
|
||||
}
|
||||
}
|
||||
|
||||
function tsSet ({ name: setName, sub }: TypeDef, imports: TypeImports): string {
|
||||
setImports(imports, ['Set']);
|
||||
function tsSet (definitions: object, { name: setName, sub }: TypeDef, imports: TypeImports): string {
|
||||
setImports(definitions, imports, ['Set']);
|
||||
|
||||
const types = (sub as TypeDef[]).map(({ name }): string =>
|
||||
createGetter(`is${name}`, 'boolean', imports)
|
||||
createGetter(definitions, `is${name}`, 'boolean', imports)
|
||||
);
|
||||
|
||||
return exportInterface(setName, 'Set', types.join(''));
|
||||
}
|
||||
|
||||
function tsStruct ({ name: structName, sub }: TypeDef, imports: TypeImports): string {
|
||||
function tsStruct (definitions: object, { name: structName, sub }: TypeDef, imports: TypeImports): string {
|
||||
setImports(definitions, imports, ['Struct']);
|
||||
|
||||
const keys = (sub as TypeDef[]).map((typedef): string => {
|
||||
const [embedType, returnType] = _tsStructGetterType(structName, typedef, imports);
|
||||
const returnType = formatType(definitions, typedef, imports);
|
||||
|
||||
setImports(imports, ['Struct', embedType]);
|
||||
|
||||
return createGetter(typedef.name, returnType, imports);
|
||||
return createGetter(definitions, typedef.name, returnType, imports);
|
||||
});
|
||||
|
||||
return exportInterface(structName, 'Struct', keys.join(''));
|
||||
}
|
||||
|
||||
function _tsTupleGetterType (tupleName: string | undefined, { info, sub, type }: TypeDef, imports: TypeImports): string {
|
||||
switch (info) {
|
||||
case TypeDefInfo.Option:
|
||||
setImports(imports, ['Option', (sub as TypeDef).type]);
|
||||
|
||||
return type;
|
||||
|
||||
case TypeDefInfo.Plain:
|
||||
setImports(imports, [type]);
|
||||
|
||||
return type;
|
||||
|
||||
default:
|
||||
throw new Error(`Struct: ${tupleName}: Unhandled type ${TypeDefInfo[info]}`);
|
||||
}
|
||||
}
|
||||
|
||||
function tsTuple ({ name: tupleName, sub }: TypeDef, imports: TypeImports): string {
|
||||
setImports(imports, ['ITuple']);
|
||||
|
||||
const types = (sub as TypeDef[]).map((typedef): string =>
|
||||
_tsTupleGetterType(tupleName, typedef, imports)
|
||||
);
|
||||
|
||||
return exportType(tupleName, formatTuple(types));
|
||||
}
|
||||
|
||||
function tsVec ({ ext, info, name: vectorName, sub }: TypeDef, imports: TypeImports): string {
|
||||
const type = info === TypeDefInfo.VecFixed
|
||||
? (ext as TypeDefExtVecFixed).type
|
||||
: (sub as TypeDef).type;
|
||||
function tsVec (definitions: object, def: TypeDef, imports: TypeImports): string {
|
||||
const type = def.info === TypeDefInfo.VecFixed
|
||||
? (def.ext as TypeDefExtVecFixed).type
|
||||
: (def.sub as TypeDef).type;
|
||||
|
||||
// FIXME This should be a VecFixed
|
||||
// FIXME Technically Vec has length prefix, so for others this is not 100%
|
||||
if (info === TypeDefInfo.VecFixed && type === 'u8') {
|
||||
setImports(imports, ['Codec']);
|
||||
if (def.info === TypeDefInfo.VecFixed && type === 'u8') {
|
||||
setImports(definitions, imports, ['Codec']);
|
||||
|
||||
return exportType(vectorName, 'Uint8Array, Codec');
|
||||
return exportType(def.name, 'Uint8Array, Codec');
|
||||
}
|
||||
|
||||
setImports(imports, ['Vec', type]);
|
||||
|
||||
return exportType(vectorName, formatVec(type));
|
||||
return exportInterface(def.name, formatType(definitions, def, imports));
|
||||
}
|
||||
|
||||
function generateInterfaces ({ types }: { types: Record<string, any> }, imports: Imports): [string, string][] {
|
||||
function generateInterfaces (definitions: object, { types }: { types: Record<string, any> }, imports: Imports): [string, string][] {
|
||||
// handlers are defined externally to use - this means that when we do a
|
||||
// `generators[typedef.info](...)` TS will show any unhandled types. Rather
|
||||
// we are being explicit in having no handlers where we do not support (yet)
|
||||
@@ -244,13 +149,14 @@ function generateInterfaces ({ types }: { types: Record<string, any> }, imports:
|
||||
return Object.entries(types).map(([name, type]): [string, string] => {
|
||||
const def = getTypeDef(isString(type) ? type.toString() : JSON.stringify(type), { name });
|
||||
|
||||
return [name, generators[def.info](def, imports)];
|
||||
return [name, generators[def.info](definitions, def, imports)];
|
||||
});
|
||||
}
|
||||
|
||||
function generateTsDefFor (defName: string, { types }: { types: Record<string, any> }): void {
|
||||
const imports = { ...createImports({ types }), interfaces: [] } as Imports;
|
||||
const interfaces = generateInterfaces({ types }, imports);
|
||||
function generateTsDefFor (importDefinitions: { [importPath: string]: object }, defName: string, { types }: { types: Record<string, any> }, outputDir: string): void {
|
||||
const imports = { ...createImports(importDefinitions, { types }), interfaces: [] } as Imports;
|
||||
const definitions = imports.definitions;
|
||||
const interfaces = generateInterfaces(definitions, { types }, imports);
|
||||
const sortedDefs = interfaces.sort((a, b): number => a[0].localeCompare(b[0])).map(([, definition]): string => definition).join('\n\n');
|
||||
|
||||
const header = createImportCode(HEADER, [
|
||||
@@ -267,7 +173,7 @@ function generateTsDefFor (defName: string, { types }: { types: Record<string, a
|
||||
types: Object.keys(imports.primitiveTypes)
|
||||
},
|
||||
...Object.keys(imports.localTypes).map((moduleName): { file: string; types: string[] } => ({
|
||||
file: `@polkadot/types/interfaces/${moduleName}`,
|
||||
file: `${imports.moduleToPackage[moduleName]}/${moduleName}`,
|
||||
types: Object.keys(imports.localTypes[moduleName])
|
||||
}))
|
||||
]);
|
||||
@@ -280,19 +186,30 @@ function generateTsDefFor (defName: string, { types }: { types: Record<string, a
|
||||
}
|
||||
});
|
||||
|
||||
fs.writeFileSync(`packages/types/src/interfaces/${defName}/types.ts`, header.concat(sortedDefs).concat(FOOTER), { flag: 'w' });
|
||||
fs.writeFileSync(`packages/types/src/interfaces/${defName}/index.ts`, HEADER.concat('export * from \'./types\';').concat(FOOTER), { flag: 'w' });
|
||||
fs.writeFileSync(path.join(outputDir, defName, 'types.ts'), header.concat(sortedDefs).concat(FOOTER), { flag: 'w' });
|
||||
fs.writeFileSync(path.join(outputDir, defName, 'index.ts'), HEADER.concat('export * from \'./types\';').concat(FOOTER), { flag: 'w' });
|
||||
}
|
||||
|
||||
export default function generateTsDef (): void {
|
||||
export function generateTsDef (importDefinitions: { [importPath: string]: object }, outputDir: string, generatingPackage: string): void {
|
||||
const definitions = importDefinitions[generatingPackage];
|
||||
Object.entries(definitions).forEach(([defName, obj]): void => {
|
||||
console.log(`Extracting interfaces for ${defName}`);
|
||||
|
||||
generateTsDefFor(defName, obj);
|
||||
generateTsDefFor(importDefinitions, defName, obj, outputDir);
|
||||
});
|
||||
|
||||
console.log('Writing interfaces/types.ts');
|
||||
console.log(`Writing ${outputDir}`);
|
||||
|
||||
fs.writeFileSync('packages/types/src/interfaces/types.ts', HEADER.concat(Object.keys(definitions).map((moduleName): string => `export * from './${moduleName}/types';`).join('\n')).concat(FOOTER), { flag: 'w' });
|
||||
fs.writeFileSync('packages/types/src/interfaces/index.ts', HEADER.concat('export * from \'./types\';').concat(FOOTER), { flag: 'w' });
|
||||
fs.writeFileSync(path.join(outputDir, 'types.ts'), HEADER.concat(Object.keys(definitions).map((moduleName): string => `export * from './${moduleName}/types';`).join('\n')).concat(FOOTER), { flag: 'w' });
|
||||
fs.writeFileSync(path.join(outputDir, 'index.ts'), HEADER.concat('export * from \'./types\';').concat(FOOTER), { flag: 'w' });
|
||||
}
|
||||
|
||||
export default function generateTsDefDefault (): void {
|
||||
generateTsDef(
|
||||
{
|
||||
'@polkadot/types/interfaces': defaultDefinitions
|
||||
},
|
||||
'packages/types/src/interfaces',
|
||||
'@polkadot/types/interfaces'
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2,48 +2,64 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { TypeDef } from '../../codec/types';
|
||||
import { TypeDef, TypeDefInfo } from '../../codec/types';
|
||||
import { Constructor, Registry } from '../../types';
|
||||
|
||||
import { isChildClass, isCompactEncodable } from './class';
|
||||
import { ClassOfUnsafe, getTypeDef } from '../../codec/create';
|
||||
import AbstractInt from '../../codec/AbstractInt';
|
||||
import Vec from '../../codec/Vec';
|
||||
import { formatCompact, formatOption, formatVec } from './formatting';
|
||||
import { formatType } from './formatting';
|
||||
import { setImports, TypeImports } from './imports';
|
||||
import * as primitiveClasses from '../../primitive';
|
||||
|
||||
// From `T`, generate `Compact<T>, Option<T>, Vec<T>`
|
||||
export function getDerivedTypes (type: string, primitiveName: string, imports: TypeImports): string[] {
|
||||
export function getDerivedTypes (definitions: object, type: string, primitiveName: string, imports: TypeImports): string[] {
|
||||
// `primitiveName` represents the actual primitive type our type is mapped to
|
||||
const isCompact = isCompactEncodable((primitiveClasses as any)[primitiveName]);
|
||||
const def = getTypeDef(type);
|
||||
|
||||
setImports(imports, ['Option', 'Vec', isCompact ? 'Compact' : '']);
|
||||
setImports(definitions, imports, ['Option', 'Vec', isCompact ? 'Compact' : '']);
|
||||
|
||||
return [
|
||||
`${type}: ${type};`,
|
||||
isCompact
|
||||
? `'${formatCompact(type)}': ${formatCompact(type)};`
|
||||
: '',
|
||||
`'${formatOption(type)}': ${formatOption(type)};`,
|
||||
`'${formatVec(type)}': ${formatVec(type)};`
|
||||
]
|
||||
.filter((x): boolean => !!x);
|
||||
const types = [
|
||||
{
|
||||
info: TypeDefInfo.Option,
|
||||
type,
|
||||
sub: def
|
||||
},
|
||||
{
|
||||
info: TypeDefInfo.Vec,
|
||||
type,
|
||||
sub: def
|
||||
}
|
||||
];
|
||||
if (isCompact) {
|
||||
types.unshift({
|
||||
info: TypeDefInfo.Compact,
|
||||
type,
|
||||
sub: def
|
||||
});
|
||||
}
|
||||
|
||||
const result = types.map(t => formatType(definitions, t, imports)).map(t => `'${t}': ${t};`);
|
||||
result.unshift(`${type}: ${type};`);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Make types a little bit more flexible
|
||||
// - if param instanceof AbstractInt, then param: u64 | Uint8array | string | number
|
||||
// etc
|
||||
export function getSimilarTypes (registry: Registry, type: string, imports: TypeImports): string[] {
|
||||
export function getSimilarTypes (definitions: object, registry: Registry, type: string, imports: TypeImports): string[] {
|
||||
const possibleTypes = [type];
|
||||
|
||||
if (type === 'Extrinsic') {
|
||||
setImports(imports, ['IExtrinsic']);
|
||||
setImports(definitions, imports, ['IExtrinsic']);
|
||||
return ['IExtrinsic'];
|
||||
}
|
||||
|
||||
if (isChildClass(Vec, ClassOfUnsafe(registry, type))) {
|
||||
return [`(${getSimilarTypes(registry, ((getTypeDef(type).sub) as TypeDef).type, imports).join(' | ')})[]`];
|
||||
return [`(${getSimilarTypes(definitions, registry, ((getTypeDef(type).sub) as TypeDef).type, imports).join(' | ')})[]`];
|
||||
}
|
||||
|
||||
// FIXME This is a hack, it's hard to correctly type StorageKeys in the
|
||||
|
||||
@@ -2,7 +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 { TypeDef, TypeDefInfo } from '../../codec/types';
|
||||
import { TypeDef, TypeDefInfo, TypeDefExtVecFixed } from '../../codec/types';
|
||||
|
||||
import { getTypeDef } from '../../codec/create';
|
||||
import { paramsNotation } from '../../codec/utils';
|
||||
@@ -37,83 +37,102 @@ export function exportType (name = '', base: string): string {
|
||||
/**
|
||||
* Given the inner `K` & `V`, return a `BTreeMap<K, V>` string
|
||||
*/
|
||||
export function formatBTreeMap (key: string, val: string): string {
|
||||
function formatBTreeMap (key: string, val: string): string {
|
||||
return `BTreeMap<${key}, ${val}>`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given the inner `T`, return a `Compact<T>` string
|
||||
*/
|
||||
export function formatCompact (inner: string): string {
|
||||
function formatCompact (inner: string): string {
|
||||
return paramsNotation('Compact', inner);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given the inner `O` & `E`, return a `Result<O, E>` string
|
||||
*/
|
||||
export function formatResult (innerOk: string, innerError: string): string {
|
||||
function formatResult (innerOk: string, innerError: string): string {
|
||||
return `Result<${innerOk}, ${innerError}>`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given the inner `T`, return a `Option<T>` string
|
||||
*/
|
||||
export function formatOption (inner: string): string {
|
||||
function formatOption (inner: string): string {
|
||||
return paramsNotation('Option', inner);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given the inners `T[]`, return a `ITuple<...T>` string
|
||||
*/
|
||||
export function formatTuple (inners: string[]): string {
|
||||
function formatTuple (inners: string[]): string {
|
||||
return paramsNotation('ITuple', `[${inners.join(', ')}]`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given the inner `T`, return a `Vec<T>` string
|
||||
*/
|
||||
export function formatVec (inner: string): string {
|
||||
function formatVec (inner: string): string {
|
||||
return paramsNotation('Vec', inner);
|
||||
}
|
||||
|
||||
/**
|
||||
* Correctly format a given type
|
||||
*/
|
||||
export function formatType (type: string | TypeDef, imports: TypeImports): string {
|
||||
let typeDef;
|
||||
export function formatType (definitions: object, type: string | TypeDef, imports: TypeImports): string {
|
||||
let typeDef: TypeDef;
|
||||
if (typeof type === 'string') {
|
||||
typeDef = getTypeDef(type);
|
||||
} else {
|
||||
typeDef = type;
|
||||
}
|
||||
|
||||
setImports(definitions, imports, [typeDef.type]);
|
||||
|
||||
switch (typeDef.info) {
|
||||
case TypeDefInfo.Compact: {
|
||||
return formatCompact(formatType((typeDef.sub as TypeDef).type, imports));
|
||||
setImports(definitions, imports, ['Compact']);
|
||||
return formatCompact(formatType(definitions, (typeDef.sub as TypeDef).type, imports));
|
||||
}
|
||||
case TypeDefInfo.Option: {
|
||||
return formatOption(formatType((typeDef.sub as TypeDef).type, imports));
|
||||
setImports(definitions, imports, ['Option']);
|
||||
return formatOption(formatType(definitions, (typeDef.sub as TypeDef).type, imports));
|
||||
}
|
||||
case TypeDefInfo.Plain: {
|
||||
return typeDef.type;
|
||||
}
|
||||
case TypeDefInfo.Vec: {
|
||||
return formatVec(formatType((typeDef.sub as TypeDef).type, imports));
|
||||
setImports(definitions, imports, ['Vec']);
|
||||
return formatVec(formatType(definitions, (typeDef.sub as TypeDef).type, imports));
|
||||
}
|
||||
case TypeDefInfo.Tuple: {
|
||||
setImports(imports, ['ITuple']);
|
||||
setImports(definitions, imports, ['ITuple']);
|
||||
|
||||
// `(a,b)` gets transformed into `ITuple<[a, b]>`
|
||||
return formatTuple(
|
||||
((typeDef.sub as TypeDef[])
|
||||
.map((sub): string => formatType(sub.type, imports)))
|
||||
.map((sub): string => formatType(definitions, sub.type, imports)))
|
||||
);
|
||||
}
|
||||
case TypeDefInfo.VecFixed: {
|
||||
setImports(imports, ['U8a']);
|
||||
if ((typeDef.ext as TypeDefExtVecFixed).type === 'u8') {
|
||||
setImports(definitions, imports, ['U8a']);
|
||||
|
||||
// `[u8, 32]` gets transformed into U8a
|
||||
return 'U8a';
|
||||
// `[u8, 32]` gets transformed into U8a
|
||||
return 'U8a';
|
||||
}
|
||||
setImports(definitions, imports, ['Vec']);
|
||||
return formatVec(formatType(definitions, (typeDef.ext as TypeDefExtVecFixed).type, imports));
|
||||
}
|
||||
case TypeDefInfo.BTreeMap: {
|
||||
setImports(definitions, imports, ['BTreeMap']);
|
||||
const [keyDef, valDef] = (typeDef.sub as TypeDef[]);
|
||||
return formatBTreeMap(formatType(definitions, keyDef.type, imports), formatType(definitions, valDef.type, imports));
|
||||
}
|
||||
case TypeDefInfo.Result: {
|
||||
setImports(definitions, imports, ['Result']);
|
||||
const [okDef, errorDef] = (typeDef.sub as TypeDef[]);
|
||||
return formatResult(formatType(definitions, okDef.type, imports), formatType(definitions, errorDef.type, imports));
|
||||
}
|
||||
default: {
|
||||
throw new Error(`Cannot format ${type}.`);
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
import * as codecClasses from '../../codec';
|
||||
import { getTypeDef } from '../../codec/create';
|
||||
import { TypeDefInfo } from '../../codec/types';
|
||||
import * as definitions from '../../interfaces/definitions';
|
||||
import * as primitiveClasses from '../../primitive';
|
||||
|
||||
// these map all the codec and primitive types for import, see the TypeImports below. If
|
||||
@@ -22,11 +21,14 @@ export interface TypeImports {
|
||||
ignoredTypes: string[]; // No need to import these types
|
||||
primitiveTypes: TypeExist; // `import {} from '@polkadot/types/primitive`
|
||||
typesTypes: TypeExist; // `import {} from '@polkadot/types/types`
|
||||
definitions: object; // all definitions
|
||||
typeToModule: Record<string, string>;
|
||||
moduleToPackage: Record<string, string>;
|
||||
}
|
||||
|
||||
// Maps the types as found to the source location. This is used to generate the
|
||||
// imports in the output file, dep-duped and sorted
|
||||
export function setImports (imports: TypeImports, types: string[]): void {
|
||||
export function setImports (definitions: object, imports: TypeImports, types: string[]): void {
|
||||
const { codecTypes, localTypes, ignoredTypes, primitiveTypes, typesTypes } = imports;
|
||||
|
||||
types.forEach((type): void => {
|
||||
@@ -42,14 +44,14 @@ export function setImports (imports: TypeImports, types: string[]): void {
|
||||
// If the type is a bit special (tuple, fixed u8, nested type...), then we
|
||||
// need to parse it with `getTypeDef`.
|
||||
const typeDef = getTypeDef(type);
|
||||
setImports(imports, [TypeDefInfo[typeDef.info]]);
|
||||
setImports(definitions, imports, [TypeDefInfo[typeDef.info]]);
|
||||
|
||||
// TypeDef.sub is a `TypeDef | TypeDef[]`
|
||||
if (Array.isArray(typeDef.sub)) {
|
||||
typeDef.sub.forEach((subType): void => setImports(imports, [subType.type]));
|
||||
typeDef.sub.forEach((subType): void => setImports(definitions, imports, [subType.type]));
|
||||
} else if (typeDef.sub) {
|
||||
// typeDef.sub is a TypeDef in this case
|
||||
setImports(imports, [typeDef.sub.type]);
|
||||
setImports(definitions, imports, [typeDef.sub.type]);
|
||||
}
|
||||
} else {
|
||||
// find this module inside the exports from the rest
|
||||
@@ -65,7 +67,26 @@ export function setImports (imports: TypeImports, types: string[]): void {
|
||||
}
|
||||
|
||||
// Create an Imports object, can be prefilled with `ignoredTypes`
|
||||
export function createImports ({ types }: { types: Record<string, any> } = { types: {} }): TypeImports {
|
||||
export function createImports (importDefinitions: { [importPath: string]: object }, { types }: { types: Record<string, any> } = { types: {} }): TypeImports {
|
||||
const definitions = {} as Record<string, object>;
|
||||
const typeToModule = {} as Record<string, string>;
|
||||
const moduleToPackage = {} as Record<string, string>;
|
||||
for (const [packagePath, packageDef] of Object.entries(importDefinitions)) {
|
||||
for (const [name, moduleDef] of Object.entries(packageDef)) {
|
||||
if (definitions[name]) {
|
||||
throw new Error(`Duplicated module: ${name}. Packages: ${packagePath}, ${moduleToPackage[name]}`);
|
||||
}
|
||||
definitions[name] = moduleDef;
|
||||
moduleToPackage[name] = packagePath;
|
||||
for (const type of Object.keys(moduleDef.types)) {
|
||||
if (typeToModule[type]) {
|
||||
throw new Error(`Duplicated type: ${type}. Modules: ${name}, ${typeToModule[type]}`);
|
||||
}
|
||||
typeToModule[type] = name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const codecTypes: TypeExist = {};
|
||||
const localTypes: TypeExistMap = Object.keys(definitions).reduce((localTypes: Record<string, TypeExist>, moduleName): Record<string, TypeExist> => {
|
||||
localTypes[moduleName] = {};
|
||||
@@ -76,7 +97,7 @@ export function createImports ({ types }: { types: Record<string, any> } = { typ
|
||||
const primitiveTypes: TypeExist = {};
|
||||
const typesTypes: TypeExist = {};
|
||||
|
||||
const imports = { codecTypes, localTypes, ignoredTypes, primitiveTypes, typesTypes };
|
||||
const imports = { codecTypes, localTypes, ignoredTypes, primitiveTypes, typesTypes, definitions, typeToModule, moduleToPackage };
|
||||
|
||||
return imports;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user