Replace require(...) with import(...) in typegen (#5495)
* Replace require(...) with import(...) in typegen * Update metadata * Adjust & actual run * Bump API * Rebuild * Bump build
This commit is contained in:
@@ -23,11 +23,11 @@
|
||||
"version": "9.14.3-4-x",
|
||||
"main": "index.js",
|
||||
"bin": {
|
||||
"polkadot-types-chain-info": "./scripts/polkadot-types-chain-info.cjs",
|
||||
"polkadot-types-from-chain": "./scripts/polkadot-types-from-chain.cjs",
|
||||
"polkadot-types-from-defs": "./scripts/polkadot-types-from-defs.cjs",
|
||||
"polkadot-types-internal-interfaces": "./scripts/polkadot-types-internal-interfaces.cjs",
|
||||
"polkadot-types-internal-metadata": "./scripts/polkadot-types-internal-metadata.cjs"
|
||||
"polkadot-types-chain-info": "./scripts/polkadot-types-chain-info.mjs",
|
||||
"polkadot-types-from-chain": "./scripts/polkadot-types-from-chain.mjs",
|
||||
"polkadot-types-from-defs": "./scripts/polkadot-types-from-defs.mjs",
|
||||
"polkadot-types-internal-interfaces": "./scripts/polkadot-types-internal-interfaces.mjs",
|
||||
"polkadot-types-internal-metadata": "./scripts/polkadot-types-internal-metadata.mjs"
|
||||
},
|
||||
"dependencies": {
|
||||
"@babel/core": "^7.21.0",
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
// Copyright 2017-2023 @polkadot/typegen authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
module.exports = function (file) {
|
||||
let main;
|
||||
|
||||
try {
|
||||
main = require(`../cjs/${file}.js`).main;
|
||||
} catch (error) {
|
||||
// required for dev-only setups
|
||||
process.env.JEST_WORKER_ID = '123';
|
||||
|
||||
// register aliases
|
||||
require('@babel/register')({
|
||||
extensions: ['.js', '.ts'],
|
||||
plugins: [
|
||||
['module-resolver', {
|
||||
alias: {
|
||||
'^@polkadot/api-(augment|base|derive)(.*)': './packages/api-\\1/src\\2',
|
||||
// eslint-disable-next-line sort-keys
|
||||
'^@polkadot/api(.*)': './packages/api/src/\\1',
|
||||
'^@polkadot/rpc-(augment|core|provider)(.*)': './packages/rpc-\\1/src\\2',
|
||||
'^@polkadot/types-(augment|codec|create|known|metadata|support)(.*)': './packages/types-\\1/src\\2',
|
||||
// eslint-disable-next-line sort-keys
|
||||
'^@polkadot/types(.*)': './packages/types/src\\1'
|
||||
}
|
||||
}]
|
||||
]
|
||||
});
|
||||
|
||||
// get ts source
|
||||
main = require(`../src/${file}.ts`).main;
|
||||
}
|
||||
|
||||
main();
|
||||
};
|
||||
+3
-1
@@ -2,4 +2,6 @@
|
||||
// Copyright 2017-2023 @polkadot/typegen authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
require('./execWithSrc.cjs')('fromDefs');
|
||||
import { main } from '@polkadot/typegen/extractChain';
|
||||
|
||||
main();
|
||||
+3
-1
@@ -2,4 +2,6 @@
|
||||
// Copyright 2017-2023 @polkadot/typegen authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
require('./execWithSrc.cjs')('extractChain');
|
||||
import { main } from '@polkadot/typegen/fromChain';
|
||||
|
||||
main();
|
||||
+3
-1
@@ -2,4 +2,6 @@
|
||||
// Copyright 2017-2023 @polkadot/typegen authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
require('./execWithSrc.cjs')('interfacesTs');
|
||||
import { main } from '@polkadot/typegen/fromDefs';
|
||||
|
||||
main();
|
||||
@@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env node
|
||||
// Copyright 2017-2023 @polkadot/typegen authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { main } from '@polkadot/typegen/interfacesTs';
|
||||
|
||||
main();
|
||||
@@ -1,5 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
// Copyright 2017-2023 @polkadot/typegen authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
require('./execWithSrc.cjs')('metadataMd');
|
||||
+3
-1
@@ -2,4 +2,6 @@
|
||||
// Copyright 2017-2023 @polkadot/typegen authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
require('./execWithSrc.cjs')('fromChain');
|
||||
import { main } from '@polkadot/typegen/metadataMd';
|
||||
|
||||
main();
|
||||
@@ -6,13 +6,13 @@ import type { HexString } from '@polkadot/util/types';
|
||||
import path from 'path';
|
||||
import yargs from 'yargs';
|
||||
|
||||
import { Definitions } from '@polkadot/types/types';
|
||||
import { Definitions, DefinitionsTypes } from '@polkadot/types/types';
|
||||
import { formatNumber } from '@polkadot/util';
|
||||
|
||||
import { generateDefaultConsts, generateDefaultErrors, generateDefaultEvents, generateDefaultQuery, generateDefaultRpc, generateDefaultRuntime, generateDefaultTx } from './generate';
|
||||
import { assertDir, assertFile, getMetadataViaWs, HEADER, writeFile } from './util';
|
||||
|
||||
function generate (metaHex: HexString, pkg: string | undefined, output: string, isStrict?: boolean): void {
|
||||
async function generate (metaHex: HexString, pkg: string | undefined, output: string, isStrict?: boolean): Promise<void> {
|
||||
console.log(`Generating from metadata, ${formatNumber((metaHex.length - 2) / 2)} bytes`);
|
||||
|
||||
const outputPath = assertDir(path.join(process.cwd(), output));
|
||||
@@ -21,9 +21,12 @@ function generate (metaHex: HexString, pkg: string | undefined, output: string,
|
||||
|
||||
if (pkg) {
|
||||
try {
|
||||
const defPath = assertFile(path.join(outputPath, 'definitions.ts'));
|
||||
const defCont = await import(defPath) as { module: any };
|
||||
|
||||
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>
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
[pkg]: defCont.module
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('ERROR: No custom definitions found:', (error as Error).message);
|
||||
@@ -31,10 +34,12 @@ function generate (metaHex: HexString, pkg: string | undefined, output: string,
|
||||
}
|
||||
|
||||
try {
|
||||
const lookPath = assertFile(path.join(outputPath, 'lookup.ts'));
|
||||
const lookCont = await import(lookPath) as { module: { default: DefinitionsTypes } };
|
||||
|
||||
customLookupDefinitions = {
|
||||
rpc: {},
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-var-requires
|
||||
types: require(assertFile(path.join(outputPath, 'lookup.ts'))).default
|
||||
types: lookCont.module.default
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('ERROR: No lookup definitions found:', (error as Error).message);
|
||||
@@ -64,7 +69,7 @@ function generate (metaHex: HexString, pkg: string | undefined, output: string,
|
||||
|
||||
type ArgV = { endpoint: string; output: string; package?: string; strict?: boolean };
|
||||
|
||||
export function main (): void {
|
||||
async function mainPromise (): Promise<void> {
|
||||
const { endpoint, output, package: pkg, strict: isStrict } = yargs.strict().options({
|
||||
endpoint: {
|
||||
description: 'The endpoint to connect to (e.g. wss://kusama-rpc.polkadot.io) or relative path to a file containing the JSON output of an RPC state_getMetadata call',
|
||||
@@ -86,14 +91,20 @@ export function main (): void {
|
||||
}
|
||||
}).argv as ArgV;
|
||||
|
||||
if (endpoint.startsWith('wss://') || endpoint.startsWith('ws://')) {
|
||||
getMetadataViaWs(endpoint)
|
||||
.then((metadata) => generate(metadata, pkg, output, isStrict))
|
||||
.catch(() => process.exit(1));
|
||||
} else {
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const metadata = (require(assertFile(path.join(process.cwd(), endpoint))) as Record<string, HexString>).result;
|
||||
let metadata: HexString;
|
||||
|
||||
generate(metadata, pkg, output, isStrict);
|
||||
if (endpoint.startsWith('wss://') || endpoint.startsWith('ws://')) {
|
||||
metadata = await getMetadataViaWs(endpoint);
|
||||
} else {
|
||||
const metaPath = assertFile(path.join(process.cwd(), endpoint));
|
||||
const metaCont = await import(metaPath) as { result: HexString };
|
||||
|
||||
metadata = metaCont.result;
|
||||
}
|
||||
|
||||
await generate(metadata, pkg, output, isStrict);
|
||||
}
|
||||
|
||||
export function main (): void {
|
||||
mainPromise().catch(() => process.exit(1));
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ import { assertDir, assertFile, getMetadataViaWs } from './util';
|
||||
|
||||
type ArgV = { input: string; package: string; endpoint?: string; };
|
||||
|
||||
export function main (): void {
|
||||
async function mainPromise (): Promise<void> {
|
||||
const { endpoint, input, package: pkg } = yargs.strict().options({
|
||||
endpoint: {
|
||||
description: 'The endpoint to connect to (e.g. wss://kusama-rpc.polkadot.io) or relative path to a file containing the JSON output of an RPC state_getMetadata call',
|
||||
@@ -37,8 +37,9 @@ export function main (): void {
|
||||
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>;
|
||||
const defPath = assertFile(path.join(inputPath, 'definitions.ts'));
|
||||
|
||||
userDefs = await import(defPath) as Record<string, any>;
|
||||
} catch (error) {
|
||||
console.error('ERROR: Unable to load user definitions:', (error as Error).message);
|
||||
}
|
||||
@@ -70,15 +71,21 @@ export function main (): void {
|
||||
generateInterfaceTypes(allDefs, path.join(inputPath, 'augment-types.ts'));
|
||||
|
||||
if (endpoint) {
|
||||
if (endpoint.startsWith('wss://') || endpoint.startsWith('ws://')) {
|
||||
getMetadataViaWs(endpoint)
|
||||
.then((metadata) => generateDefaultLookup(inputPath, metadata))
|
||||
.catch(() => process.exit(1));
|
||||
} else {
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const metaHex = (require(assertFile(path.join(process.cwd(), endpoint))) as Record<string, HexString>).result;
|
||||
let metadata: HexString;
|
||||
|
||||
generateDefaultLookup(inputPath, metaHex);
|
||||
if (endpoint.startsWith('wss://') || endpoint.startsWith('ws://')) {
|
||||
metadata = await getMetadataViaWs(endpoint);
|
||||
} else {
|
||||
const metaPath = assertFile(path.join(process.cwd(), endpoint));
|
||||
const metaCont = await import(metaPath) as { result: HexString };
|
||||
|
||||
metadata = metaCont.result;
|
||||
}
|
||||
|
||||
generateDefaultLookup(inputPath, metadata);
|
||||
}
|
||||
}
|
||||
|
||||
export function main (): void {
|
||||
mainPromise().catch(() => process.exit(1));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user