@cosmwasm/ts-codegen 0.35.6 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE-Apache +201 -0
- package/LICENSE-MIT +21 -0
- package/README.md +15 -7
- package/{types/src/builder → builder}/builder.d.ts +2 -2
- package/builder/builder.js +172 -0
- package/builder/index.js +17 -0
- package/bundler/bundler.d.ts +8 -0
- package/bundler/bundler.js +79 -0
- package/bundler/index.js +17 -0
- package/cli.d.ts +2 -0
- package/cli.js +24 -0
- package/cmds.d.ts +2 -0
- package/cmds.js +14 -0
- package/commands/create-boilerplate.d.ts +3 -0
- package/commands/create-boilerplate.js +132 -0
- package/commands/generate.d.ts +3 -0
- package/commands/generate.js +175 -0
- package/commands/install.d.ts +3 -0
- package/commands/install.js +107 -0
- package/{src/builder/builder.ts → esm/builder/builder.js} +39 -139
- package/esm/builder/index.js +1 -0
- package/esm/bundler/bundler.js +47 -0
- package/esm/bundler/index.js +1 -0
- package/esm/cli.js +20 -0
- package/{src → esm}/cmds.js +0 -1
- package/{src/commands/create-boilerplate.ts → esm/commands/create-boilerplate.js} +35 -70
- package/{src/commands/generate.ts → esm/commands/generate.js} +19 -24
- package/{src/commands/install.ts → esm/commands/install.js} +22 -35
- package/esm/file.js +18 -0
- package/esm/generators/client.js +67 -0
- package/esm/generators/create-helpers.js +36 -0
- package/esm/generators/message-composer.js +55 -0
- package/esm/generators/msg-builder.js +58 -0
- package/esm/generators/react-query.js +76 -0
- package/esm/generators/recoil.js +58 -0
- package/esm/generators/types.js +58 -0
- package/{src/index.ts → esm/index.js} +3 -6
- package/esm/plugins/client.js +59 -0
- package/esm/plugins/message-builder.js +52 -0
- package/esm/plugins/message-composer.js +46 -0
- package/esm/plugins/plugin-base.js +47 -0
- package/esm/plugins/provider-bundle.js +58 -0
- package/esm/plugins/provider.js +73 -0
- package/esm/plugins/react-query.js +69 -0
- package/esm/plugins/recoil.js +51 -0
- package/esm/plugins/types.js +38 -0
- package/{src → esm}/ts-codegen.js +1 -2
- package/{src/utils/clean.ts → esm/utils/clean.js} +7 -7
- package/{src/utils/cleanse.ts → esm/utils/cleanse.js} +18 -22
- package/esm/utils/files.js +42 -0
- package/{src/utils/parse.ts → esm/utils/parse.js} +4 -13
- package/esm/utils/prompt.js +59 -0
- package/{src/utils/schemas.ts → esm/utils/schemas.js} +16 -44
- package/esm/utils/unused.js +47 -0
- package/file.js +20 -0
- package/generators/client.js +95 -0
- package/generators/create-helpers.js +43 -0
- package/generators/message-composer.js +83 -0
- package/generators/msg-builder.js +86 -0
- package/generators/react-query.js +104 -0
- package/generators/recoil.js +86 -0
- package/generators/types.js +86 -0
- package/{module/helpers → helpers}/contractContextBase.js +5 -2
- package/{module/helpers → helpers}/contractContextBaseShortHandCtor.js +5 -2
- package/{module/helpers → helpers}/contractsContextTSX.js +5 -2
- package/{module/helpers/index.js → helpers/index.d.ts} +1 -1
- package/helpers/index.js +19 -0
- package/index.js +39 -0
- package/package.json +27 -51
- package/plugins/client.js +86 -0
- package/plugins/index.d.ts +1 -0
- package/plugins/index.js +17 -0
- package/plugins/message-builder.js +79 -0
- package/plugins/message-composer.js +73 -0
- package/{types/src/plugins → plugins}/plugin-base.d.ts +1 -1
- package/plugins/plugin-base.js +77 -0
- package/plugins/provider-bundle.js +85 -0
- package/{types/src/plugins → plugins}/provider.d.ts +2 -2
- package/plugins/provider.js +102 -0
- package/plugins/react-query.js +96 -0
- package/plugins/recoil.js +78 -0
- package/plugins/types.js +65 -0
- package/ts-codegen.js +8 -0
- package/utils/clean.js +49 -0
- package/utils/cleanse.js +89 -0
- package/utils/files.js +73 -0
- package/utils/header.js +14 -0
- package/utils/index.d.ts +1 -0
- package/utils/index.js +17 -0
- package/utils/parse.d.ts +1 -0
- package/utils/parse.js +43 -0
- package/utils/prompt.d.ts +3 -0
- package/utils/prompt.js +65 -0
- package/utils/schemas.d.ts +11 -0
- package/utils/schemas.js +81 -0
- package/utils/unused.js +73 -0
- package/main/builder/builder.js +0 -308
- package/main/builder/index.js +0 -16
- package/main/bundler/bundler.js +0 -61
- package/main/bundler/index.js +0 -16
- package/main/cli.js +0 -51
- package/main/cmds.js +0 -15
- package/main/commands/create-boilerplate.js +0 -166
- package/main/commands/generate.js +0 -196
- package/main/commands/install.js +0 -138
- package/main/file.js +0 -35
- package/main/generators/client.js +0 -90
- package/main/generators/create-helpers.js +0 -38
- package/main/generators/message-composer.js +0 -77
- package/main/generators/msg-builder.js +0 -84
- package/main/generators/react-query.js +0 -102
- package/main/generators/recoil.js +0 -81
- package/main/generators/types.js +0 -82
- package/main/helpers/contractContextBase.js +0 -8
- package/main/helpers/contractContextBaseShortHandCtor.js +0 -8
- package/main/helpers/contractsContextTSX.js +0 -8
- package/main/helpers/index.js +0 -38
- package/main/index.js +0 -119
- package/main/plugins/client.js +0 -112
- package/main/plugins/index.js +0 -16
- package/main/plugins/message-builder.js +0 -102
- package/main/plugins/message-composer.js +0 -98
- package/main/plugins/plugin-base.js +0 -103
- package/main/plugins/provider-bundle.js +0 -108
- package/main/plugins/provider.js +0 -126
- package/main/plugins/react-query.js +0 -120
- package/main/plugins/recoil.js +0 -108
- package/main/plugins/types.js +0 -89
- package/main/ts-codegen.js +0 -20
- package/main/utils/clean.js +0 -53
- package/main/utils/cleanse.js +0 -87
- package/main/utils/files.js +0 -57
- package/main/utils/header.js +0 -11
- package/main/utils/index.js +0 -16
- package/main/utils/parse.js +0 -44
- package/main/utils/prompt.js +0 -98
- package/main/utils/schemas.js +0 -144
- package/main/utils/unused.js +0 -57
- package/module/builder/builder.js +0 -143
- package/module/builder/index.js +0 -1
- package/module/bundler/bundler.js +0 -36
- package/module/bundler/index.js +0 -1
- package/module/cli.js +0 -19
- package/module/cmds.js +0 -8
- package/module/commands/create-boilerplate.js +0 -102
- package/module/commands/generate.js +0 -163
- package/module/commands/install.js +0 -98
- package/module/file.js +0 -18
- package/module/generators/client.js +0 -65
- package/module/generators/create-helpers.js +0 -33
- package/module/generators/message-composer.js +0 -52
- package/module/generators/msg-builder.js +0 -58
- package/module/generators/react-query.js +0 -75
- package/module/generators/recoil.js +0 -53
- package/module/generators/types.js +0 -55
- package/module/index.js +0 -14
- package/module/plugins/client.js +0 -63
- package/module/plugins/index.js +0 -1
- package/module/plugins/message-builder.js +0 -55
- package/module/plugins/message-composer.js +0 -49
- package/module/plugins/plugin-base.js +0 -66
- package/module/plugins/provider-bundle.js +0 -56
- package/module/plugins/provider.js +0 -68
- package/module/plugins/react-query.js +0 -72
- package/module/plugins/recoil.js +0 -57
- package/module/plugins/types.js +0 -42
- package/module/ts-codegen.js +0 -6
- package/module/utils/clean.js +0 -44
- package/module/utils/cleanse.js +0 -78
- package/module/utils/files.js +0 -40
- package/module/utils/header.js +0 -8
- package/module/utils/index.js +0 -1
- package/module/utils/parse.js +0 -35
- package/module/utils/prompt.js +0 -63
- package/module/utils/schemas.js +0 -85
- package/module/utils/unused.js +0 -43
- package/src/builder/index.ts +0 -1
- package/src/bundler/bundler.ts +0 -67
- package/src/bundler/index.ts +0 -1
- package/src/cli.js +0 -21
- package/src/file.js +0 -20
- package/src/generators/client.ts +0 -109
- package/src/generators/create-helpers.ts +0 -71
- package/src/generators/message-composer.ts +0 -86
- package/src/generators/msg-builder.ts +0 -78
- package/src/generators/react-query.ts +0 -102
- package/src/generators/recoil.ts +0 -92
- package/src/generators/types.ts +0 -94
- package/src/helpers/index.ts +0 -3
- package/src/plugins/client.ts +0 -124
- package/src/plugins/index.ts +0 -1
- package/src/plugins/message-builder.ts +0 -85
- package/src/plugins/message-composer.ts +0 -89
- package/src/plugins/plugin-base.ts +0 -122
- package/src/plugins/provider-bundle.ts +0 -98
- package/src/plugins/provider.ts +0 -115
- package/src/plugins/react-query.ts +0 -115
- package/src/plugins/recoil.ts +0 -89
- package/src/plugins/types.ts +0 -74
- package/src/utils/files.ts +0 -73
- package/src/utils/index.ts +0 -1
- package/src/utils/prompt.js +0 -65
- package/src/utils/unused.ts +0 -52
- package/types/src/bundler/bundler.d.ts +0 -4
- package/types/src/cli.d.ts +0 -1
- package/types/src/cmds.d.ts +0 -1
- package/types/src/commands/create-boilerplate.d.ts +0 -2
- package/types/src/commands/generate.d.ts +0 -2
- package/types/src/commands/install.d.ts +0 -2
- package/types/src/generators/msg-builder.ts +0 -5
- package/types/src/plugins/msg-builder.d.ts +0 -12
- package/types/src/plugins/use-contracts.d.ts +0 -12
- package/types/src/types.d.ts +0 -16
- package/types/src/utils/parse.d.ts +0 -1
- package/types/src/utils/prompt.d.ts +0 -3
- package/types/src/utils/schemas.d.ts +0 -10
- /package/{types/src/builder → builder}/index.d.ts +0 -0
- /package/{types/src/bundler → bundler}/index.d.ts +0 -0
- /package/{src/helpers/contractContextBase.ts → esm/helpers/contractContextBase.js} +0 -0
- /package/{src/helpers/contractContextBaseShortHandCtor.ts → esm/helpers/contractContextBaseShortHandCtor.js} +0 -0
- /package/{src/helpers/contractsContextTSX.ts → esm/helpers/contractsContextTSX.js} +0 -0
- /package/{types/src/helpers/index.d.ts → esm/helpers/index.js} +0 -0
- /package/{types/src/plugins/index.d.ts → esm/plugins/index.js} +0 -0
- /package/{src/utils/header.ts → esm/utils/header.js} +0 -0
- /package/{types/src/utils/index.d.ts → esm/utils/index.js} +0 -0
- /package/{types/src/file.d.ts → file.d.ts} +0 -0
- /package/{types/src/generators → generators}/client.d.ts +0 -0
- /package/{types/src/generators → generators}/create-helpers.d.ts +0 -0
- /package/{types/src/generators → generators}/message-composer.d.ts +0 -0
- /package/{types/src/generators → generators}/msg-builder.d.ts +0 -0
- /package/{types/src/generators → generators}/react-query.d.ts +0 -0
- /package/{types/src/generators → generators}/recoil.d.ts +0 -0
- /package/{types/src/generators → generators}/types.d.ts +0 -0
- /package/{types/src/helpers → helpers}/contractContextBase.d.ts +0 -0
- /package/{types/src/helpers → helpers}/contractContextBaseShortHandCtor.d.ts +0 -0
- /package/{types/src/helpers → helpers}/contractsContextTSX.d.ts +0 -0
- /package/{types/src/index.d.ts → index.d.ts} +0 -0
- /package/{types/src/plugins → plugins}/client.d.ts +0 -0
- /package/{types/src/plugins → plugins}/message-builder.d.ts +0 -0
- /package/{types/src/plugins → plugins}/message-composer.d.ts +0 -0
- /package/{types/src/plugins → plugins}/provider-bundle.d.ts +0 -0
- /package/{types/src/plugins → plugins}/react-query.d.ts +0 -0
- /package/{types/src/plugins → plugins}/recoil.d.ts +0 -0
- /package/{types/src/plugins → plugins}/types.d.ts +0 -0
- /package/{types/src/ts-codegen.d.ts → ts-codegen.d.ts} +0 -0
- /package/{types/src/utils → utils}/clean.d.ts +0 -0
- /package/{types/src/utils → utils}/cleanse.d.ts +0 -0
- /package/{types/src/utils → utils}/files.d.ts +0 -0
- /package/{types/src/utils → utils}/header.d.ts +0 -0
- /package/{types/src/utils → utils}/unused.d.ts +0 -0
@@ -0,0 +1,52 @@
|
|
1
|
+
import { pascal } from 'case';
|
2
|
+
import * as w from 'wasm-ast-types';
|
3
|
+
import { findAndParseTypes, findQueryMsg, findExecuteMsg } from '../utils';
|
4
|
+
import { getMessageProperties, RenderContext } from 'wasm-ast-types';
|
5
|
+
import { BuilderPluginBase } from './plugin-base';
|
6
|
+
export class MessageBuilderPlugin extends BuilderPluginBase {
|
7
|
+
initContext(contract, options) {
|
8
|
+
return new RenderContext(contract, options, this.builder.builderContext);
|
9
|
+
}
|
10
|
+
async doRender(name, context) {
|
11
|
+
const { enabled } = this.option.messageBuilder;
|
12
|
+
if (!enabled) {
|
13
|
+
return;
|
14
|
+
}
|
15
|
+
const { schemas } = context.contract;
|
16
|
+
const localname = pascal(name) + '.message-builder.ts';
|
17
|
+
const TypesFile = pascal(name) + '.types';
|
18
|
+
const ExecuteMsg = findExecuteMsg(schemas);
|
19
|
+
const typeHash = await findAndParseTypes(schemas);
|
20
|
+
const body = [];
|
21
|
+
body.push(w.importStmt(Object.keys(typeHash), `./${TypesFile}`));
|
22
|
+
body.push(w.importStmt(['CamelCasedProperties'], 'type-fest'));
|
23
|
+
// execute messages
|
24
|
+
if (ExecuteMsg) {
|
25
|
+
const children = getMessageProperties(ExecuteMsg);
|
26
|
+
if (children.length > 0) {
|
27
|
+
const className = pascal(`${name}ExecuteMsgBuilder`);
|
28
|
+
body.push(w.createMessageBuilderClass(context, className, ExecuteMsg));
|
29
|
+
}
|
30
|
+
}
|
31
|
+
const QueryMsg = findQueryMsg(schemas);
|
32
|
+
// query messages
|
33
|
+
if (QueryMsg) {
|
34
|
+
const children = getMessageProperties(QueryMsg);
|
35
|
+
if (children.length > 0) {
|
36
|
+
const className = pascal(`${name}QueryMsgBuilder`);
|
37
|
+
body.push(w.createMessageBuilderClass(context, className, QueryMsg));
|
38
|
+
}
|
39
|
+
}
|
40
|
+
if (typeHash.hasOwnProperty('Coin')) {
|
41
|
+
// @ts-ignore
|
42
|
+
delete context.utils.Coin;
|
43
|
+
}
|
44
|
+
return [
|
45
|
+
{
|
46
|
+
type: 'message-builder',
|
47
|
+
localname,
|
48
|
+
body
|
49
|
+
}
|
50
|
+
];
|
51
|
+
}
|
52
|
+
}
|
@@ -0,0 +1,46 @@
|
|
1
|
+
import { pascal } from "case";
|
2
|
+
import * as w from "wasm-ast-types";
|
3
|
+
import { findAndParseTypes, findExecuteMsg } from "../utils";
|
4
|
+
import { getMessageProperties, RenderContext, } from "wasm-ast-types";
|
5
|
+
import { BuilderPluginBase } from "./plugin-base";
|
6
|
+
export const TYPE = "message-composer";
|
7
|
+
export class MessageComposerPlugin extends BuilderPluginBase {
|
8
|
+
initContext(contract, options) {
|
9
|
+
return new RenderContext(contract, options, this.builder.builderContext);
|
10
|
+
}
|
11
|
+
async doRender(name, context) {
|
12
|
+
const { enabled } = this.option.messageComposer;
|
13
|
+
if (!enabled) {
|
14
|
+
return;
|
15
|
+
}
|
16
|
+
const { schemas } = context.contract;
|
17
|
+
const localname = pascal(name) + ".message-composer.ts";
|
18
|
+
const TypesFile = pascal(name) + ".types";
|
19
|
+
const ExecuteMsg = findExecuteMsg(schemas);
|
20
|
+
const typeHash = await findAndParseTypes(schemas);
|
21
|
+
const body = [];
|
22
|
+
body.push(w.importStmt(Object.keys(typeHash), `./${TypesFile}`));
|
23
|
+
// execute messages
|
24
|
+
if (ExecuteMsg) {
|
25
|
+
const children = getMessageProperties(ExecuteMsg);
|
26
|
+
if (children.length > 0) {
|
27
|
+
const TheClass = pascal(`${name}MsgComposer`);
|
28
|
+
const Interface = pascal(`${name}Msg`);
|
29
|
+
body.push(w.createMessageComposerInterface(context, Interface, ExecuteMsg));
|
30
|
+
body.push(w.createMessageComposerClass(context, TheClass, Interface, ExecuteMsg));
|
31
|
+
context.addProviderInfo(name, w.PROVIDER_TYPES.MESSAGE_COMPOSER_TYPE, TheClass, localname);
|
32
|
+
}
|
33
|
+
}
|
34
|
+
if (typeHash.hasOwnProperty("Coin")) {
|
35
|
+
// @ts-ignore
|
36
|
+
delete context.utils.Coin;
|
37
|
+
}
|
38
|
+
return [
|
39
|
+
{
|
40
|
+
type: TYPE,
|
41
|
+
localname,
|
42
|
+
body,
|
43
|
+
},
|
44
|
+
];
|
45
|
+
}
|
46
|
+
}
|
@@ -0,0 +1,47 @@
|
|
1
|
+
import { sync as mkdirp } from "mkdirp";
|
2
|
+
import { join } from "path";
|
3
|
+
import { writeFileSync } from "fs";
|
4
|
+
import { header } from "../utils/header";
|
5
|
+
import generate from "@babel/generator";
|
6
|
+
import * as t from "@babel/types";
|
7
|
+
/**
|
8
|
+
* BuilderPluginBase enable ts-codegen users implement their own plugins by only implement a few functions.
|
9
|
+
*/
|
10
|
+
export class BuilderPluginBase {
|
11
|
+
builder;
|
12
|
+
option;
|
13
|
+
utils;
|
14
|
+
constructor(opt, builder) {
|
15
|
+
this.option = opt;
|
16
|
+
this.builder = builder;
|
17
|
+
}
|
18
|
+
setBuilder(builder) {
|
19
|
+
this.builder = builder;
|
20
|
+
}
|
21
|
+
async render(name, contractInfo, outPath) {
|
22
|
+
const { enabled } = this.option;
|
23
|
+
if (!enabled) {
|
24
|
+
return;
|
25
|
+
}
|
26
|
+
const context = this.initContext(contractInfo, this.option);
|
27
|
+
const results = await this.doRender(name, context);
|
28
|
+
if (!results || !results.length) {
|
29
|
+
return [];
|
30
|
+
}
|
31
|
+
return results.map((result) => {
|
32
|
+
const imports = context.getImports(this.utils, result.localname);
|
33
|
+
// @ts-ignore
|
34
|
+
const code = header + generate(t.program([...imports, ...result.body])).code;
|
35
|
+
mkdirp(outPath);
|
36
|
+
const filename = join(outPath, result.localname);
|
37
|
+
writeFileSync(filename, code);
|
38
|
+
return {
|
39
|
+
type: result.type,
|
40
|
+
pluginType: result.pluginType,
|
41
|
+
contract: name,
|
42
|
+
localname: result.localname,
|
43
|
+
filename,
|
44
|
+
};
|
45
|
+
});
|
46
|
+
}
|
47
|
+
}
|
@@ -0,0 +1,58 @@
|
|
1
|
+
import { pascal } from "case";
|
2
|
+
import * as w from "wasm-ast-types";
|
3
|
+
import { RenderContext } from "wasm-ast-types";
|
4
|
+
import { BuilderPluginBase } from "./plugin-base";
|
5
|
+
import { GetLocalBaseNameByContractName } from "./provider";
|
6
|
+
export class ContractsProviderBundlePlugin extends BuilderPluginBase {
|
7
|
+
constructor(opt) {
|
8
|
+
super(opt);
|
9
|
+
this.utils = {
|
10
|
+
CosmWasmClient: "@cosmjs/cosmwasm-stargate",
|
11
|
+
SigningCosmWasmClient: "@cosmjs/cosmwasm-stargate",
|
12
|
+
IQueryClientProvider: "__contractContextBase__",
|
13
|
+
ISigningClientProvider: "__contractContextBase__",
|
14
|
+
IMessageComposerProvider: "__contractContextBase__",
|
15
|
+
};
|
16
|
+
}
|
17
|
+
initContext(contract, options) {
|
18
|
+
return new RenderContext(contract, options, this.builder.builderContext);
|
19
|
+
}
|
20
|
+
async doRender(name, context) {
|
21
|
+
if (!this.option?.useContractsHooks?.enabled) {
|
22
|
+
return;
|
23
|
+
}
|
24
|
+
const providerInfos = context.getProviderInfos();
|
25
|
+
if (!Object.keys(providerInfos || {})?.length) {
|
26
|
+
return;
|
27
|
+
}
|
28
|
+
const localname = "contractContextProviders.ts";
|
29
|
+
const body = [];
|
30
|
+
context.addUtil("CosmWasmClient");
|
31
|
+
context.addUtil("SigningCosmWasmClient");
|
32
|
+
context.addUtil("IQueryClientProvider");
|
33
|
+
context.addUtil("ISigningClientProvider");
|
34
|
+
context.addUtil("IMessageComposerProvider");
|
35
|
+
for (const name in providerInfos) {
|
36
|
+
if (Object.prototype.hasOwnProperty.call(providerInfos, name)) {
|
37
|
+
const providerInfo = providerInfos[name];
|
38
|
+
for (const key in providerInfo) {
|
39
|
+
if (Object.prototype.hasOwnProperty.call(providerInfo, key)) {
|
40
|
+
const info = providerInfo[key];
|
41
|
+
body.push(w.importStmt([info.classname], `./${info.basename}`));
|
42
|
+
}
|
43
|
+
}
|
44
|
+
body.push(w.importStmt([pascal(name)], `./${GetLocalBaseNameByContractName(name)}`));
|
45
|
+
}
|
46
|
+
}
|
47
|
+
body.push(w.createIContractsContext(providerInfos));
|
48
|
+
body.push(w.createGettingProviders(providerInfos));
|
49
|
+
return [
|
50
|
+
{
|
51
|
+
type: "plugin",
|
52
|
+
pluginType: "contractContextProviders",
|
53
|
+
localname,
|
54
|
+
body,
|
55
|
+
},
|
56
|
+
];
|
57
|
+
}
|
58
|
+
}
|
@@ -0,0 +1,73 @@
|
|
1
|
+
import { pascal } from "case";
|
2
|
+
import * as w from "wasm-ast-types";
|
3
|
+
import { RenderContext } from "wasm-ast-types";
|
4
|
+
import { BuilderPluginBase } from "./plugin-base";
|
5
|
+
export const GetLocalNameByContractName = (name) => `${pascal(name)}.provider.ts`;
|
6
|
+
export const GetLocalBaseNameByContractName = (name) => `${pascal(name)}.provider`;
|
7
|
+
export class ContractsContextProviderPlugin extends BuilderPluginBase {
|
8
|
+
constructor(opt) {
|
9
|
+
super(opt);
|
10
|
+
this.utils = {
|
11
|
+
ContractBase: "__contractContextBase__",
|
12
|
+
IContractConstructor: "__contractContextBase__",
|
13
|
+
IEmptyClient: "__contractContextBase__",
|
14
|
+
};
|
15
|
+
}
|
16
|
+
initContext(contract, options) {
|
17
|
+
return new RenderContext(contract, options, this.builder.builderContext);
|
18
|
+
}
|
19
|
+
async doRender(name, context) {
|
20
|
+
if (!this.option?.useContractsHooks?.enabled) {
|
21
|
+
return;
|
22
|
+
}
|
23
|
+
const providerInfo = context.getProviderInfos()[name];
|
24
|
+
if (!Object.keys(providerInfo || {})?.length) {
|
25
|
+
return;
|
26
|
+
}
|
27
|
+
context.addUtil("ContractBase");
|
28
|
+
context.addUtil("IContractConstructor");
|
29
|
+
const localname = GetLocalNameByContractName(name);
|
30
|
+
let needEmptyClientType = false;
|
31
|
+
let clientFile = null;
|
32
|
+
let clientClasses = [];
|
33
|
+
const body = [];
|
34
|
+
const signClientProviderInfo = providerInfo[w.PROVIDER_TYPES.SIGNING_CLIENT_TYPE];
|
35
|
+
if (signClientProviderInfo) {
|
36
|
+
clientFile = `./${signClientProviderInfo.basename}`;
|
37
|
+
clientClasses.push(signClientProviderInfo.classname);
|
38
|
+
}
|
39
|
+
else {
|
40
|
+
needEmptyClientType = true;
|
41
|
+
}
|
42
|
+
const queryClientProviderInfo = providerInfo[w.PROVIDER_TYPES.QUERY_CLIENT_TYPE];
|
43
|
+
if (queryClientProviderInfo) {
|
44
|
+
clientFile = `./${queryClientProviderInfo.basename}`;
|
45
|
+
clientClasses.push(queryClientProviderInfo.classname);
|
46
|
+
}
|
47
|
+
else {
|
48
|
+
needEmptyClientType = true;
|
49
|
+
}
|
50
|
+
if (clientFile) {
|
51
|
+
body.push(w.importStmt(clientClasses, clientFile));
|
52
|
+
}
|
53
|
+
const messageComposerProviderInfo = providerInfo[w.PROVIDER_TYPES.MESSAGE_COMPOSER_TYPE];
|
54
|
+
if (messageComposerProviderInfo) {
|
55
|
+
body.push(w.importStmt([messageComposerProviderInfo.classname], `./${messageComposerProviderInfo.basename}`));
|
56
|
+
}
|
57
|
+
else {
|
58
|
+
needEmptyClientType = true;
|
59
|
+
}
|
60
|
+
if (needEmptyClientType) {
|
61
|
+
context.addUtil("IEmptyClient");
|
62
|
+
}
|
63
|
+
body.push(w.createProvider(name, providerInfo));
|
64
|
+
return [
|
65
|
+
{
|
66
|
+
type: "plugin",
|
67
|
+
pluginType: "provider",
|
68
|
+
localname,
|
69
|
+
body,
|
70
|
+
},
|
71
|
+
];
|
72
|
+
}
|
73
|
+
}
|
@@ -0,0 +1,69 @@
|
|
1
|
+
import { pascal } from 'case';
|
2
|
+
import * as w from 'wasm-ast-types';
|
3
|
+
import { findAndParseTypes, findExecuteMsg, findQueryMsg } from '../utils';
|
4
|
+
import { getMessageProperties, RenderContext } from 'wasm-ast-types';
|
5
|
+
import { BuilderPluginBase } from './plugin-base';
|
6
|
+
export class ReactQueryPlugin extends BuilderPluginBase {
|
7
|
+
initContext(contract, options) {
|
8
|
+
return new RenderContext(contract, options, this.builder.builderContext);
|
9
|
+
}
|
10
|
+
async doRender(name, context) {
|
11
|
+
const options = this.option.reactQuery;
|
12
|
+
const { enabled } = options;
|
13
|
+
if (!enabled) {
|
14
|
+
return;
|
15
|
+
}
|
16
|
+
const { schemas } = context.contract;
|
17
|
+
const localname = pascal(`${name}`) + '.react-query.ts';
|
18
|
+
const ContractFile = pascal(`${name}`) + '.client';
|
19
|
+
const TypesFile = pascal(`${name}`) + '.types';
|
20
|
+
const QueryMsg = findQueryMsg(schemas);
|
21
|
+
const ExecuteMsg = findExecuteMsg(schemas);
|
22
|
+
const typeHash = await findAndParseTypes(schemas);
|
23
|
+
const ExecuteClient = pascal(`${name}Client`);
|
24
|
+
const QueryClient = pascal(`${name}QueryClient`);
|
25
|
+
const body = [];
|
26
|
+
const clientImports = [];
|
27
|
+
QueryMsg && clientImports.push(QueryClient);
|
28
|
+
// check that there are commands within the exec msg
|
29
|
+
const shouldGenerateMutationHooks = ExecuteMsg &&
|
30
|
+
options?.version === 'v4' &&
|
31
|
+
options?.mutations &&
|
32
|
+
getMessageProperties(ExecuteMsg).length > 0;
|
33
|
+
if (shouldGenerateMutationHooks) {
|
34
|
+
clientImports.push(ExecuteClient);
|
35
|
+
}
|
36
|
+
// general contract imports
|
37
|
+
body.push(w.importStmt(Object.keys(typeHash), `./${TypesFile}`));
|
38
|
+
// client imports
|
39
|
+
body.push(w.importStmt(clientImports, `./${ContractFile}`));
|
40
|
+
// query messages
|
41
|
+
if (QueryMsg) {
|
42
|
+
[].push.apply(body, w.createReactQueryHooks({
|
43
|
+
context,
|
44
|
+
queryMsg: QueryMsg,
|
45
|
+
contractName: name,
|
46
|
+
QueryClient
|
47
|
+
}));
|
48
|
+
}
|
49
|
+
if (shouldGenerateMutationHooks) {
|
50
|
+
[].push.apply(body, w.createReactQueryMutationHooks({
|
51
|
+
context,
|
52
|
+
execMsg: ExecuteMsg,
|
53
|
+
contractName: name,
|
54
|
+
ExecuteClient
|
55
|
+
}));
|
56
|
+
}
|
57
|
+
if (typeHash.hasOwnProperty('Coin')) {
|
58
|
+
// @ts-ignore
|
59
|
+
delete context.utils.Coin;
|
60
|
+
}
|
61
|
+
return [
|
62
|
+
{
|
63
|
+
type: 'react-query',
|
64
|
+
localname,
|
65
|
+
body
|
66
|
+
}
|
67
|
+
];
|
68
|
+
}
|
69
|
+
}
|
@@ -0,0 +1,51 @@
|
|
1
|
+
import { pascal } from 'case';
|
2
|
+
import * as w from 'wasm-ast-types';
|
3
|
+
import { findAndParseTypes, findQueryMsg } from '../utils';
|
4
|
+
import { RenderContext } from 'wasm-ast-types';
|
5
|
+
import { BuilderPluginBase } from './plugin-base';
|
6
|
+
export class RecoilPlugin extends BuilderPluginBase {
|
7
|
+
utils = {
|
8
|
+
selectorFamily: 'recoil',
|
9
|
+
};
|
10
|
+
initContext(contract, options) {
|
11
|
+
return new RenderContext(contract, options, this.builder.builderContext);
|
12
|
+
}
|
13
|
+
async doRender(name, context) {
|
14
|
+
const { enabled } = this.option.recoil;
|
15
|
+
if (!enabled) {
|
16
|
+
return;
|
17
|
+
}
|
18
|
+
const { schemas } = context.contract;
|
19
|
+
const localname = pascal(name) + '.recoil.ts';
|
20
|
+
const ContractFile = pascal(name) + '.client';
|
21
|
+
const TypesFile = pascal(name) + '.types';
|
22
|
+
const QueryMsg = findQueryMsg(schemas);
|
23
|
+
const typeHash = await findAndParseTypes(schemas);
|
24
|
+
let QueryClient = null;
|
25
|
+
let ReadOnlyInstance = null;
|
26
|
+
const body = [];
|
27
|
+
body.push(w.importStmt(['cosmWasmClient'], './chain'));
|
28
|
+
body.push(w.importStmt(Object.keys(typeHash), `./${TypesFile}`));
|
29
|
+
// query messages
|
30
|
+
if (QueryMsg) {
|
31
|
+
QueryClient = pascal(`${name}QueryClient`);
|
32
|
+
ReadOnlyInstance = pascal(`${name}ReadOnlyInterface`);
|
33
|
+
body.push(w.importStmt([QueryClient], `./${ContractFile}`));
|
34
|
+
body.push(w.createRecoilQueryClientType());
|
35
|
+
body.push(w.createRecoilQueryClient(context, name, QueryClient));
|
36
|
+
const selectors = w.createRecoilSelectors(context, name, QueryClient, QueryMsg);
|
37
|
+
body.push(...selectors);
|
38
|
+
}
|
39
|
+
if (typeHash.hasOwnProperty('Coin')) {
|
40
|
+
// @ts-ignore
|
41
|
+
delete context.utils.Coin;
|
42
|
+
}
|
43
|
+
return [
|
44
|
+
{
|
45
|
+
type: 'recoil',
|
46
|
+
localname,
|
47
|
+
body
|
48
|
+
}
|
49
|
+
];
|
50
|
+
}
|
51
|
+
}
|
@@ -0,0 +1,38 @@
|
|
1
|
+
import * as t from '@babel/types';
|
2
|
+
import { clean } from '../utils/clean';
|
3
|
+
import { pascal } from 'case';
|
4
|
+
import { findExecuteMsg, findAndParseTypes } from '../utils';
|
5
|
+
import { RenderContext } from 'wasm-ast-types';
|
6
|
+
import { BuilderPluginBase } from './plugin-base';
|
7
|
+
export class TypesPlugin extends BuilderPluginBase {
|
8
|
+
initContext(contract, options) {
|
9
|
+
return new RenderContext(contract, options, this.builder.builderContext);
|
10
|
+
}
|
11
|
+
async doRender(name, context) {
|
12
|
+
const { enabled } = this.option.types;
|
13
|
+
if (!enabled) {
|
14
|
+
return;
|
15
|
+
}
|
16
|
+
const { schemas } = context.contract;
|
17
|
+
const options = this.option.types;
|
18
|
+
const localname = pascal(name) + '.types.ts';
|
19
|
+
const ExecuteMsg = findExecuteMsg(schemas);
|
20
|
+
const typeHash = await findAndParseTypes(schemas);
|
21
|
+
const body = [];
|
22
|
+
// TYPES
|
23
|
+
Object.values(typeHash).forEach((type) => {
|
24
|
+
body.push(clean(type));
|
25
|
+
});
|
26
|
+
// alias the ExecuteMsg
|
27
|
+
if (options.aliasExecuteMsg && ExecuteMsg) {
|
28
|
+
body.push(t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier(`${name}ExecuteMsg`), null, t.tsTypeReference(t.identifier('ExecuteMsg')))));
|
29
|
+
}
|
30
|
+
return [
|
31
|
+
{
|
32
|
+
type: 'type',
|
33
|
+
localname,
|
34
|
+
body
|
35
|
+
}
|
36
|
+
];
|
37
|
+
}
|
38
|
+
}
|
@@ -1,15 +1,14 @@
|
|
1
1
|
export const clean = (obj) => {
|
2
2
|
var copy;
|
3
3
|
// Handle the 3 simple types, and null or undefined
|
4
|
-
if (null == obj || 'object' != typeof obj)
|
5
|
-
|
4
|
+
if (null == obj || 'object' != typeof obj)
|
5
|
+
return obj;
|
6
6
|
// Handle Date
|
7
7
|
if (obj instanceof Date) {
|
8
8
|
copy = new Date();
|
9
9
|
copy.setTime(obj.getTime());
|
10
10
|
return copy;
|
11
11
|
}
|
12
|
-
|
13
12
|
// Handle Array
|
14
13
|
if (obj instanceof Array) {
|
15
14
|
copy = [];
|
@@ -18,7 +17,6 @@ export const clean = (obj) => {
|
|
18
17
|
}
|
19
18
|
return copy;
|
20
19
|
}
|
21
|
-
|
22
20
|
// Handle Object
|
23
21
|
if (obj instanceof Object || typeof obj === 'object') {
|
24
22
|
copy = {};
|
@@ -32,14 +30,16 @@ export const clean = (obj) => {
|
|
32
30
|
case 'end':
|
33
31
|
break;
|
34
32
|
default:
|
33
|
+
// @ts-ignore
|
35
34
|
copy[attr] = clean(obj[attr]);
|
36
35
|
}
|
37
|
-
}
|
36
|
+
}
|
37
|
+
else {
|
38
|
+
// @ts-ignore
|
38
39
|
copy[attr] = clean(obj[attr]);
|
39
40
|
}
|
40
41
|
}
|
41
42
|
return copy;
|
42
43
|
}
|
43
|
-
|
44
44
|
throw new Error("Unable to copy obj! Its type isn't supported.");
|
45
|
-
};
|
45
|
+
};
|
@@ -1,5 +1,4 @@
|
|
1
1
|
import { pascal } from "case";
|
2
|
-
|
3
2
|
const cleanFor = (str) => {
|
4
3
|
/*
|
5
4
|
1. look at first char after _for_
|
@@ -9,30 +8,25 @@ const cleanFor = (str) => {
|
|
9
8
|
const m = str.match(/(_[a-z]+_)[A-Z]/);
|
10
9
|
str = str.replace(m[1], pascal(m[1]));
|
11
10
|
}
|
12
|
-
|
13
11
|
return str;
|
14
12
|
};
|
15
|
-
|
16
13
|
const cleanNullable = (str) => {
|
17
14
|
if (/^Nullable_/.test(str)) {
|
18
15
|
str = str.replace(/^Nullable_/, 'Nullable');
|
19
16
|
}
|
20
|
-
|
21
17
|
return str;
|
22
18
|
};
|
23
|
-
|
24
19
|
export const cleanse = (obj) => {
|
25
20
|
var copy;
|
26
21
|
// Handle the 3 simple types, and null or undefined
|
27
|
-
if (null == obj || 'object' != typeof obj)
|
28
|
-
|
22
|
+
if (null == obj || 'object' != typeof obj)
|
23
|
+
return obj;
|
29
24
|
// Handle Date
|
30
25
|
if (obj instanceof Date) {
|
31
26
|
copy = new Date();
|
32
27
|
copy.setTime(obj.getTime());
|
33
28
|
return copy;
|
34
29
|
}
|
35
|
-
|
36
30
|
// Handle Array
|
37
31
|
if (obj instanceof Array) {
|
38
32
|
copy = [];
|
@@ -41,11 +35,9 @@ export const cleanse = (obj) => {
|
|
41
35
|
}
|
42
36
|
return copy;
|
43
37
|
}
|
44
|
-
|
45
38
|
// Handle Object
|
46
39
|
if (obj instanceof Object || typeof obj === 'object') {
|
47
40
|
copy = {};
|
48
|
-
|
49
41
|
// https://github.com/CosmWasm/cosmwasm-typescript-gen/issues/27
|
50
42
|
if (Array.isArray(obj.enum) && obj.enum.length === 0) {
|
51
43
|
delete obj.enum;
|
@@ -53,37 +45,41 @@ export const cleanse = (obj) => {
|
|
53
45
|
obj.type = 'string';
|
54
46
|
}
|
55
47
|
}
|
56
|
-
|
57
48
|
for (var attr in obj) {
|
58
49
|
if (obj.hasOwnProperty(attr)) {
|
59
|
-
|
60
50
|
if (/_for_/.test(attr)) {
|
51
|
+
// @ts-ignore
|
61
52
|
copy[cleanFor(attr)] = cleanse(obj[attr]);
|
62
|
-
}
|
53
|
+
}
|
54
|
+
else if (/^Nullable_/.test(attr)) {
|
55
|
+
// @ts-ignore
|
63
56
|
copy[cleanNullable(attr)] = cleanse(obj[attr]);
|
64
|
-
}
|
57
|
+
}
|
58
|
+
else {
|
65
59
|
switch (attr) {
|
66
60
|
case 'title':
|
67
61
|
case '$ref':
|
68
62
|
if (typeof obj[attr] === 'string') {
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
63
|
+
// @ts-ignore
|
64
|
+
copy[attr] = cleanse(cleanNullable(cleanFor(obj[attr])));
|
65
|
+
}
|
66
|
+
else {
|
67
|
+
// @ts-ignore
|
73
68
|
copy[attr] = cleanse(obj[attr]);
|
74
69
|
}
|
75
70
|
break;
|
76
71
|
default:
|
72
|
+
// @ts-ignore
|
77
73
|
copy[attr] = cleanse(obj[attr]);
|
78
74
|
}
|
79
75
|
}
|
80
|
-
|
81
|
-
|
76
|
+
}
|
77
|
+
else {
|
78
|
+
// @ts-ignore
|
82
79
|
copy[attr] = cleanse(obj[attr]);
|
83
80
|
}
|
84
81
|
}
|
85
82
|
return copy;
|
86
83
|
}
|
87
|
-
|
88
84
|
throw new Error("Unable to copy obj! Its type isn't supported.");
|
89
|
-
};
|
85
|
+
};
|
@@ -0,0 +1,42 @@
|
|
1
|
+
import * as t from "@babel/types";
|
2
|
+
import { parse } from "@babel/parser";
|
3
|
+
import { sync as mkdirp } from "mkdirp";
|
4
|
+
import { writeFileSync } from "fs";
|
5
|
+
import { dirname } from "path";
|
6
|
+
import generate from "@babel/generator";
|
7
|
+
import { unused } from "./unused";
|
8
|
+
import traverse from "@babel/traverse";
|
9
|
+
export const writeAstToFile = (outPath, program, filename, removeUnusedImports = false, isTsDisable = false, isEslintDisable = false) => {
|
10
|
+
const ast = t.program(program);
|
11
|
+
const content = generate(ast).code;
|
12
|
+
if (removeUnusedImports) {
|
13
|
+
const plugins = ["typescript"];
|
14
|
+
const newAst = parse(content, {
|
15
|
+
sourceType: "module",
|
16
|
+
plugins,
|
17
|
+
});
|
18
|
+
traverse(newAst, unused);
|
19
|
+
const content2 = generate(newAst).code;
|
20
|
+
writeContentToFile(outPath, content2, filename, isTsDisable, isEslintDisable);
|
21
|
+
}
|
22
|
+
else {
|
23
|
+
writeContentToFile(outPath, content, filename, isTsDisable, isEslintDisable);
|
24
|
+
}
|
25
|
+
};
|
26
|
+
export const writeContentToFile = (outPath, content, filename, isTsDisable = false, isEslintDisable = false) => {
|
27
|
+
let esLintPrefix = "";
|
28
|
+
let tsLintPrefix = "";
|
29
|
+
let nameWithoutPath = filename.replace(outPath, "");
|
30
|
+
// strip off leading slash
|
31
|
+
if (nameWithoutPath.startsWith("/"))
|
32
|
+
nameWithoutPath = nameWithoutPath.replace(/^\//, "");
|
33
|
+
if (isTsDisable) {
|
34
|
+
tsLintPrefix = `//@ts-nocheck\n`;
|
35
|
+
}
|
36
|
+
if (isEslintDisable) {
|
37
|
+
esLintPrefix = `/* eslint-disable */\n`;
|
38
|
+
}
|
39
|
+
const text = tsLintPrefix + esLintPrefix + content;
|
40
|
+
mkdirp(dirname(filename));
|
41
|
+
writeFileSync(filename, text);
|
42
|
+
};
|
@@ -1,32 +1,24 @@
|
|
1
1
|
import babelTraverse from '@babel/traverse';
|
2
|
-
import { parse
|
3
|
-
|
2
|
+
import { parse } from '@babel/parser';
|
4
3
|
export const parser = (codes) => {
|
5
|
-
|
6
4
|
const hash = {};
|
7
5
|
codes.forEach(code => {
|
8
|
-
|
9
|
-
const plugins: ParserPlugin[] = [
|
6
|
+
const plugins = [
|
10
7
|
'typescript',
|
11
8
|
];
|
12
|
-
|
13
9
|
const ast = parse(code, {
|
14
10
|
sourceType: 'module',
|
15
11
|
plugins
|
16
12
|
});
|
17
|
-
|
18
13
|
const visitor = visitorFn({
|
19
14
|
addType(key, node) {
|
20
15
|
hash[key] = node;
|
21
16
|
}
|
22
|
-
})
|
17
|
+
});
|
23
18
|
babelTraverse(ast, visitor);
|
24
19
|
});
|
25
|
-
|
26
20
|
return hash;
|
27
|
-
|
28
|
-
}
|
29
|
-
|
21
|
+
};
|
30
22
|
const visitorFn = (parser) => ({
|
31
23
|
TSTypeAliasDeclaration(path) {
|
32
24
|
parser.addType(path.node.id.name, path.parentPath.node);
|
@@ -42,4 +34,3 @@ const visitorFn = (parser) => ({
|
|
42
34
|
parser.addType(path.node.id.name, path.parentPath.node);
|
43
35
|
}
|
44
36
|
});
|
45
|
-
|