@cosmwasm/ts-codegen
Version:
@cosmwasm/ts-codegen converts your CosmWasm smart contracts into dev-friendly TypeScript classes so you can focus on shipping code.
39 lines (38 loc) • 1.39 kB
JavaScript
import * as t from '@babel/types';
import { RenderContext, } from '@cosmwasm/ts-codegen-ast';
import { pascal } from 'case';
import { findAndParseTypes, findExecuteMsg } from '../utils';
import { clean } from '../utils/clean';
import { BuilderPluginBase } from './plugin-base';
export class TypesPlugin extends BuilderPluginBase {
initContext(contract, options) {
return new RenderContext(contract, options, this.builder?.builderContext);
}
async doRender(name, context) {
const { enabled } = this.options.types;
if (!enabled) {
return;
}
const { schemas } = context.contract;
const options = this.options.types;
const localname = pascal(name) + '.types.ts';
const ExecuteMsg = findExecuteMsg(schemas);
const typeHash = await findAndParseTypes(schemas);
const body = [];
// TYPES
Object.values(typeHash).forEach((type) => {
body.push(clean(type));
});
// alias the ExecuteMsg
if (options.aliasExecuteMsg && ExecuteMsg) {
body.push(t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier(`${name}ExecuteMsg`), null, t.tsTypeReference(t.identifier('ExecuteMsg')))));
}
return [
{
type: 'type',
localname,
body,
},
];
}
}