@cosmwasm/ts-codegen 0.25.2 → 0.26.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/main/builder/builder.js +165 -254
- package/main/commands/generate.js +3 -0
- package/main/generators/msg-builder.js +113 -0
- package/main/index.js +14 -0
- package/main/plugins/client.js +141 -0
- package/main/plugins/index.js +18 -0
- package/main/plugins/message-composer.js +128 -0
- package/main/plugins/msg-builder.js +139 -0
- package/main/plugins/plugin-base.js +126 -0
- package/main/plugins/react-query.js +152 -0
- package/main/plugins/recoil.js +145 -0
- package/main/plugins/types.js +123 -0
- package/module/builder/builder.js +63 -109
- package/module/commands/generate.js +3 -0
- package/module/generators/msg-builder.js +62 -0
- package/module/index.js +1 -0
- package/module/plugins/client.js +66 -0
- package/module/plugins/index.js +1 -0
- package/module/plugins/message-composer.js +53 -0
- package/module/plugins/msg-builder.js +63 -0
- package/module/plugins/plugin-base.js +59 -0
- package/module/plugins/react-query.js +77 -0
- package/module/plugins/recoil.js +64 -0
- package/module/plugins/types.js +45 -0
- package/package.json +3 -3
- package/src/builder/builder.ts +82 -82
- package/src/commands/generate.ts +3 -0
- package/src/generators/msg-builder.ts +80 -0
- package/src/index.ts +1 -0
- package/src/plugins/client.ts +108 -0
- package/src/plugins/index.ts +1 -0
- package/src/plugins/message-composer.ts +80 -0
- package/src/plugins/msg-builder.ts +85 -0
- package/src/plugins/plugin-base.ts +112 -0
- package/src/plugins/react-query.ts +115 -0
- package/src/plugins/recoil.ts +89 -0
- package/src/plugins/types.ts +74 -0
- package/types/src/builder/builder.d.ts +13 -9
- package/types/src/generators/msg-builder.d.ts +5 -0
- package/types/src/generators/msg-builder.ts +5 -0
- package/types/src/index.d.ts +1 -0
- package/types/src/plugins/client.d.ts +12 -0
- package/types/src/plugins/index.d.ts +1 -0
- package/types/src/plugins/message-composer.d.ts +12 -0
- package/types/src/plugins/msg-builder.d.ts +12 -0
- package/types/src/plugins/plugin-base.d.ts +47 -0
- package/types/src/plugins/react-query.d.ts +12 -0
- package/types/src/plugins/recoil.d.ts +13 -0
- package/types/src/plugins/types.d.ts +12 -0
@@ -1,20 +1,9 @@
|
|
1
|
-
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
2
1
|
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
3
|
-
const _excluded = ["enabled"],
|
4
|
-
_excluded2 = ["enabled"],
|
5
|
-
_excluded3 = ["enabled"],
|
6
|
-
_excluded4 = ["enabled"],
|
7
|
-
_excluded5 = ["enabled"];
|
8
2
|
import { defaultOptions } from "wasm-ast-types";
|
9
3
|
import { header } from '../utils/header';
|
10
4
|
import { join } from "path";
|
11
5
|
import { writeFileSync } from 'fs';
|
12
6
|
import { sync as mkdirp } from "mkdirp";
|
13
|
-
import generateMessageComposer from '../generators/message-composer';
|
14
|
-
import generateTypes from '../generators/types';
|
15
|
-
import generateReactQuery from '../generators/react-query';
|
16
|
-
import generateRecoil from '../generators/recoil';
|
17
|
-
import generateClient from '../generators/client';
|
18
7
|
import { basename } from 'path';
|
19
8
|
import { readSchemas } from '../utils';
|
20
9
|
import deepmerge from 'deepmerge';
|
@@ -22,6 +11,12 @@ import { pascal } from "case";
|
|
22
11
|
import { createFileBundle, recursiveModuleBundle } from "../bundler";
|
23
12
|
import generate from '@babel/generator';
|
24
13
|
import * as t from '@babel/types';
|
14
|
+
import { ReactQueryPlugin } from "../plugins/react-query";
|
15
|
+
import { RecoilPlugin } from "../plugins/recoil";
|
16
|
+
import { MsgBuilderPlugin } from "../plugins/msg-builder";
|
17
|
+
import { MessageComposerPlugin } from "../plugins/message-composer";
|
18
|
+
import { ClientPlugin } from "../plugins/client";
|
19
|
+
import { TypesPlugin } from "../plugins/types";
|
25
20
|
const defaultOpts = {
|
26
21
|
bundle: {
|
27
22
|
enabled: true,
|
@@ -32,11 +27,33 @@ const defaultOpts = {
|
|
32
27
|
;
|
33
28
|
;
|
34
29
|
;
|
30
|
+
|
31
|
+
function getContract(contractOpt) {
|
32
|
+
if (typeof contractOpt === 'string') {
|
33
|
+
const name = basename(contractOpt);
|
34
|
+
const contractName = pascal(name);
|
35
|
+
return {
|
36
|
+
name: contractName,
|
37
|
+
dir: contractOpt
|
38
|
+
};
|
39
|
+
}
|
40
|
+
|
41
|
+
return {
|
42
|
+
name: pascal(contractOpt.name),
|
43
|
+
dir: contractOpt.dir
|
44
|
+
};
|
45
|
+
}
|
46
|
+
|
35
47
|
export class TSBuilder {
|
48
|
+
loadDefaultPlugins() {
|
49
|
+
[].push.apply(this.plugins, [new TypesPlugin(this.options), new ClientPlugin(this.options), new MessageComposerPlugin(this.options), new ReactQueryPlugin(this.options), new RecoilPlugin(this.options), new MsgBuilderPlugin(this.options)]);
|
50
|
+
}
|
51
|
+
|
36
52
|
constructor({
|
37
53
|
contracts,
|
38
54
|
outPath,
|
39
|
-
options
|
55
|
+
options,
|
56
|
+
plugins
|
40
57
|
}) {
|
41
58
|
_defineProperty(this, "contracts", void 0);
|
42
59
|
|
@@ -44,118 +61,49 @@ export class TSBuilder {
|
|
44
61
|
|
45
62
|
_defineProperty(this, "options", void 0);
|
46
63
|
|
64
|
+
_defineProperty(this, "plugins", []);
|
65
|
+
|
47
66
|
_defineProperty(this, "files", []);
|
48
67
|
|
49
68
|
this.contracts = contracts;
|
50
69
|
this.outPath = outPath;
|
51
70
|
this.options = deepmerge(deepmerge(defaultOptions, defaultOpts), options ?? {});
|
52
|
-
|
71
|
+
this.loadDefaultPlugins();
|
53
72
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
const name = basename(contractOpt);
|
58
|
-
const contractName = pascal(name);
|
59
|
-
return {
|
60
|
-
name: contractName,
|
61
|
-
dir: contractOpt
|
62
|
-
};
|
63
|
-
}
|
64
|
-
|
65
|
-
return {
|
66
|
-
name: pascal(contractOpt.name),
|
67
|
-
dir: contractOpt.dir
|
68
|
-
};
|
69
|
-
});
|
70
|
-
}
|
71
|
-
|
72
|
-
async renderTypes(contract) {
|
73
|
-
const _this$options$types = this.options.types,
|
74
|
-
{
|
75
|
-
enabled
|
76
|
-
} = _this$options$types,
|
77
|
-
options = _objectWithoutProperties(_this$options$types, _excluded);
|
78
|
-
|
79
|
-
if (!enabled) return;
|
80
|
-
const contractInfo = await readSchemas({
|
81
|
-
schemaDir: contract.dir
|
82
|
-
});
|
83
|
-
const files = await generateTypes(contract.name, contractInfo, this.outPath, options);
|
84
|
-
[].push.apply(this.files, files);
|
73
|
+
if (plugins && plugins.length) {
|
74
|
+
[].push.apply(this.plugins, plugins);
|
75
|
+
}
|
85
76
|
}
|
86
77
|
|
87
|
-
async
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
} = _this$options$client,
|
92
|
-
options = _objectWithoutProperties(_this$options$client, _excluded2);
|
93
|
-
|
94
|
-
if (!enabled) return;
|
95
|
-
const contractInfo = await readSchemas({
|
96
|
-
schemaDir: contract.dir
|
97
|
-
});
|
98
|
-
const files = await generateClient(contract.name, contractInfo, this.outPath, options);
|
99
|
-
[].push.apply(this.files, files);
|
100
|
-
}
|
78
|
+
async build() {
|
79
|
+
await this.process();
|
80
|
+
await this.after();
|
81
|
+
} // lifecycle functions
|
101
82
|
|
102
|
-
async renderRecoil(contract) {
|
103
|
-
const _this$options$recoil = this.options.recoil,
|
104
|
-
{
|
105
|
-
enabled
|
106
|
-
} = _this$options$recoil,
|
107
|
-
options = _objectWithoutProperties(_this$options$recoil, _excluded3);
|
108
83
|
|
109
|
-
|
110
|
-
const
|
111
|
-
|
112
|
-
});
|
113
|
-
const files = await generateRecoil(contract.name, contractInfo, this.outPath, options);
|
114
|
-
[].push.apply(this.files, files);
|
115
|
-
}
|
84
|
+
async process() {
|
85
|
+
for (const contractOpt of this.contracts) {
|
86
|
+
const contract = getContract(contractOpt); //resolve contract schema.
|
116
87
|
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
enabled
|
121
|
-
} = _this$options$reactQu,
|
122
|
-
options = _objectWithoutProperties(_this$options$reactQu, _excluded4);
|
88
|
+
const contractInfo = await readSchemas({
|
89
|
+
schemaDir: contract.dir
|
90
|
+
}); //lifecycle and plugins.
|
123
91
|
|
124
|
-
|
125
|
-
|
126
|
-
schemaDir: contract.dir
|
127
|
-
});
|
128
|
-
const files = await generateReactQuery(contract.name, contractInfo, this.outPath, options);
|
129
|
-
[].push.apply(this.files, files);
|
92
|
+
await this.render(contract.name, contractInfo);
|
93
|
+
}
|
130
94
|
}
|
131
95
|
|
132
|
-
async
|
133
|
-
const
|
134
|
-
|
135
|
-
enabled
|
136
|
-
} = _this$options$message,
|
137
|
-
options = _objectWithoutProperties(_this$options$message, _excluded5);
|
138
|
-
|
139
|
-
if (!enabled) return;
|
140
|
-
const contractInfo = await readSchemas({
|
141
|
-
schemaDir: contract.dir
|
142
|
-
});
|
143
|
-
const files = await generateMessageComposer(contract.name, contractInfo, this.outPath, options);
|
144
|
-
[].push.apply(this.files, files);
|
145
|
-
}
|
96
|
+
async render(name, contractInfo) {
|
97
|
+
for (const plugin of this.plugins) {
|
98
|
+
let files = await plugin.render(name, contractInfo, this.outPath);
|
146
99
|
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
for (let c = 0; c < contracts.length; c++) {
|
151
|
-
const contract = contracts[c];
|
152
|
-
await this.renderTypes(contract);
|
153
|
-
await this.renderClient(contract);
|
154
|
-
await this.renderMessageComposer(contract);
|
155
|
-
await this.renderReactQuery(contract);
|
156
|
-
await this.renderRecoil(contract);
|
100
|
+
if (files && files.length) {
|
101
|
+
[].push.apply(this.files, files);
|
102
|
+
}
|
157
103
|
}
|
104
|
+
}
|
158
105
|
|
106
|
+
async after() {
|
159
107
|
if (this.options.bundle.enabled) {
|
160
108
|
this.bundle();
|
161
109
|
}
|
@@ -164,16 +112,22 @@ export class TSBuilder {
|
|
164
112
|
async bundle() {
|
165
113
|
const allFiles = this.files;
|
166
114
|
const bundleFile = this.options.bundle.bundleFile;
|
115
|
+
const bundlePath = join(this.options?.bundle?.bundlePath ?? this.outPath, bundleFile);
|
167
116
|
const bundleVariables = {};
|
168
117
|
const importPaths = [];
|
169
118
|
allFiles.forEach(file => {
|
170
|
-
createFileBundle(`${this.options.bundle.scope}.${file.contract}`, file.
|
119
|
+
createFileBundle(`${this.options.bundle.scope}.${file.contract}`, file.filename, bundlePath, importPaths, bundleVariables);
|
171
120
|
});
|
172
121
|
const ast = recursiveModuleBundle(bundleVariables);
|
173
122
|
let code = generate(t.program([...importPaths, ...ast])).code;
|
123
|
+
|
124
|
+
if (this.options?.bundle?.bundlePath) {
|
125
|
+
mkdirp(this.options?.bundle?.bundlePath);
|
126
|
+
}
|
127
|
+
|
174
128
|
mkdirp(this.outPath);
|
175
129
|
if (code.trim() === '') code = 'export {};';
|
176
|
-
writeFileSync(
|
130
|
+
writeFileSync(bundlePath, header + code);
|
177
131
|
}
|
178
132
|
|
179
133
|
}
|
@@ -0,0 +1,62 @@
|
|
1
|
+
import { pascal } from "case";
|
2
|
+
import { header } from "../utils/header";
|
3
|
+
import { join } from "path";
|
4
|
+
import { sync as mkdirp } from "mkdirp";
|
5
|
+
import * as w from "wasm-ast-types";
|
6
|
+
import * as t from "@babel/types";
|
7
|
+
import { writeFileSync } from "fs";
|
8
|
+
import generate from "@babel/generator";
|
9
|
+
import { getMessageProperties } from "wasm-ast-types";
|
10
|
+
import { findAndParseTypes, findExecuteMsg, findQueryMsg } from '../utils';
|
11
|
+
import { RenderContext } from 'wasm-ast-types';
|
12
|
+
export default (async (name, contractInfo, outPath, msgBuilderOptions) => {
|
13
|
+
const {
|
14
|
+
schemas
|
15
|
+
} = contractInfo;
|
16
|
+
const context = new RenderContext(contractInfo, {
|
17
|
+
msgBuilder: msgBuilderOptions ?? {}
|
18
|
+
});
|
19
|
+
const localname = pascal(name) + ".msg-builder.ts";
|
20
|
+
const TypesFile = pascal(name) + ".types";
|
21
|
+
const ExecuteMsg = findExecuteMsg(schemas);
|
22
|
+
const typeHash = await findAndParseTypes(schemas);
|
23
|
+
const body = [];
|
24
|
+
body.push(w.importStmt(Object.keys(typeHash), `./${TypesFile}`));
|
25
|
+
body.push(w.importStmt(["CamelCasedProperties"], "type-fest")); // execute messages
|
26
|
+
|
27
|
+
if (ExecuteMsg) {
|
28
|
+
const children = getMessageProperties(ExecuteMsg);
|
29
|
+
|
30
|
+
if (children.length > 0) {
|
31
|
+
const className = pascal(`${name}ExecuteMsgBuilder`);
|
32
|
+
body.push(w.createMsgBuilderClass(context, className, ExecuteMsg));
|
33
|
+
}
|
34
|
+
}
|
35
|
+
|
36
|
+
const QueryMsg = findQueryMsg(schemas); // query messages
|
37
|
+
|
38
|
+
if (QueryMsg) {
|
39
|
+
const children = getMessageProperties(QueryMsg);
|
40
|
+
|
41
|
+
if (children.length > 0) {
|
42
|
+
const className = pascal(`${name}QueryMsgBuilder`);
|
43
|
+
body.push(w.createMsgBuilderClass(context, className, QueryMsg));
|
44
|
+
}
|
45
|
+
}
|
46
|
+
|
47
|
+
if (typeHash.hasOwnProperty("Coin")) {
|
48
|
+
// @ts-ignore
|
49
|
+
delete context.utils.Coin;
|
50
|
+
}
|
51
|
+
|
52
|
+
const imports = context.getImports();
|
53
|
+
const code = header + generate(t.program([...imports, ...body])).code;
|
54
|
+
mkdirp(outPath);
|
55
|
+
writeFileSync(join(outPath, localname), code);
|
56
|
+
return [{
|
57
|
+
type: "msg-builder",
|
58
|
+
contract: name,
|
59
|
+
localname,
|
60
|
+
filename: join(outPath, localname)
|
61
|
+
}];
|
62
|
+
});
|
package/module/index.js
CHANGED
@@ -7,6 +7,7 @@ export { default as generateRecoil } from './generators/recoil';
|
|
7
7
|
export * from './utils';
|
8
8
|
export * from './builder';
|
9
9
|
export * from './bundler';
|
10
|
+
export * from './plugins';
|
10
11
|
export default (async input => {
|
11
12
|
const builder = new TSBuilder(input);
|
12
13
|
await builder.build();
|
@@ -0,0 +1,66 @@
|
|
1
|
+
import { pascal } from 'case';
|
2
|
+
import * as w from 'wasm-ast-types';
|
3
|
+
import { findExecuteMsg, findAndParseTypes, findQueryMsg } from '../utils';
|
4
|
+
import { RenderContext, getMessageProperties } from 'wasm-ast-types';
|
5
|
+
import { BuilderPluginBase } from './plugin-base';
|
6
|
+
export class ClientPlugin extends BuilderPluginBase {
|
7
|
+
initContext(contract, options) {
|
8
|
+
return new RenderContext(contract, options);
|
9
|
+
}
|
10
|
+
|
11
|
+
async doRender(name, context) {
|
12
|
+
const {
|
13
|
+
enabled
|
14
|
+
} = this.option.client;
|
15
|
+
|
16
|
+
if (!enabled) {
|
17
|
+
return;
|
18
|
+
}
|
19
|
+
|
20
|
+
const {
|
21
|
+
schemas
|
22
|
+
} = context.contract;
|
23
|
+
const localname = pascal(name) + '.client.ts';
|
24
|
+
const TypesFile = pascal(name) + '.types';
|
25
|
+
const QueryMsg = findQueryMsg(schemas);
|
26
|
+
const ExecuteMsg = findExecuteMsg(schemas);
|
27
|
+
const typeHash = await findAndParseTypes(schemas);
|
28
|
+
let Client = null;
|
29
|
+
let Instance = null;
|
30
|
+
let QueryClient = null;
|
31
|
+
let ReadOnlyInstance = null;
|
32
|
+
const body = [];
|
33
|
+
body.push(w.importStmt(Object.keys(typeHash), `./${TypesFile}`)); // query messages
|
34
|
+
|
35
|
+
if (QueryMsg) {
|
36
|
+
QueryClient = pascal(`${name}QueryClient`);
|
37
|
+
ReadOnlyInstance = pascal(`${name}ReadOnlyInterface`);
|
38
|
+
body.push(w.createQueryInterface(context, ReadOnlyInstance, QueryMsg));
|
39
|
+
body.push(w.createQueryClass(context, QueryClient, ReadOnlyInstance, QueryMsg));
|
40
|
+
} // execute messages
|
41
|
+
|
42
|
+
|
43
|
+
if (ExecuteMsg) {
|
44
|
+
const children = getMessageProperties(ExecuteMsg);
|
45
|
+
|
46
|
+
if (children.length > 0) {
|
47
|
+
Client = pascal(`${name}Client`);
|
48
|
+
Instance = pascal(`${name}Interface`);
|
49
|
+
body.push(w.createExecuteInterface(context, Instance, this.option.client.execExtendsQuery ? ReadOnlyInstance : null, ExecuteMsg));
|
50
|
+
body.push(w.createExecuteClass(context, Client, Instance, this.option.client.execExtendsQuery ? QueryClient : null, ExecuteMsg));
|
51
|
+
}
|
52
|
+
}
|
53
|
+
|
54
|
+
if (typeHash.hasOwnProperty('Coin')) {
|
55
|
+
// @ts-ignore
|
56
|
+
delete context.utils.Coin;
|
57
|
+
}
|
58
|
+
|
59
|
+
return [{
|
60
|
+
type: 'client',
|
61
|
+
localname,
|
62
|
+
body
|
63
|
+
}];
|
64
|
+
}
|
65
|
+
|
66
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from "./plugin-base";
|
@@ -0,0 +1,53 @@
|
|
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 class MessageComposerPlugin extends BuilderPluginBase {
|
7
|
+
initContext(contract, options) {
|
8
|
+
return new RenderContext(contract, options);
|
9
|
+
}
|
10
|
+
|
11
|
+
async doRender(name, context) {
|
12
|
+
const {
|
13
|
+
enabled
|
14
|
+
} = this.option.messageComposer;
|
15
|
+
|
16
|
+
if (!enabled) {
|
17
|
+
return;
|
18
|
+
}
|
19
|
+
|
20
|
+
const {
|
21
|
+
schemas
|
22
|
+
} = context.contract;
|
23
|
+
const localname = pascal(name) + '.message-composer.ts';
|
24
|
+
const TypesFile = pascal(name) + '.types';
|
25
|
+
const ExecuteMsg = findExecuteMsg(schemas);
|
26
|
+
const typeHash = await findAndParseTypes(schemas);
|
27
|
+
const body = [];
|
28
|
+
body.push(w.importStmt(Object.keys(typeHash), `./${TypesFile}`)); // execute messages
|
29
|
+
|
30
|
+
if (ExecuteMsg) {
|
31
|
+
const children = getMessageProperties(ExecuteMsg);
|
32
|
+
|
33
|
+
if (children.length > 0) {
|
34
|
+
const TheClass = pascal(`${name}MessageComposer`);
|
35
|
+
const Interface = pascal(`${name}Message`);
|
36
|
+
body.push(w.createMessageComposerInterface(context, Interface, ExecuteMsg));
|
37
|
+
body.push(w.createMessageComposerClass(context, TheClass, Interface, ExecuteMsg));
|
38
|
+
}
|
39
|
+
}
|
40
|
+
|
41
|
+
if (typeHash.hasOwnProperty('Coin')) {
|
42
|
+
// @ts-ignore
|
43
|
+
delete context.utils.Coin;
|
44
|
+
}
|
45
|
+
|
46
|
+
return [{
|
47
|
+
type: 'message-composer',
|
48
|
+
localname,
|
49
|
+
body
|
50
|
+
}];
|
51
|
+
}
|
52
|
+
|
53
|
+
}
|
@@ -0,0 +1,63 @@
|
|
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 MsgBuilderPlugin extends BuilderPluginBase {
|
7
|
+
initContext(contract, options) {
|
8
|
+
return new RenderContext(contract, options);
|
9
|
+
}
|
10
|
+
|
11
|
+
async doRender(name, context) {
|
12
|
+
const {
|
13
|
+
enabled
|
14
|
+
} = this.option.msgBuilder;
|
15
|
+
|
16
|
+
if (!enabled) {
|
17
|
+
return;
|
18
|
+
}
|
19
|
+
|
20
|
+
const {
|
21
|
+
schemas
|
22
|
+
} = context.contract;
|
23
|
+
const localname = pascal(name) + '.msg-builder.ts';
|
24
|
+
const TypesFile = pascal(name) + '.types';
|
25
|
+
const ExecuteMsg = findExecuteMsg(schemas);
|
26
|
+
const typeHash = await findAndParseTypes(schemas);
|
27
|
+
const body = [];
|
28
|
+
body.push(w.importStmt(Object.keys(typeHash), `./${TypesFile}`));
|
29
|
+
body.push(w.importStmt(['CamelCasedProperties'], 'type-fest')); // execute messages
|
30
|
+
|
31
|
+
if (ExecuteMsg) {
|
32
|
+
const children = getMessageProperties(ExecuteMsg);
|
33
|
+
|
34
|
+
if (children.length > 0) {
|
35
|
+
const className = pascal(`${name}ExecuteMsgBuilder`);
|
36
|
+
body.push(w.createMsgBuilderClass(context, className, ExecuteMsg));
|
37
|
+
}
|
38
|
+
}
|
39
|
+
|
40
|
+
const QueryMsg = findQueryMsg(schemas); // query messages
|
41
|
+
|
42
|
+
if (QueryMsg) {
|
43
|
+
const children = getMessageProperties(QueryMsg);
|
44
|
+
|
45
|
+
if (children.length > 0) {
|
46
|
+
const className = pascal(`${name}QueryMsgBuilder`);
|
47
|
+
body.push(w.createMsgBuilderClass(context, className, QueryMsg));
|
48
|
+
}
|
49
|
+
}
|
50
|
+
|
51
|
+
if (typeHash.hasOwnProperty('Coin')) {
|
52
|
+
// @ts-ignore
|
53
|
+
delete context.utils.Coin;
|
54
|
+
}
|
55
|
+
|
56
|
+
return [{
|
57
|
+
type: 'msg-builder',
|
58
|
+
localname,
|
59
|
+
body
|
60
|
+
}];
|
61
|
+
}
|
62
|
+
|
63
|
+
}
|
@@ -0,0 +1,59 @@
|
|
1
|
+
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
2
|
+
import { sync as mkdirp } from 'mkdirp';
|
3
|
+
import { join } from 'path';
|
4
|
+
import { writeFileSync } from 'fs';
|
5
|
+
import { header } from '../utils/header';
|
6
|
+
import generate from '@babel/generator';
|
7
|
+
import * as t from '@babel/types';
|
8
|
+
|
9
|
+
/**
|
10
|
+
* BuilderPluginBase enable ts-codegen users implement their own plugins by only implement a few functions.
|
11
|
+
*/
|
12
|
+
export class BuilderPluginBase {
|
13
|
+
constructor(opt) {
|
14
|
+
_defineProperty(this, "option", void 0);
|
15
|
+
|
16
|
+
_defineProperty(this, "utils", void 0);
|
17
|
+
|
18
|
+
this.option = opt;
|
19
|
+
}
|
20
|
+
|
21
|
+
async render(name, contractInfo, outPath) {
|
22
|
+
const {
|
23
|
+
enabled
|
24
|
+
} = this.option;
|
25
|
+
|
26
|
+
if (!enabled) {
|
27
|
+
return;
|
28
|
+
}
|
29
|
+
|
30
|
+
const context = this.initContext(contractInfo, this.option);
|
31
|
+
const results = await this.doRender(name, context);
|
32
|
+
|
33
|
+
if (!results || !results.length) {
|
34
|
+
return [];
|
35
|
+
}
|
36
|
+
|
37
|
+
return results.map(result => {
|
38
|
+
const imports = context.getImports(this.utils);
|
39
|
+
const code = header + generate(t.program([...imports, ...result.body])).code;
|
40
|
+
mkdirp(outPath);
|
41
|
+
const filename = join(outPath, result.localname);
|
42
|
+
writeFileSync(filename, code);
|
43
|
+
return {
|
44
|
+
type: result.type,
|
45
|
+
pluginType: result.pluginType,
|
46
|
+
contract: name,
|
47
|
+
localname: result.localname,
|
48
|
+
filename
|
49
|
+
};
|
50
|
+
});
|
51
|
+
}
|
52
|
+
/**
|
53
|
+
* init context here
|
54
|
+
* @param contract
|
55
|
+
* @param options
|
56
|
+
*/
|
57
|
+
|
58
|
+
|
59
|
+
}
|
@@ -0,0 +1,77 @@
|
|
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);
|
9
|
+
}
|
10
|
+
|
11
|
+
async doRender(name, context) {
|
12
|
+
const options = this.option.reactQuery;
|
13
|
+
const {
|
14
|
+
enabled
|
15
|
+
} = options;
|
16
|
+
|
17
|
+
if (!enabled) {
|
18
|
+
return;
|
19
|
+
}
|
20
|
+
|
21
|
+
const {
|
22
|
+
schemas
|
23
|
+
} = context.contract;
|
24
|
+
const localname = pascal(`${name}`) + '.react-query.ts';
|
25
|
+
const ContractFile = pascal(`${name}`) + '.client';
|
26
|
+
const TypesFile = pascal(`${name}`) + '.types';
|
27
|
+
const QueryMsg = findQueryMsg(schemas);
|
28
|
+
const ExecuteMsg = findExecuteMsg(schemas);
|
29
|
+
const typeHash = await findAndParseTypes(schemas);
|
30
|
+
const ExecuteClient = pascal(`${name}Client`);
|
31
|
+
const QueryClient = pascal(`${name}QueryClient`);
|
32
|
+
const body = [];
|
33
|
+
const clientImports = [];
|
34
|
+
QueryMsg && clientImports.push(QueryClient); // check that there are commands within the exec msg
|
35
|
+
|
36
|
+
const shouldGenerateMutationHooks = ExecuteMsg && options?.version === 'v4' && options?.mutations && getMessageProperties(ExecuteMsg).length > 0;
|
37
|
+
|
38
|
+
if (shouldGenerateMutationHooks) {
|
39
|
+
clientImports.push(ExecuteClient);
|
40
|
+
} // general contract imports
|
41
|
+
|
42
|
+
|
43
|
+
body.push(w.importStmt(Object.keys(typeHash), `./${TypesFile}`)); // client imports
|
44
|
+
|
45
|
+
body.push(w.importStmt(clientImports, `./${ContractFile}`)); // query messages
|
46
|
+
|
47
|
+
if (QueryMsg) {
|
48
|
+
[].push.apply(body, w.createReactQueryHooks({
|
49
|
+
context,
|
50
|
+
queryMsg: QueryMsg,
|
51
|
+
contractName: name,
|
52
|
+
QueryClient
|
53
|
+
}));
|
54
|
+
}
|
55
|
+
|
56
|
+
if (shouldGenerateMutationHooks) {
|
57
|
+
[].push.apply(body, w.createReactQueryMutationHooks({
|
58
|
+
context,
|
59
|
+
execMsg: ExecuteMsg,
|
60
|
+
contractName: name,
|
61
|
+
ExecuteClient
|
62
|
+
}));
|
63
|
+
}
|
64
|
+
|
65
|
+
if (typeHash.hasOwnProperty('Coin')) {
|
66
|
+
// @ts-ignore
|
67
|
+
delete context.utils.Coin;
|
68
|
+
}
|
69
|
+
|
70
|
+
return [{
|
71
|
+
type: 'react-query',
|
72
|
+
localname,
|
73
|
+
body
|
74
|
+
}];
|
75
|
+
}
|
76
|
+
|
77
|
+
}
|