-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodegen.ts
More file actions
92 lines (80 loc) · 3.49 KB
/
Copy pathcodegen.ts
File metadata and controls
92 lines (80 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { confirm } from '@inquirer/prompts'
import { Manifest } from '@mimicprotocol/sdk'
import { Command, Flags } from '@oclif/core'
import * as fs from 'fs'
import { join } from 'path'
import { AbisInterfaceGenerator, InputsInterfaceGenerator, ManifestHandler } from '../lib'
import log from '../log'
import { FlagsType } from '../types'
import Functions, { DefaultFunctionConfig } from './functions'
export type CodegenFlags = FlagsType<typeof Codegen>
export default class Codegen extends Command {
static override description = 'Generates typed interfaces for declared inputs and ABIs from your manifest.yaml file'
static override examples = [
'<%= config.bin %> <%= command.id %> --manifest ./manifest.yaml --types-directory ./types',
]
static override flags = {
...Functions.flags,
function: Flags.string({
char: 'f',
description: 'Function to use when resolving function configuration',
default: DefaultFunctionConfig.function,
}),
manifest: Flags.string({
char: 'm',
description: 'Specify a custom manifest file path',
default: DefaultFunctionConfig.manifest,
}),
'types-directory': Flags.string({
char: 't',
description: 'Output directory for generated types',
default: DefaultFunctionConfig['types-directory'],
}),
clean: Flags.boolean({
char: 'c',
description: 'Remove existing generated types before generating new files',
default: false,
}),
}
public async run(): Promise<void> {
const { flags } = await this.parse(Codegen)
await Functions.runFunctions(this, flags, Codegen.codegen, 'code generation')
}
public static async codegen(cmd: Command, flags: CodegenFlags): Promise<void> {
const { manifest: manifestDir, 'types-directory': typesDir, clean } = flags
const manifest = ManifestHandler.load(cmd, manifestDir)
if (clean) {
const shouldDelete = await confirm({
message: `Are you sure you want to ${log.warnText('delete')} all the contents in ${log.highlightText(typesDir)}. This action is ${log.warnText('irreversible')}`,
default: false,
})
if (!shouldDelete) {
console.log('You can remove the --clean flag from your command')
console.log('Stopping initialization...')
cmd.exit(0)
}
log.startAction(`Deleting contents of ${typesDir}`)
if (fs.existsSync(typesDir)) fs.rmSync(typesDir, { recursive: true, force: true })
}
log.startAction('Generating code')
if (Object.keys(manifest.inputs).length == 0 && Object.keys(manifest.abis).length == 0) {
log.stopAction()
return
}
if (!fs.existsSync(typesDir)) fs.mkdirSync(typesDir, { recursive: true })
Codegen.generateAbisCode(manifest, typesDir, manifestDir)
Codegen.generateInputsCode(manifest, typesDir)
log.stopAction()
}
private static generateAbisCode(manifest: Manifest, typesDir: string, manifestDir: string) {
for (const [contractName, path] of Object.entries(manifest.abis)) {
const abi = JSON.parse(fs.readFileSync(join(manifestDir, '../', path), 'utf-8'))
const abiInterface = AbisInterfaceGenerator.generate(abi, contractName)
if (abiInterface.length > 0) fs.writeFileSync(`${typesDir}/${contractName}.ts`, abiInterface)
}
}
private static generateInputsCode(manifest: Manifest, typesDir: string) {
const inputsInterface = InputsInterfaceGenerator.generate(manifest.inputs)
if (inputsInterface.length > 0) fs.writeFileSync(`${typesDir}/index.ts`, inputsInterface)
}
}