Cleanup some generation duplication (#1797)
* Cleanup some generation duplicaton * Override for Metadata import * Import order in generators
This commit is contained in:
@@ -5,12 +5,11 @@
|
||||
import { ModuleMetadataLatest } from '../../interfaces/metadata';
|
||||
import * as defaultDefs from '../../interfaces/definitions';
|
||||
|
||||
import fs from 'fs';
|
||||
import staticData from '@polkadot/metadata/Metadata/static';
|
||||
import { stringCamelCase } from '@polkadot/util';
|
||||
|
||||
import { Metadata, TypeRegistry } from '../..';
|
||||
import { createDocComments, createImportCode, createImports, FOOTER, HEADER, indent, setImports, TypeImports } from '../util';
|
||||
import { FOOTER, HEADER, TypeImports, createDocComments, createImportCode, createImports, indent, setImports, writeFile } from '../util';
|
||||
|
||||
// Generate types for one module
|
||||
/** @internal */
|
||||
@@ -34,53 +33,33 @@ function generateModule (allDefs: object, { constants, name }: ModuleMetadataLat
|
||||
|
||||
/** @internal */
|
||||
function generateForMeta (meta: Metadata, dest: string, extraTypes: Record<string, Record<string, object>>): void {
|
||||
console.log(`${dest}\n\tGenerating`);
|
||||
writeFile(dest, (): string => {
|
||||
const allTypes: Record<string, Record<string, object>> = { '@polkadot/types/interfaces': defaultDefs, ...extraTypes };
|
||||
const imports = createImports(allTypes);
|
||||
const allDefs = Object.entries(allTypes).reduce((defs, [, obj]) => {
|
||||
return Object.entries(obj).reduce((defs, [key, value]) => ({ ...defs, [key]: value }), defs);
|
||||
}, {});
|
||||
const body = meta.asLatest.modules.reduce((acc, mod): string[] => {
|
||||
return acc.concat(generateModule(allDefs, mod, imports));
|
||||
}, [] as string[]);
|
||||
const header = createImportCode(HEADER, imports, [
|
||||
...Object.keys(imports.localTypes).map((moduleName): { file: string; types: string[] } => ({
|
||||
file: `${imports.moduleToPackage[moduleName]}/${moduleName}`,
|
||||
types: Object.keys(imports.localTypes[moduleName])
|
||||
}))
|
||||
]);
|
||||
const interfaceStart = [
|
||||
"declare module '@polkadot/metadata/Decorated/types' {",
|
||||
indent(2)('export interface Constants {\n')
|
||||
].join('\n');
|
||||
const interfaceEnd = `\n${indent(2)('}')}\n}`;
|
||||
|
||||
const allTypes: Record<string, Record<string, object>> = { '@polkadot/types/interfaces': defaultDefs, ...extraTypes };
|
||||
const imports = createImports(allTypes);
|
||||
const allDefs = Object.entries(allTypes).reduce((defs, [, obj]) => {
|
||||
return Object.entries(obj).reduce((defs, [key, value]) => ({ ...defs, [key]: value }), defs);
|
||||
}, {});
|
||||
const body = meta.asLatest.modules.reduce((acc, mod): string[] => {
|
||||
return acc.concat(generateModule(allDefs, mod, imports));
|
||||
}, [] as string[]);
|
||||
const header = createImportCode(HEADER, [
|
||||
{
|
||||
file: '@polkadot/types/codec',
|
||||
types: Object.keys(imports.codecTypes).filter((name): boolean => name !== 'Tuple')
|
||||
},
|
||||
{
|
||||
file: '@polkadot/types',
|
||||
types: Object.keys(imports.primitiveTypes)
|
||||
},
|
||||
...Object.keys(imports.localTypes).map((moduleName): { file: string; types: string[] } => ({
|
||||
file: `${imports.moduleToPackage[moduleName]}/${moduleName}`,
|
||||
types: Object.keys(imports.localTypes[moduleName])
|
||||
})),
|
||||
{
|
||||
file: '@polkadot/types/types',
|
||||
types: Object.keys(imports.typesTypes)
|
||||
}
|
||||
]);
|
||||
const interfaceStart = [
|
||||
"declare module '@polkadot/metadata/Decorated/types' {",
|
||||
indent(2)('export interface Constants {\n')
|
||||
].join('\n');
|
||||
const interfaceEnd = `\n${indent(2)('}')}\n}`;
|
||||
|
||||
console.log('\tWriting');
|
||||
|
||||
fs.writeFileSync(
|
||||
dest,
|
||||
header
|
||||
return header
|
||||
.concat(interfaceStart)
|
||||
.concat(body.join('\n'))
|
||||
.concat(interfaceEnd)
|
||||
.concat(FOOTER)
|
||||
, { flag: 'w' }
|
||||
);
|
||||
|
||||
console.log('');
|
||||
.concat(FOOTER);
|
||||
});
|
||||
}
|
||||
|
||||
// Call `generateForMeta()` with current static metadata
|
||||
|
||||
@@ -2,12 +2,10 @@
|
||||
// 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 Raw from '../../codec/Raw';
|
||||
import * as defaultDefinitions from '../../interfaces/definitions';
|
||||
import * as defaultPrimitives from '../../primitive';
|
||||
import { FOOTER, HEADER, createImportCode, createImports, getDerivedTypes, indent, setImports } from '../util';
|
||||
import { FOOTER, HEADER, createImportCode, createImports, getDerivedTypes, indent, setImports, writeFile } from '../util';
|
||||
|
||||
const primitiveClasses = {
|
||||
...defaultPrimitives,
|
||||
@@ -15,65 +13,54 @@ const primitiveClasses = {
|
||||
};
|
||||
|
||||
/** @internal */
|
||||
export function generateInterfaceRegistry (importDefinitions: { [importPath: string]: object }, output: string): void {
|
||||
console.log(`${output}\n\tGenerating`);
|
||||
export function generateInterfaceRegistry (importDefinitions: { [importPath: string]: object }, dest: string): void {
|
||||
writeFile(dest, (): string => {
|
||||
Object.entries(importDefinitions).reduce((acc, def) => Object.assign(acc, def), {} as object);
|
||||
|
||||
Object.entries(importDefinitions).reduce((acc, def) => Object.assign(acc, def), {} as object);
|
||||
const imports = createImports(importDefinitions);
|
||||
const definitions = imports.definitions;
|
||||
|
||||
const imports = createImports(importDefinitions);
|
||||
const definitions = imports.definitions;
|
||||
const primitives = Object
|
||||
.keys(primitiveClasses)
|
||||
.filter((name): boolean => !!name.indexOf('Generic'))
|
||||
.reduce((accumulator, primitiveName): string => {
|
||||
setImports(definitions, imports, [primitiveName]);
|
||||
|
||||
const primitives = Object
|
||||
.keys(primitiveClasses)
|
||||
.filter((name): boolean => !!name.indexOf('Generic'))
|
||||
.reduce((accumulator, primitiveName): string => {
|
||||
setImports(definitions, imports, [primitiveName]);
|
||||
return [
|
||||
accumulator,
|
||||
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(definitions, imports, Object.keys(types));
|
||||
|
||||
return [
|
||||
accumulator,
|
||||
getDerivedTypes(definitions, primitiveName, primitiveName, imports).map(indent(2)).join('\n')
|
||||
...Object.keys(types).map((type): string =>
|
||||
getDerivedTypes(definitions, type, types[type], 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(definitions, imports, Object.keys(types));
|
||||
const header = createImportCode(HEADER, imports, [
|
||||
...Object.keys(imports.localTypes).map((moduleName): { file: string; types: string[] } => ({
|
||||
file: `${imports.moduleToPackage[moduleName]}/${moduleName}`,
|
||||
types: Object.keys(imports.localTypes[moduleName])
|
||||
}))
|
||||
]);
|
||||
|
||||
return [
|
||||
accumulator,
|
||||
...Object.keys(types).map((type): string =>
|
||||
getDerivedTypes(definitions, type, types[type], imports).map(indent(2)).join('\n')
|
||||
)
|
||||
].join('\n');
|
||||
}, '');
|
||||
const interfaceStart = 'export interface InterfaceRegistry {';
|
||||
const interfaceEnd = '\n}';
|
||||
|
||||
const header = createImportCode(HEADER, [
|
||||
{
|
||||
file: '@polkadot/types/codec',
|
||||
types: Object.keys(imports.codecTypes).filter((name): boolean => name !== 'Tuple')
|
||||
},
|
||||
{
|
||||
file: '@polkadot/types/primitive',
|
||||
types: Object.keys(imports.primitiveTypes)
|
||||
},
|
||||
...Object.keys(imports.localTypes).map((moduleName): { file: string; types: string[] } => ({
|
||||
file: `${imports.moduleToPackage[moduleName]}/${moduleName}`,
|
||||
types: Object.keys(imports.localTypes[moduleName])
|
||||
}))
|
||||
]);
|
||||
|
||||
const interfaceStart = 'export interface InterfaceRegistry {';
|
||||
const interfaceEnd = '\n}';
|
||||
|
||||
console.log('\tWriting');
|
||||
|
||||
fs.writeFileSync(
|
||||
output,
|
||||
header.concat(interfaceStart).concat(primitives).concat(srml).concat(interfaceEnd).concat(FOOTER)
|
||||
, { flag: 'w' }
|
||||
);
|
||||
|
||||
console.log('');
|
||||
return header
|
||||
.concat(interfaceStart)
|
||||
.concat(primitives)
|
||||
.concat(srml)
|
||||
.concat(interfaceEnd)
|
||||
.concat(FOOTER);
|
||||
});
|
||||
}
|
||||
|
||||
// Generate `packages/types/src/interfaceRegistry.ts`, the registry of all interfaces
|
||||
|
||||
@@ -6,13 +6,12 @@ import { ModuleMetadataLatest, StorageEntryMetadataLatest } from '../../interfac
|
||||
import { Registry } from '../../types';
|
||||
import * as defaultDefs from '../../interfaces/definitions';
|
||||
|
||||
import fs from 'fs';
|
||||
import staticData from '@polkadot/metadata/Metadata/static';
|
||||
import { stringLowerFirst } from '@polkadot/util';
|
||||
|
||||
import { Metadata } from '../..';
|
||||
import { TypeRegistry } from '../../codec';
|
||||
import { FOOTER, HEADER, TypeImports, createDocComments, createImportCode, createImports, formatType, getSimilarTypes, indent, setImports } from '../util';
|
||||
import { FOOTER, HEADER, TypeImports, createDocComments, createImportCode, createImports, formatType, getSimilarTypes, indent, setImports, writeFile } from '../util';
|
||||
|
||||
// If the StorageEntry returns T, output `Option<T>` if the modifier is optional
|
||||
/** @internal */
|
||||
@@ -87,57 +86,37 @@ function generateModule (allDefs: object, registry: Registry, { name, storage }:
|
||||
|
||||
/** @internal */
|
||||
function generateForMeta (registry: Registry, meta: Metadata, dest: string, extraTypes: Record<string, Record<string, object>>): void {
|
||||
console.log(`${dest}\n\tGenerating`);
|
||||
writeFile(dest, (): string => {
|
||||
const allTypes: Record<string, Record<string, object>> = { '@polkadot/types/interfaces': defaultDefs, ...extraTypes };
|
||||
const imports = createImports(allTypes);
|
||||
const allDefs = Object.entries(allTypes).reduce((defs, [, obj]) => {
|
||||
return Object.entries(obj).reduce((defs, [key, value]) => ({ ...defs, [key]: value }), defs);
|
||||
}, {});
|
||||
const body = meta.asLatest.modules.reduce((acc, mod): string[] => {
|
||||
return acc.concat(generateModule(allDefs, registry, mod, imports));
|
||||
}, [] as string[]);
|
||||
const header = createImportCode(HEADER, imports, [
|
||||
...Object.keys(imports.localTypes).map((moduleName): { file: string; types: string[] } => ({
|
||||
file: `${imports.moduleToPackage[moduleName]}/${moduleName}`,
|
||||
types: Object.keys(imports.localTypes[moduleName])
|
||||
})),
|
||||
{
|
||||
file: 'rxjs',
|
||||
types: ['Observable']
|
||||
}
|
||||
]);
|
||||
const interfaceStart = [
|
||||
"declare module '@polkadot/api/types/storage' {",
|
||||
indent(2)('export interface AugmentedQueries<ApiType> {\n')
|
||||
].join('\n');
|
||||
const interfaceEnd = `\n${indent(2)('}')}\n}`;
|
||||
|
||||
const allTypes: Record<string, Record<string, object>> = { '@polkadot/types/interfaces': defaultDefs, ...extraTypes };
|
||||
const imports = createImports(allTypes);
|
||||
const allDefs = Object.entries(allTypes).reduce((defs, [, obj]) => {
|
||||
return Object.entries(obj).reduce((defs, [key, value]) => ({ ...defs, [key]: value }), defs);
|
||||
}, {});
|
||||
const body = meta.asLatest.modules.reduce((acc, mod): string[] => {
|
||||
return acc.concat(generateModule(allDefs, registry, mod, imports));
|
||||
}, [] as string[]);
|
||||
const header = createImportCode(HEADER, [
|
||||
{
|
||||
file: 'rxjs',
|
||||
types: ['Observable']
|
||||
},
|
||||
{
|
||||
file: '@polkadot/types/codec',
|
||||
types: Object.keys(imports.codecTypes).filter((name): boolean => name !== 'Tuple')
|
||||
},
|
||||
{
|
||||
file: '@polkadot/types',
|
||||
types: Object.keys(imports.primitiveTypes)
|
||||
},
|
||||
...Object.keys(imports.localTypes).map((moduleName): { file: string; types: string[] } => ({
|
||||
file: `${imports.moduleToPackage[moduleName]}/${moduleName}`,
|
||||
types: Object.keys(imports.localTypes[moduleName])
|
||||
})),
|
||||
{
|
||||
file: '@polkadot/types/types',
|
||||
types: Object.keys(imports.typesTypes)
|
||||
}
|
||||
]);
|
||||
const interfaceStart = [
|
||||
"declare module '@polkadot/api/types/storage' {",
|
||||
indent(2)('export interface AugmentedQueries<ApiType> {\n')
|
||||
].join('\n');
|
||||
const interfaceEnd = `\n${indent(2)('}')}\n}`;
|
||||
|
||||
console.log('\tWriting');
|
||||
|
||||
fs.writeFileSync(
|
||||
dest,
|
||||
header
|
||||
return header
|
||||
.concat(interfaceStart)
|
||||
.concat(body.join('\n'))
|
||||
.concat(interfaceEnd)
|
||||
.concat(FOOTER)
|
||||
, { flag: 'w' }
|
||||
);
|
||||
|
||||
console.log('');
|
||||
.concat(FOOTER);
|
||||
});
|
||||
}
|
||||
|
||||
// Call `generateForMeta()` with current static metadata
|
||||
|
||||
@@ -2,86 +2,68 @@
|
||||
// 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 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';
|
||||
import { FOOTER, HEADER, createImportCode, createImports, getSimilarTypes, setImports, writeFile } from '../util';
|
||||
|
||||
/** @internal */
|
||||
export default function generateRpcTypes (dest = 'packages/api/src/types/augment/rpc.ts'): void {
|
||||
console.log(`${dest}\n\tGenerating`);
|
||||
writeFile(dest, (): string => {
|
||||
const registry = new TypeRegistry();
|
||||
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];
|
||||
|
||||
const registry = new TypeRegistry();
|
||||
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(definitions, 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(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>;';
|
||||
// FIXME These 2 are too hard to type, I give up
|
||||
if (section === 'state') {
|
||||
if (method.method === 'getStorage') {
|
||||
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>;';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const args = method.params.map((param): string => {
|
||||
const similarTypes = getSimilarTypes(definitions, registry, param.type, imports);
|
||||
setImports(definitions, imports, [param.type, ...similarTypes]);
|
||||
const args = method.params.map((param): string => {
|
||||
const similarTypes = getSimilarTypes(definitions, registry, param.type, imports);
|
||||
setImports(definitions, imports, [param.type, ...similarTypes]);
|
||||
|
||||
return `${param.name}${param.isOptional ? '?' : ''}: ${similarTypes.join(' | ')}`;
|
||||
return `${param.name}${param.isOptional ? '?' : ''}: ${similarTypes.join(' | ')}`;
|
||||
});
|
||||
|
||||
return ` /**\n * ${method.description}\n **/\n ${method.method}(${args.join(', ')}): Observable<${method.type}>;`;
|
||||
});
|
||||
|
||||
return ` /**\n * ${method.description}\n **/\n ${method.method}(${args.join(', ')}): Observable<${method.type}>;`;
|
||||
});
|
||||
return allSections.concat(
|
||||
[
|
||||
` ${section}: {`,
|
||||
...allMethods,
|
||||
' };'
|
||||
].join('\n')
|
||||
);
|
||||
}, []).join('\n');
|
||||
|
||||
return allSections.concat(
|
||||
[
|
||||
` ${section}: {`,
|
||||
...allMethods,
|
||||
' };'
|
||||
].join('\n')
|
||||
);
|
||||
}, []).join('\n');
|
||||
const header = createImportCode(HEADER, imports, [
|
||||
...Object.keys(imports.localTypes).map((moduleName): { file: string; types: string[] } => ({
|
||||
file: `@polkadot/types/interfaces/${moduleName}`,
|
||||
types: Object.keys(imports.localTypes[moduleName])
|
||||
})),
|
||||
{
|
||||
file: 'rxjs',
|
||||
types: ['Observable']
|
||||
}
|
||||
]);
|
||||
const interfaceStart = "declare module '@polkadot/rpc-core/types.jsonrpc' {\n export interface RpcInterface {\n";
|
||||
const interfaceEnd = '\n }\n}';
|
||||
|
||||
const header = createImportCode(HEADER, [
|
||||
{
|
||||
file: 'rxjs',
|
||||
types: ['Observable']
|
||||
},
|
||||
{
|
||||
file: '@polkadot/types/codec',
|
||||
types: Object.keys(imports.codecTypes).filter((name): boolean => name !== 'Tuple')
|
||||
},
|
||||
{
|
||||
file: '@polkadot/types',
|
||||
types: Object.keys(imports.primitiveTypes)
|
||||
},
|
||||
...Object.keys(imports.localTypes).map((moduleName): { file: string; types: string[] } => ({
|
||||
file: `@polkadot/types/interfaces/${moduleName}`,
|
||||
types: Object.keys(imports.localTypes[moduleName])
|
||||
})),
|
||||
{
|
||||
file: '@polkadot/types/types',
|
||||
types: Object.keys(imports.typesTypes)
|
||||
}
|
||||
]);
|
||||
const interfaceStart = "declare module '@polkadot/rpc-core/types.jsonrpc' {\n export interface RpcInterface {\n";
|
||||
const interfaceEnd = '\n }\n}';
|
||||
|
||||
console.log('\tWriting');
|
||||
|
||||
fs.writeFileSync(
|
||||
dest,
|
||||
header.concat(interfaceStart).concat(body).concat(interfaceEnd).concat(FOOTER)
|
||||
, { flag: 'w' }
|
||||
);
|
||||
|
||||
console.log('');
|
||||
return header
|
||||
.concat(interfaceStart)
|
||||
.concat(body)
|
||||
.concat(interfaceEnd)
|
||||
.concat(FOOTER);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import { isString, stringCamelCase, stringUpperFirst } from '@polkadot/util';
|
||||
|
||||
import { getTypeDef } from '../../codec/create';
|
||||
import * as defaultDefinitions from '../../interfaces/definitions';
|
||||
import { createImportCode, createImports, exportInterface, exportType, formatType, FOOTER, HEADER, setImports, TypeImports } from '../util';
|
||||
import { FOOTER, HEADER, TypeImports, createImportCode, createImports, exportInterface, exportType, formatType, setImports, writeFile } from '../util';
|
||||
|
||||
interface Imports extends TypeImports {
|
||||
interfaces: [string, string][];
|
||||
@@ -175,19 +175,7 @@ function generateTsDefFor (importDefinitions: { [importPath: string]: object },
|
||||
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, [
|
||||
{
|
||||
file: '@polkadot/types/types',
|
||||
types: Object.keys(imports.typesTypes)
|
||||
},
|
||||
{
|
||||
file: '@polkadot/types/codec',
|
||||
types: Object.keys(imports.codecTypes).filter((name): boolean => name !== 'Tuple')
|
||||
},
|
||||
{
|
||||
file: '@polkadot/types/primitive',
|
||||
types: Object.keys(imports.primitiveTypes)
|
||||
},
|
||||
const header = createImportCode(HEADER, imports, [
|
||||
...Object.keys(imports.localTypes).map((moduleName): { file: string; types: string[] } => ({
|
||||
file: `${imports.moduleToPackage[moduleName]}/${moduleName}`,
|
||||
types: Object.keys(imports.localTypes[moduleName])
|
||||
@@ -200,22 +188,26 @@ function generateTsDefFor (importDefinitions: { [importPath: string]: object },
|
||||
|
||||
/** @internal */
|
||||
export function generateTsDef (importDefinitions: { [importPath: string]: object }, outputDir: string, generatingPackage: string): void {
|
||||
console.log(`${outputDir}\n\tGenerating`);
|
||||
writeFile(path.join(outputDir, 'types.ts'), (): string => {
|
||||
const definitions = importDefinitions[generatingPackage];
|
||||
|
||||
const definitions = importDefinitions[generatingPackage];
|
||||
Object.entries(definitions).forEach(([defName, obj]): void => {
|
||||
console.log(`\tExtracting interfaces for ${defName}`);
|
||||
|
||||
Object.entries(definitions).forEach(([defName, obj]): void => {
|
||||
console.log(`\tExtracting interfaces for ${defName}`);
|
||||
generateTsDefFor(importDefinitions, defName, obj, outputDir);
|
||||
});
|
||||
|
||||
generateTsDefFor(importDefinitions, defName, obj, outputDir);
|
||||
return HEADER
|
||||
.concat(
|
||||
Object
|
||||
.keys(definitions)
|
||||
.map((moduleName): string => `export * from './${moduleName}/types';`)
|
||||
.join('\n')
|
||||
)
|
||||
.concat(FOOTER);
|
||||
});
|
||||
|
||||
console.log('\tWriting');
|
||||
|
||||
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' });
|
||||
|
||||
console.log('');
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
|
||||
@@ -6,13 +6,12 @@ import { ModuleMetadataLatest } from '../../interfaces/metadata';
|
||||
import * as defaultDefs from '../../interfaces/definitions';
|
||||
import { Registry } from '../../types';
|
||||
|
||||
import fs from 'fs';
|
||||
import staticData from '@polkadot/metadata/Metadata/static';
|
||||
import { Text } from '@polkadot/types';
|
||||
import { stringCamelCase } from '@polkadot/util';
|
||||
|
||||
import { Metadata, TypeRegistry } from '../..';
|
||||
import { FOOTER, HEADER, TypeImports, createDocComments, createImportCode, createImports, formatType, getSimilarTypes, indent, setImports } from '../util';
|
||||
import { FOOTER, HEADER, TypeImports, createDocComments, createImportCode, createImports, formatType, getSimilarTypes, indent, setImports, writeFile } from '../util';
|
||||
|
||||
const MAPPED_NAMES: Record<string, string> = {
|
||||
new: 'updated'
|
||||
@@ -62,57 +61,37 @@ function generateModule (registry: Registry, allDefs: object, { calls, name }: M
|
||||
|
||||
/** @internal */
|
||||
function generateForMeta (registry: Registry, meta: Metadata, dest: string, extraTypes: Record<string, Record<string, object>>): void {
|
||||
console.log(`${dest}\n\tGenerating`);
|
||||
writeFile(dest, (): string => {
|
||||
const allTypes: Record<string, Record<string, object>> = { '@polkadot/types/interfaces': defaultDefs, ...extraTypes };
|
||||
const imports = createImports(allTypes);
|
||||
const allDefs = Object.entries(allTypes).reduce((defs, [, obj]) => {
|
||||
return Object.entries(obj).reduce((defs, [key, value]) => ({ ...defs, [key]: value }), defs);
|
||||
}, {});
|
||||
const body = meta.asLatest.modules.reduce((acc, mod): string[] => {
|
||||
return acc.concat(generateModule(registry, allDefs, mod, imports));
|
||||
}, [] as string[]);
|
||||
const header = createImportCode(HEADER, imports, [
|
||||
...Object.keys(imports.localTypes).map((moduleName): { file: string; types: string[] } => ({
|
||||
file: `${imports.moduleToPackage[moduleName]}/${moduleName}`,
|
||||
types: Object.keys(imports.localTypes[moduleName])
|
||||
})),
|
||||
{
|
||||
file: '@polkadot/api/submittable/types',
|
||||
types: ['SubmittableExtrinsic']
|
||||
}
|
||||
]);
|
||||
const interfaceStart = [
|
||||
"declare module '@polkadot/api/types/submittable' {",
|
||||
indent(2)('export interface AugmentedSubmittables<ApiType> {\n')
|
||||
].join('\n');
|
||||
const interfaceEnd = `\n${indent(2)('}')}\n}`;
|
||||
|
||||
const allTypes: Record<string, Record<string, object>> = { '@polkadot/types/interfaces': defaultDefs, ...extraTypes };
|
||||
const imports = createImports(allTypes);
|
||||
const allDefs = Object.entries(allTypes).reduce((defs, [, obj]) => {
|
||||
return Object.entries(obj).reduce((defs, [key, value]) => ({ ...defs, [key]: value }), defs);
|
||||
}, {});
|
||||
const body = meta.asLatest.modules.reduce((acc, mod): string[] => {
|
||||
return acc.concat(generateModule(registry, allDefs, mod, imports));
|
||||
}, [] as string[]);
|
||||
const header = createImportCode(HEADER, [
|
||||
{
|
||||
file: '@polkadot/api/submittable/types',
|
||||
types: ['SubmittableExtrinsic']
|
||||
},
|
||||
{
|
||||
file: '@polkadot/types/codec',
|
||||
types: Object.keys(imports.codecTypes).filter((name): boolean => name !== 'Tuple')
|
||||
},
|
||||
{
|
||||
file: '@polkadot/types',
|
||||
types: Object.keys(imports.primitiveTypes)
|
||||
},
|
||||
...Object.keys(imports.localTypes).map((moduleName): { file: string; types: string[] } => ({
|
||||
file: `${imports.moduleToPackage[moduleName]}/${moduleName}`,
|
||||
types: Object.keys(imports.localTypes[moduleName])
|
||||
})),
|
||||
{
|
||||
file: '@polkadot/types/types',
|
||||
types: Object.keys(imports.typesTypes)
|
||||
}
|
||||
]);
|
||||
const interfaceStart = [
|
||||
"declare module '@polkadot/api/types/submittable' {",
|
||||
indent(2)('export interface AugmentedSubmittables<ApiType> {\n')
|
||||
].join('\n');
|
||||
const interfaceEnd = `\n${indent(2)('}')}\n}`;
|
||||
|
||||
console.log('\tWriting');
|
||||
|
||||
fs.writeFileSync(
|
||||
dest,
|
||||
header
|
||||
return header
|
||||
.concat(interfaceStart)
|
||||
.concat(body.join('\n'))
|
||||
.concat(interfaceEnd)
|
||||
.concat(FOOTER)
|
||||
, { flag: 'w' }
|
||||
);
|
||||
|
||||
console.log('');
|
||||
.concat(FOOTER);
|
||||
});
|
||||
}
|
||||
|
||||
// Call `generateForMeta()` with current static metadata
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
// Copyright 2017-2020 @polkadot/types authors & contributors
|
||||
// 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';
|
||||
|
||||
export function writeFile (dest: string, generator: () => string): void {
|
||||
console.log(`${dest}\n\tGenerating`);
|
||||
|
||||
const generated = generator();
|
||||
|
||||
console.log('\tWriting');
|
||||
|
||||
fs.writeFileSync(dest, generated, { flag: 'w' });
|
||||
|
||||
console.log('');
|
||||
}
|
||||
@@ -11,15 +11,39 @@ import { setImports, TypeImports } from './imports';
|
||||
export const HEADER = '// Auto-generated via `yarn polkadot-types-from-defs`, do not edit\n/* eslint-disable @typescript-eslint/no-empty-interface */\n\n';
|
||||
export const FOOTER = '\n';
|
||||
|
||||
const TYPES_NON_PRIMITIVE = ['Metadata'];
|
||||
|
||||
// creates the import lines
|
||||
/** @internal */
|
||||
export function createImportCode (header: string, checks: { file: string; types: string[] }[]): string {
|
||||
return checks.reduce((result, { file, types }): string => {
|
||||
if (types.length) {
|
||||
result += `import { ${types.sort().join(', ')} } from '${file}';\n`;
|
||||
}
|
||||
|
||||
return result;
|
||||
export function createImportCode (header: string, imports: TypeImports, checks: { file: string; types: string[] }[]): string {
|
||||
return [
|
||||
{
|
||||
file: '@polkadot/types/types',
|
||||
types: Object.keys(imports.typesTypes)
|
||||
},
|
||||
{
|
||||
file: '@polkadot/types/codec',
|
||||
types: Object
|
||||
.keys(imports.codecTypes)
|
||||
.filter((name): boolean => name !== 'Tuple')
|
||||
},
|
||||
{
|
||||
file: '@polkadot/types/primitive',
|
||||
types: Object
|
||||
.keys(imports.primitiveTypes)
|
||||
.filter((name): boolean => !TYPES_NON_PRIMITIVE.includes(name))
|
||||
},
|
||||
{
|
||||
file: '@polkadot/types',
|
||||
types: Object
|
||||
.keys(imports.primitiveTypes)
|
||||
.filter((name): boolean => TYPES_NON_PRIMITIVE.includes(name))
|
||||
},
|
||||
...checks
|
||||
].reduce((result, { file, types }): string => {
|
||||
return types.length
|
||||
? `${result}import { ${types.sort().join(', ')} } from '${file}';\n`
|
||||
: result;
|
||||
}, header) + '\n';
|
||||
}
|
||||
|
||||
|
||||
@@ -4,5 +4,6 @@
|
||||
|
||||
export * from './derived';
|
||||
export * from './docs';
|
||||
export * from './file';
|
||||
export * from './formatting';
|
||||
export * from './imports';
|
||||
|
||||
Reference in New Issue
Block a user