Skip to content

Commit

Permalink
fix: import event's parameters types of the same spec version
Browse files Browse the repository at this point in the history
affects: @joystream/hydra-typegen
  • Loading branch information
zeeshanakram3 committed Mar 28, 2023
1 parent c114562 commit b9319e6
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 10 deletions.
10 changes: 10 additions & 0 deletions packages/hydra-typegen/src/commands/typegen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ import {
import { parseConfigFile } from '../../config/parse-yaml'
import { validate } from '../../config/validate'
import { generateTypeRegistry } from '../../generators/gen-typeRegistry'
import { generateTypesLookup } from '../../generators/gen-typesLookup'

export interface IConfig {
metadata: MetadataSource
events: string[]
calls: string[]
outDir: string
typegenBinPath: string
strict?: boolean
}

Expand All @@ -35,6 +37,7 @@ export type Flags = {
outDir: string
strict: boolean
debug: boolean
typegenBinPath?: string
}

const debug = Debug('hydra-typegen:typegen')
Expand Down Expand Up @@ -77,6 +80,12 @@ Otherwise a relative path to a json file matching the RPC call response is expec
'A relative path the root folder where the generated files will be generated',
default: 'generated/types',
}),
typegenBinPath: flags.string({
char: 't',
description:
'A relative path to the polkadot typegen binary (polkadot-types-from-defs)',
default: 'node_modules/.bin/polkadot-types-from-defs',
}),
strict: flags.boolean({
char: 's',
description: `Strict mode. If on, the generated code throws an error if the input event argument \
Expand Down Expand Up @@ -155,6 +164,7 @@ types don't much the metadata definiton`,
debug(`Output dir: ${dest}`)
fs.mkdirSync(dest, { recursive: true })

await generateTypesLookup(config, generatorConfig)
generateModuleTypes(generatorConfig)
generateIndex(generatorConfig)
generateTypeRegistry(generatorConfig)
Expand Down
6 changes: 0 additions & 6 deletions packages/hydra-typegen/src/generators/gen-index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fs from 'fs'
import path from 'path'
import { GeneratorConfig } from '.'
import { formatWithPrettier, readTemplate, writeFile } from '../util'
Expand All @@ -13,11 +12,6 @@ export function generateIndex({
originalMetadata,
dest,
}: GeneratorConfig): void {
fs.writeFileSync(
path.join(dest, `metadata.json`),
JSON.stringify(originalMetadata.toHex())
)

writeFile(path.join(dest, `index.ts`), () =>
formatWithPrettier(
generateIndexTemplate({
Expand Down
15 changes: 14 additions & 1 deletion packages/hydra-typegen/src/generators/gen-typeRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from 'fs'
import path from 'path'
import { GeneratorConfig } from '.'
import { formatWithPrettier, readTemplate, writeFile } from '../util'
Expand All @@ -9,7 +10,19 @@ const generateTypeRegistryTemplate = handlebars.compile(
readTemplate('typeRegistry')
)

export function generateTypeRegistry({ dest }: GeneratorConfig): void {
export function generateTypeRegistry({
dest,
originalMetadata,
}: GeneratorConfig): void {
fs.writeFileSync(
path.join(dest, `metadata.json`),
JSON.stringify({
'id': 1,
'jsonrpc': '2.0',
result: originalMetadata.toHex(),
})
)

writeFile(path.join(dest, `typeRegistry.ts`), () =>
formatWithPrettier(generateTypeRegistryTemplate({}))
)
Expand Down
53 changes: 53 additions & 0 deletions packages/hydra-typegen/src/generators/gen-typesLookup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { execFile } from 'child_process'
import fs from 'fs'
import path from 'path'
import { promisify } from 'util'
import { GeneratorConfig } from '.'
import { IConfig } from '../commands/typegen'
import { formatWithPrettier, writeFile } from '../util'

const debug = require('debug')('hydra-typegen:gen-typesLookup')

export async function generateTypesLookup(
{ outDir, metadata: { source }, typegenBinPath }: IConfig,
{ dest, specVersion }: GeneratorConfig
) {
const TYPES_LOOKUP_FILE_NAME = 'types-lookup.ts'
const TYPES_OUT_DIR_NAME = 'types'

// create the temporary types-lookup directory
const typesLookupOutDir = path.join(dest, TYPES_OUT_DIR_NAME)
fs.mkdirSync(typesLookupOutDir, { recursive: true })

// generate types-lookup.ts using polkadot typegen
const pExecFile = promisify(execFile)
await pExecFile(path.join(process.cwd(), typegenBinPath), [
'--endpoint',
source,
'--input',
path.join(outDir, specVersion.toString(), TYPES_OUT_DIR_NAME),
'--package',
dest,
])

// read types-lookup.ts
const file = fs.readFileSync(
path.join(typesLookupOutDir, TYPES_LOOKUP_FILE_NAME),
'utf8'
)

// remove module augmentation from the file
const outFile = file
.replace(`declare module '@polkadot/types/lookup' {`, '')
.replace('} // declare module', '')

// save the file
writeFile(path.join(dest, TYPES_LOOKUP_FILE_NAME), () =>
formatWithPrettier(outFile)
)

// remove the temporary types-lookup directory
fs.rmSync(typesLookupOutDir, { recursive: true, force: true })

debug('Done writing types-lookup')
}
2 changes: 1 addition & 1 deletion packages/hydra-typegen/src/generators/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function buildModuleImports(
const importsDef: ImportsDef = {}

types.forEach((i) => {
const importLoc = importsRegistry[i] || '@polkadot/types/lookup'
const importLoc = importsRegistry[i] || './types-lookup'

if (!importsDef[importLoc]) {
importsDef[importLoc] = {}
Expand Down
4 changes: 2 additions & 2 deletions packages/hydra-typegen/src/templates/typeRegistry.hbs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable */

import { TypeRegistry, Metadata } from '@polkadot/types'
import metadataHex from './metadata.json'
import metadataObject from './metadata.json'

const typeRegistry = new TypeRegistry() as any
const metadata = new Metadata(typeRegistry, metadataHex as `0x${string}`)
const metadata = new Metadata(typeRegistry, metadataObject.result as `0x${string}`)

typeRegistry.setMetadata(metadata)

Expand Down

0 comments on commit b9319e6

Please sign in to comment.