From 44a7ea4bafe9fd24e30b4a323c2f437c5a449189 Mon Sep 17 00:00:00 2001 From: Satoshi Qazi Muhammed Date: Thu, 8 Oct 2026 04:03:56 +0400 Subject: [PATCH] typegen: make the metadata docs CLI work from CommonJS and without docs/ - metadataMd located @pezkuwi/api-derive with import.meta.resolve at module load. import.meta is a syntax error in the CJS output, so require('@pezkuwi/typegen/metadataMd') failed. The directory now comes from api-derive's packageInfo, which the build fills for each format (package root for ESM, cjs/ for CommonJS), and is looked up only when the derive docs are written. - writeFile creates the output directory; the CLI used to die with an unhandled ENOENT unless docs/ and docs/derives already existed. - the endpoint examples in the CLI help pointed at wss://dicle-rpc.pezkuwi.io, which does not exist; they show wss://rpc.pezkuwichain.io. From the installed packages, the ESM bin and require(...).main() now write the same eight files, identical to what the ESM bin wrote before. --- packages/typegen/src/extractChain.ts | 2 +- packages/typegen/src/fromChain.ts | 2 +- packages/typegen/src/fromDefs.ts | 2 +- packages/typegen/src/metadataMd.ts | 31 +++++++++++++++++++--------- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/packages/typegen/src/extractChain.ts b/packages/typegen/src/extractChain.ts index b8c636a..a0999a0 100644 --- a/packages/typegen/src/extractChain.ts +++ b/packages/typegen/src/extractChain.ts @@ -37,7 +37,7 @@ export function main (): void { .options({ ws: { default: 'ws://127.0.0.1:9944', - description: 'The API endpoint to connect to, e.g. wss://dicle-rpc.pezkuwi.io', + description: 'The API endpoint to connect to, e.g. wss://rpc.pezkuwichain.io', required: true, type: 'string' } diff --git a/packages/typegen/src/fromChain.ts b/packages/typegen/src/fromChain.ts index f7e6280..0e66267 100644 --- a/packages/typegen/src/fromChain.ts +++ b/packages/typegen/src/fromChain.ts @@ -75,7 +75,7 @@ interface ArgV { endpoint: string; output: string; package?: string; strict?: bo async function mainPromise (): Promise { const { endpoint, output, package: pkg, strict: isStrict } = yargs(hideBin(process.argv)).strict().options({ endpoint: { - description: 'The endpoint to connect to (e.g. wss://dicle-rpc.pezkuwi.io) or relative path to a file containing the JSON output of an RPC state_getMetadata call', + description: 'The endpoint to connect to (e.g. wss://rpc.pezkuwichain.io) or relative path to a file containing the JSON output of an RPC state_getMetadata call', required: true, type: 'string' }, diff --git a/packages/typegen/src/fromDefs.ts b/packages/typegen/src/fromDefs.ts index 0cff196..1483be5 100644 --- a/packages/typegen/src/fromDefs.ts +++ b/packages/typegen/src/fromDefs.ts @@ -21,7 +21,7 @@ interface ArgV { input: string; package: string; endpoint?: string; } async function mainPromise (): Promise { const { endpoint, input, package: pkg } = yargs(hideBin(process.argv)).strict().options({ endpoint: { - description: 'The endpoint to connect to (e.g. wss://dicle-rpc.pezkuwi.io) or relative path to a file containing the JSON output of an RPC state_getMetadata call', + description: 'The endpoint to connect to (e.g. wss://rpc.pezkuwichain.io) or relative path to a file containing the JSON output of an RPC state_getMetadata call', type: 'string' }, input: { diff --git a/packages/typegen/src/metadataMd.ts b/packages/typegen/src/metadataMd.ts index 1193dd4..f9a9a10 100644 --- a/packages/typegen/src/metadataMd.ts +++ b/packages/typegen/src/metadataMd.ts @@ -9,13 +9,13 @@ import type { HexString } from '@pezkuwi/util/types'; import { parse, type Spec } from 'comment-parser'; import fs from 'node:fs'; -import path, { dirname, resolve } from 'node:path'; +import path, { resolve } from 'node:path'; import process from 'node:process'; -import { fileURLToPath } from 'node:url'; import yargs from 'yargs'; import { hideBin } from 'yargs/helpers'; import { derive } from '@pezkuwi/api-derive'; +import { packageInfo as derivePackageInfo } from '@pezkuwi/api-derive/packageInfo'; import { Metadata, TypeRegistry, Vec } from '@pezkuwi/types'; import * as definitions from '@pezkuwi/types/interfaces/definitions'; import { getStorage as getBizinikiwiStorage } from '@pezkuwi/types/metadata/decorate/storage/getStorage'; @@ -564,17 +564,26 @@ function addErrors (runtimeDesc: string, { lookup, pallets }: MetadataLatest): s }); } -function getDependencyBasePath (moduleName: string): string { - const modulePath = import.meta.resolve(moduleName); +/** + * The directory of the installed @pezkuwi/api-derive modules: the package root + * for ESM, cjs/ for CommonJS. Taken from its packageInfo, which the build + * fills for each format; import.meta.resolve is not valid in the CJS output. + */ +function getDeriveBasePath (): string { + if (derivePackageInfo.path === 'auto') { + throw new Error('Unable to locate the built @pezkuwi/api-derive modules; run this from an installed @pezkuwi/typegen'); + } - return resolve(dirname(fileURLToPath(modulePath))); + return resolve( + derivePackageInfo.type === 'esm' + ? decodeURIComponent(derivePackageInfo.path) + : derivePackageInfo.path + ); } -const BASE_DERIVE_PATH = getDependencyBasePath('@pezkuwi/api-derive'); - // It finds all typescript file paths withing a given derive module. const obtainDeriveFiles = (deriveModule: string) => { - const filePath = `${BASE_DERIVE_PATH}/${deriveModule}`; + const filePath = `${getDeriveBasePath()}/${deriveModule}`; const files = fs.readdirSync(filePath); return files @@ -646,7 +655,7 @@ const getDeriveDocs = ( metadata: Record, file: string ) => { - const filePath = `${BASE_DERIVE_PATH}/${file}`; + const filePath = `${getDeriveBasePath()}/${file}`; const deriveModule = file.split('/')[0]; const fileContent = fs.readFileSync(filePath, 'utf8'); const comments = parse(fileContent); @@ -735,6 +744,8 @@ function generateDerives () { /** @internal */ function writeFile (name: string, ...chunks: any[]): void { + fs.mkdirSync(path.dirname(name), { recursive: true }); + const writeStream = fs.createWriteStream(name, { encoding: 'utf8', flags: 'w' }); writeStream.on('finish', (): void => { @@ -757,7 +768,7 @@ async function mainPromise (): Promise { type: 'string' }, endpoint: { - description: 'The endpoint to connect to (e.g. wss://dicle-rpc.pezkuwi.io) or relative path to a file containing the JSON output of an RPC state_getMetadata call', + description: 'The endpoint to connect to (e.g. wss://rpc.pezkuwichain.io) or relative path to a file containing the JSON output of an RPC state_getMetadata call', type: 'string' }, metadataVer: {