@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.
Files changed (250) hide show
  1. package/LICENSE-Apache +201 -0
  2. package/LICENSE-MIT +21 -0
  3. package/README.md +15 -7
  4. package/{types/src/builder → builder}/builder.d.ts +2 -2
  5. package/builder/builder.js +172 -0
  6. package/builder/index.js +17 -0
  7. package/bundler/bundler.d.ts +8 -0
  8. package/bundler/bundler.js +79 -0
  9. package/bundler/index.js +17 -0
  10. package/cli.d.ts +2 -0
  11. package/cli.js +24 -0
  12. package/cmds.d.ts +2 -0
  13. package/cmds.js +14 -0
  14. package/commands/create-boilerplate.d.ts +3 -0
  15. package/commands/create-boilerplate.js +132 -0
  16. package/commands/generate.d.ts +3 -0
  17. package/commands/generate.js +175 -0
  18. package/commands/install.d.ts +3 -0
  19. package/commands/install.js +107 -0
  20. package/{src/builder/builder.ts → esm/builder/builder.js} +39 -139
  21. package/esm/builder/index.js +1 -0
  22. package/esm/bundler/bundler.js +47 -0
  23. package/esm/bundler/index.js +1 -0
  24. package/esm/cli.js +20 -0
  25. package/{src → esm}/cmds.js +0 -1
  26. package/{src/commands/create-boilerplate.ts → esm/commands/create-boilerplate.js} +35 -70
  27. package/{src/commands/generate.ts → esm/commands/generate.js} +19 -24
  28. package/{src/commands/install.ts → esm/commands/install.js} +22 -35
  29. package/esm/file.js +18 -0
  30. package/esm/generators/client.js +67 -0
  31. package/esm/generators/create-helpers.js +36 -0
  32. package/esm/generators/message-composer.js +55 -0
  33. package/esm/generators/msg-builder.js +58 -0
  34. package/esm/generators/react-query.js +76 -0
  35. package/esm/generators/recoil.js +58 -0
  36. package/esm/generators/types.js +58 -0
  37. package/{src/index.ts → esm/index.js} +3 -6
  38. package/esm/plugins/client.js +59 -0
  39. package/esm/plugins/message-builder.js +52 -0
  40. package/esm/plugins/message-composer.js +46 -0
  41. package/esm/plugins/plugin-base.js +47 -0
  42. package/esm/plugins/provider-bundle.js +58 -0
  43. package/esm/plugins/provider.js +73 -0
  44. package/esm/plugins/react-query.js +69 -0
  45. package/esm/plugins/recoil.js +51 -0
  46. package/esm/plugins/types.js +38 -0
  47. package/{src → esm}/ts-codegen.js +1 -2
  48. package/{src/utils/clean.ts → esm/utils/clean.js} +7 -7
  49. package/{src/utils/cleanse.ts → esm/utils/cleanse.js} +18 -22
  50. package/esm/utils/files.js +42 -0
  51. package/{src/utils/parse.ts → esm/utils/parse.js} +4 -13
  52. package/esm/utils/prompt.js +59 -0
  53. package/{src/utils/schemas.ts → esm/utils/schemas.js} +16 -44
  54. package/esm/utils/unused.js +47 -0
  55. package/file.js +20 -0
  56. package/generators/client.js +95 -0
  57. package/generators/create-helpers.js +43 -0
  58. package/generators/message-composer.js +83 -0
  59. package/generators/msg-builder.js +86 -0
  60. package/generators/react-query.js +104 -0
  61. package/generators/recoil.js +86 -0
  62. package/generators/types.js +86 -0
  63. package/{module/helpers → helpers}/contractContextBase.js +5 -2
  64. package/{module/helpers → helpers}/contractContextBaseShortHandCtor.js +5 -2
  65. package/{module/helpers → helpers}/contractsContextTSX.js +5 -2
  66. package/{module/helpers/index.js → helpers/index.d.ts} +1 -1
  67. package/helpers/index.js +19 -0
  68. package/index.js +39 -0
  69. package/package.json +27 -51
  70. package/plugins/client.js +86 -0
  71. package/plugins/index.d.ts +1 -0
  72. package/plugins/index.js +17 -0
  73. package/plugins/message-builder.js +79 -0
  74. package/plugins/message-composer.js +73 -0
  75. package/{types/src/plugins → plugins}/plugin-base.d.ts +1 -1
  76. package/plugins/plugin-base.js +77 -0
  77. package/plugins/provider-bundle.js +85 -0
  78. package/{types/src/plugins → plugins}/provider.d.ts +2 -2
  79. package/plugins/provider.js +102 -0
  80. package/plugins/react-query.js +96 -0
  81. package/plugins/recoil.js +78 -0
  82. package/plugins/types.js +65 -0
  83. package/ts-codegen.js +8 -0
  84. package/utils/clean.js +49 -0
  85. package/utils/cleanse.js +89 -0
  86. package/utils/files.js +73 -0
  87. package/utils/header.js +14 -0
  88. package/utils/index.d.ts +1 -0
  89. package/utils/index.js +17 -0
  90. package/utils/parse.d.ts +1 -0
  91. package/utils/parse.js +43 -0
  92. package/utils/prompt.d.ts +3 -0
  93. package/utils/prompt.js +65 -0
  94. package/utils/schemas.d.ts +11 -0
  95. package/utils/schemas.js +81 -0
  96. package/utils/unused.js +73 -0
  97. package/main/builder/builder.js +0 -308
  98. package/main/builder/index.js +0 -16
  99. package/main/bundler/bundler.js +0 -61
  100. package/main/bundler/index.js +0 -16
  101. package/main/cli.js +0 -51
  102. package/main/cmds.js +0 -15
  103. package/main/commands/create-boilerplate.js +0 -166
  104. package/main/commands/generate.js +0 -196
  105. package/main/commands/install.js +0 -138
  106. package/main/file.js +0 -35
  107. package/main/generators/client.js +0 -90
  108. package/main/generators/create-helpers.js +0 -38
  109. package/main/generators/message-composer.js +0 -77
  110. package/main/generators/msg-builder.js +0 -84
  111. package/main/generators/react-query.js +0 -102
  112. package/main/generators/recoil.js +0 -81
  113. package/main/generators/types.js +0 -82
  114. package/main/helpers/contractContextBase.js +0 -8
  115. package/main/helpers/contractContextBaseShortHandCtor.js +0 -8
  116. package/main/helpers/contractsContextTSX.js +0 -8
  117. package/main/helpers/index.js +0 -38
  118. package/main/index.js +0 -119
  119. package/main/plugins/client.js +0 -112
  120. package/main/plugins/index.js +0 -16
  121. package/main/plugins/message-builder.js +0 -102
  122. package/main/plugins/message-composer.js +0 -98
  123. package/main/plugins/plugin-base.js +0 -103
  124. package/main/plugins/provider-bundle.js +0 -108
  125. package/main/plugins/provider.js +0 -126
  126. package/main/plugins/react-query.js +0 -120
  127. package/main/plugins/recoil.js +0 -108
  128. package/main/plugins/types.js +0 -89
  129. package/main/ts-codegen.js +0 -20
  130. package/main/utils/clean.js +0 -53
  131. package/main/utils/cleanse.js +0 -87
  132. package/main/utils/files.js +0 -57
  133. package/main/utils/header.js +0 -11
  134. package/main/utils/index.js +0 -16
  135. package/main/utils/parse.js +0 -44
  136. package/main/utils/prompt.js +0 -98
  137. package/main/utils/schemas.js +0 -144
  138. package/main/utils/unused.js +0 -57
  139. package/module/builder/builder.js +0 -143
  140. package/module/builder/index.js +0 -1
  141. package/module/bundler/bundler.js +0 -36
  142. package/module/bundler/index.js +0 -1
  143. package/module/cli.js +0 -19
  144. package/module/cmds.js +0 -8
  145. package/module/commands/create-boilerplate.js +0 -102
  146. package/module/commands/generate.js +0 -163
  147. package/module/commands/install.js +0 -98
  148. package/module/file.js +0 -18
  149. package/module/generators/client.js +0 -65
  150. package/module/generators/create-helpers.js +0 -33
  151. package/module/generators/message-composer.js +0 -52
  152. package/module/generators/msg-builder.js +0 -58
  153. package/module/generators/react-query.js +0 -75
  154. package/module/generators/recoil.js +0 -53
  155. package/module/generators/types.js +0 -55
  156. package/module/index.js +0 -14
  157. package/module/plugins/client.js +0 -63
  158. package/module/plugins/index.js +0 -1
  159. package/module/plugins/message-builder.js +0 -55
  160. package/module/plugins/message-composer.js +0 -49
  161. package/module/plugins/plugin-base.js +0 -66
  162. package/module/plugins/provider-bundle.js +0 -56
  163. package/module/plugins/provider.js +0 -68
  164. package/module/plugins/react-query.js +0 -72
  165. package/module/plugins/recoil.js +0 -57
  166. package/module/plugins/types.js +0 -42
  167. package/module/ts-codegen.js +0 -6
  168. package/module/utils/clean.js +0 -44
  169. package/module/utils/cleanse.js +0 -78
  170. package/module/utils/files.js +0 -40
  171. package/module/utils/header.js +0 -8
  172. package/module/utils/index.js +0 -1
  173. package/module/utils/parse.js +0 -35
  174. package/module/utils/prompt.js +0 -63
  175. package/module/utils/schemas.js +0 -85
  176. package/module/utils/unused.js +0 -43
  177. package/src/builder/index.ts +0 -1
  178. package/src/bundler/bundler.ts +0 -67
  179. package/src/bundler/index.ts +0 -1
  180. package/src/cli.js +0 -21
  181. package/src/file.js +0 -20
  182. package/src/generators/client.ts +0 -109
  183. package/src/generators/create-helpers.ts +0 -71
  184. package/src/generators/message-composer.ts +0 -86
  185. package/src/generators/msg-builder.ts +0 -78
  186. package/src/generators/react-query.ts +0 -102
  187. package/src/generators/recoil.ts +0 -92
  188. package/src/generators/types.ts +0 -94
  189. package/src/helpers/index.ts +0 -3
  190. package/src/plugins/client.ts +0 -124
  191. package/src/plugins/index.ts +0 -1
  192. package/src/plugins/message-builder.ts +0 -85
  193. package/src/plugins/message-composer.ts +0 -89
  194. package/src/plugins/plugin-base.ts +0 -122
  195. package/src/plugins/provider-bundle.ts +0 -98
  196. package/src/plugins/provider.ts +0 -115
  197. package/src/plugins/react-query.ts +0 -115
  198. package/src/plugins/recoil.ts +0 -89
  199. package/src/plugins/types.ts +0 -74
  200. package/src/utils/files.ts +0 -73
  201. package/src/utils/index.ts +0 -1
  202. package/src/utils/prompt.js +0 -65
  203. package/src/utils/unused.ts +0 -52
  204. package/types/src/bundler/bundler.d.ts +0 -4
  205. package/types/src/cli.d.ts +0 -1
  206. package/types/src/cmds.d.ts +0 -1
  207. package/types/src/commands/create-boilerplate.d.ts +0 -2
  208. package/types/src/commands/generate.d.ts +0 -2
  209. package/types/src/commands/install.d.ts +0 -2
  210. package/types/src/generators/msg-builder.ts +0 -5
  211. package/types/src/plugins/msg-builder.d.ts +0 -12
  212. package/types/src/plugins/use-contracts.d.ts +0 -12
  213. package/types/src/types.d.ts +0 -16
  214. package/types/src/utils/parse.d.ts +0 -1
  215. package/types/src/utils/prompt.d.ts +0 -3
  216. package/types/src/utils/schemas.d.ts +0 -10
  217. /package/{types/src/builder → builder}/index.d.ts +0 -0
  218. /package/{types/src/bundler → bundler}/index.d.ts +0 -0
  219. /package/{src/helpers/contractContextBase.ts → esm/helpers/contractContextBase.js} +0 -0
  220. /package/{src/helpers/contractContextBaseShortHandCtor.ts → esm/helpers/contractContextBaseShortHandCtor.js} +0 -0
  221. /package/{src/helpers/contractsContextTSX.ts → esm/helpers/contractsContextTSX.js} +0 -0
  222. /package/{types/src/helpers/index.d.ts → esm/helpers/index.js} +0 -0
  223. /package/{types/src/plugins/index.d.ts → esm/plugins/index.js} +0 -0
  224. /package/{src/utils/header.ts → esm/utils/header.js} +0 -0
  225. /package/{types/src/utils/index.d.ts → esm/utils/index.js} +0 -0
  226. /package/{types/src/file.d.ts → file.d.ts} +0 -0
  227. /package/{types/src/generators → generators}/client.d.ts +0 -0
  228. /package/{types/src/generators → generators}/create-helpers.d.ts +0 -0
  229. /package/{types/src/generators → generators}/message-composer.d.ts +0 -0
  230. /package/{types/src/generators → generators}/msg-builder.d.ts +0 -0
  231. /package/{types/src/generators → generators}/react-query.d.ts +0 -0
  232. /package/{types/src/generators → generators}/recoil.d.ts +0 -0
  233. /package/{types/src/generators → generators}/types.d.ts +0 -0
  234. /package/{types/src/helpers → helpers}/contractContextBase.d.ts +0 -0
  235. /package/{types/src/helpers → helpers}/contractContextBaseShortHandCtor.d.ts +0 -0
  236. /package/{types/src/helpers → helpers}/contractsContextTSX.d.ts +0 -0
  237. /package/{types/src/index.d.ts → index.d.ts} +0 -0
  238. /package/{types/src/plugins → plugins}/client.d.ts +0 -0
  239. /package/{types/src/plugins → plugins}/message-builder.d.ts +0 -0
  240. /package/{types/src/plugins → plugins}/message-composer.d.ts +0 -0
  241. /package/{types/src/plugins → plugins}/provider-bundle.d.ts +0 -0
  242. /package/{types/src/plugins → plugins}/react-query.d.ts +0 -0
  243. /package/{types/src/plugins → plugins}/recoil.d.ts +0 -0
  244. /package/{types/src/plugins → plugins}/types.d.ts +0 -0
  245. /package/{types/src/ts-codegen.d.ts → ts-codegen.d.ts} +0 -0
  246. /package/{types/src/utils → utils}/clean.d.ts +0 -0
  247. /package/{types/src/utils → utils}/cleanse.d.ts +0 -0
  248. /package/{types/src/utils → utils}/files.d.ts +0 -0
  249. /package/{types/src/utils → utils}/header.d.ts +0 -0
  250. /package/{types/src/utils → utils}/unused.d.ts +0 -0
@@ -1,122 +0,0 @@
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 { ContractInfo, UtilMapping, IContext } from "wasm-ast-types";
6
- import generate from "@babel/generator";
7
- import * as t from "@babel/types";
8
- import {
9
- BuilderFile,
10
- BuilderFileType,
11
- TSBuilder,
12
- TSBuilderOptions,
13
- } from "../builder";
14
-
15
- /**
16
- * IBuilderPlugin is a common plugin that render generated code.
17
- */
18
- export interface IBuilderPlugin {
19
- /**
20
- * a mapping of utils will be used in generated code.
21
- */
22
- utils: UtilMapping;
23
-
24
- builder?: TSBuilder;
25
-
26
- setBuilder(builder: TSBuilder);
27
-
28
- /**
29
- * render generated cdoe.
30
- * @param name the name of contract
31
- * @param contractInfo contract
32
- * @param outPath the path of generated code.
33
- * @returns info of generated files.
34
- */
35
- render(
36
- name: string,
37
- contractInfo: ContractInfo,
38
- outPath: string
39
- ): Promise<BuilderFile[]>;
40
- }
41
-
42
- /**
43
- * BuilderPluginBase enable ts-codegen users implement their own plugins by only implement a few functions.
44
- */
45
- export abstract class BuilderPluginBase<TOpt extends { enabled?: boolean }>
46
- implements IBuilderPlugin
47
- {
48
- builder?: TSBuilder;
49
- option: TOpt;
50
- utils: UtilMapping;
51
-
52
- constructor(opt: TOpt, builder?: TSBuilder) {
53
- this.option = opt;
54
- this.builder = builder;
55
- }
56
-
57
- setBuilder(builder: TSBuilder) {
58
- this.builder = builder;
59
- }
60
-
61
- async render(
62
- name: string,
63
- contractInfo: ContractInfo,
64
- outPath: string
65
- ): Promise<BuilderFile[]> {
66
- const { enabled } = this.option;
67
-
68
- if (!enabled) {
69
- return;
70
- }
71
-
72
- const context = this.initContext(contractInfo, this.option);
73
-
74
- const results = await this.doRender(name, context);
75
-
76
- if (!results || !results.length) {
77
- return [];
78
- }
79
-
80
- return results.map((result) => {
81
- const imports = context.getImports(this.utils, result.localname);
82
- const code =
83
- header + generate(t.program([...imports, ...result.body])).code;
84
-
85
- mkdirp(outPath);
86
- const filename = join(outPath, result.localname);
87
- writeFileSync(filename, code);
88
-
89
- return {
90
- type: result.type,
91
- pluginType: result.pluginType,
92
- contract: name,
93
- localname: result.localname,
94
- filename,
95
- };
96
- });
97
- }
98
-
99
- /**
100
- * init context here
101
- * @param contract
102
- * @param options
103
- */
104
- abstract initContext(contract: ContractInfo, options?: TOpt): IContext;
105
-
106
- /**
107
- * render generated code here.
108
- * @param name
109
- * @param context
110
- */
111
- abstract doRender(
112
- name: string,
113
- context: IContext
114
- ): Promise<
115
- {
116
- type: BuilderFileType;
117
- pluginType?: string;
118
- localname: string;
119
- body: any[];
120
- }[]
121
- >;
122
- }
@@ -1,98 +0,0 @@
1
- import { pascal } from "case";
2
- import * as w from "wasm-ast-types";
3
- import { ContractInfo, RenderContextBase, RenderContext } from "wasm-ast-types";
4
- import { BuilderFileType, TSBuilderOptions } from "../builder";
5
- import { BuilderPluginBase } from "./plugin-base";
6
- import { GetLocalBaseNameByContractName } from "./provider";
7
-
8
- export class ContractsProviderBundlePlugin extends BuilderPluginBase<TSBuilderOptions> {
9
- constructor(opt: TSBuilderOptions) {
10
- super(opt);
11
-
12
- this.utils = {
13
- CosmWasmClient: "@cosmjs/cosmwasm-stargate",
14
- SigningCosmWasmClient: "@cosmjs/cosmwasm-stargate",
15
- IQueryClientProvider: "__contractContextBase__",
16
- ISigningClientProvider: "__contractContextBase__",
17
- IMessageComposerProvider: "__contractContextBase__",
18
- };
19
- }
20
-
21
- initContext(
22
- contract: ContractInfo,
23
- options?: TSBuilderOptions
24
- ): RenderContextBase<TSBuilderOptions> {
25
- return new RenderContext(contract, options, this.builder.builderContext);
26
- }
27
-
28
- async doRender(
29
- name: string,
30
- context: RenderContext
31
- ): Promise<
32
- {
33
- type: BuilderFileType;
34
- pluginType?: string;
35
- localname: string;
36
- body: any[];
37
- }[]
38
- > {
39
- if (!this.option?.useContractsHooks?.enabled) {
40
- return;
41
- }
42
-
43
- const providerInfos = context.getProviderInfos();
44
-
45
- if (!Object.keys(providerInfos || {})?.length) {
46
- return;
47
- }
48
-
49
- const localname = "contractContextProviders.ts";
50
-
51
- const body = [];
52
-
53
- context.addUtil("CosmWasmClient");
54
- context.addUtil("SigningCosmWasmClient");
55
-
56
- context.addUtil("IQueryClientProvider");
57
- context.addUtil("ISigningClientProvider");
58
- context.addUtil("IMessageComposerProvider");
59
-
60
- for (const name in providerInfos) {
61
- if (Object.prototype.hasOwnProperty.call(providerInfos, name)) {
62
- const providerInfo = providerInfos[name];
63
-
64
- for (const key in providerInfo) {
65
- if (Object.prototype.hasOwnProperty.call(providerInfo, key)) {
66
- const info = providerInfo[key];
67
-
68
- body.push(
69
- w.importStmt(
70
- [info.classname],
71
- `./${info.basename}`
72
- )
73
- );
74
- }
75
- }
76
-
77
- body.push(
78
- w.importStmt(
79
- [pascal(name)],
80
- `./${GetLocalBaseNameByContractName(name)}`
81
- )
82
- );
83
- }
84
- }
85
-
86
- body.push(w.createIContractsContext(providerInfos));
87
- body.push(w.createGettingProviders(providerInfos));
88
-
89
- return [
90
- {
91
- type: "plugin",
92
- pluginType: "contractContextProviders",
93
- localname,
94
- body,
95
- },
96
- ];
97
- }
98
- }
@@ -1,115 +0,0 @@
1
- import { pascal } from "case";
2
- import * as w from "wasm-ast-types";
3
- import { ContractInfo, RenderContextBase, RenderContext } from "wasm-ast-types";
4
- import { BuilderFileType, TSBuilderOptions } from "../builder";
5
- import { BuilderPluginBase } from "./plugin-base";
6
-
7
- export const GetLocalNameByContractName = (name) =>
8
- `${pascal(name)}.provider.ts`;
9
-
10
- export const GetLocalBaseNameByContractName = (name) =>
11
- `${pascal(name)}.provider`;
12
-
13
- export class ContractsContextProviderPlugin extends BuilderPluginBase<TSBuilderOptions> {
14
- constructor(opt: TSBuilderOptions) {
15
- super(opt);
16
-
17
- this.utils = {
18
- ContractBase: "__contractContextBase__",
19
- IContractConstructor: "__contractContextBase__",
20
- IEmptyClient: "__contractContextBase__",
21
- };
22
- }
23
-
24
- initContext(
25
- contract: ContractInfo,
26
- options?: TSBuilderOptions
27
- ): RenderContextBase<TSBuilderOptions> {
28
- return new RenderContext(contract, options, this.builder.builderContext);
29
- }
30
-
31
- async doRender(
32
- name: string,
33
- context: RenderContext
34
- ): Promise<
35
- {
36
- type: BuilderFileType;
37
- pluginType?: string;
38
- localname: string;
39
- body: any[];
40
- }[]
41
- > {
42
- if (!this.option?.useContractsHooks?.enabled) {
43
- return;
44
- }
45
-
46
- const providerInfo = context.getProviderInfos()[name];
47
-
48
- if (!Object.keys(providerInfo || {})?.length) {
49
- return;
50
- }
51
-
52
- context.addUtil("ContractBase");
53
- context.addUtil("IContractConstructor");
54
-
55
- const localname = GetLocalNameByContractName(name);
56
- let needEmptyClientType = false;
57
- let clientFile = null;
58
- let clientClasses = [];
59
-
60
- const body = [];
61
-
62
- const signClientProviderInfo =
63
- providerInfo[w.PROVIDER_TYPES.SIGNING_CLIENT_TYPE];
64
-
65
- if (signClientProviderInfo) {
66
- clientFile = `./${signClientProviderInfo.basename}`;
67
- clientClasses.push(signClientProviderInfo.classname);
68
- } else {
69
- needEmptyClientType = true;
70
- }
71
-
72
- const queryClientProviderInfo =
73
- providerInfo[w.PROVIDER_TYPES.QUERY_CLIENT_TYPE];
74
-
75
- if (queryClientProviderInfo) {
76
- clientFile = `./${queryClientProviderInfo.basename}`;
77
- clientClasses.push(queryClientProviderInfo.classname);
78
- } else {
79
- needEmptyClientType = true;
80
- }
81
-
82
- if (clientFile) {
83
- body.push(w.importStmt(clientClasses, clientFile));
84
- }
85
-
86
- const messageComposerProviderInfo =
87
- providerInfo[w.PROVIDER_TYPES.MESSAGE_COMPOSER_TYPE];
88
-
89
- if (messageComposerProviderInfo) {
90
- body.push(
91
- w.importStmt(
92
- [messageComposerProviderInfo.classname],
93
- `./${messageComposerProviderInfo.basename}`
94
- )
95
- );
96
- } else {
97
- needEmptyClientType = true;
98
- }
99
-
100
- if (needEmptyClientType) {
101
- context.addUtil("IEmptyClient");
102
- }
103
-
104
- body.push(w.createProvider(name, providerInfo));
105
-
106
- return [
107
- {
108
- type: "plugin",
109
- pluginType: "provider",
110
- localname,
111
- body,
112
- },
113
- ];
114
- }
115
- }
@@ -1,115 +0,0 @@
1
- import { pascal } from 'case';
2
- import * as w from 'wasm-ast-types';
3
- import { findAndParseTypes, findExecuteMsg, findQueryMsg } from '../utils';
4
- import {
5
- getMessageProperties,
6
- ContractInfo,
7
- RenderOptions,
8
- RenderContextBase,
9
- RenderContext
10
- } from 'wasm-ast-types';
11
- import { BuilderFileType } from '../builder';
12
- import { BuilderPluginBase } from './plugin-base';
13
-
14
- export class ReactQueryPlugin extends BuilderPluginBase<RenderOptions> {
15
- initContext(
16
- contract: ContractInfo,
17
- options?: RenderOptions
18
- ): RenderContextBase<RenderOptions> {
19
- return new RenderContext(contract, options, this.builder.builderContext);
20
- }
21
-
22
- async doRender(
23
- name: string,
24
- context: RenderContext
25
- ): Promise<
26
- {
27
- type: BuilderFileType;
28
- pluginType?: string;
29
- localname: string;
30
- body: any[];
31
- }[]
32
- > {
33
- const options = this.option.reactQuery;
34
-
35
- const { enabled } = options;
36
-
37
- if (!enabled) {
38
- return;
39
- }
40
-
41
- const { schemas } = context.contract;
42
-
43
- const localname = pascal(`${name}`) + '.react-query.ts';
44
- const ContractFile = pascal(`${name}`) + '.client';
45
- const TypesFile = pascal(`${name}`) + '.types';
46
-
47
- const QueryMsg = findQueryMsg(schemas);
48
- const ExecuteMsg = findExecuteMsg(schemas);
49
- const typeHash = await findAndParseTypes(schemas);
50
-
51
- const ExecuteClient = pascal(`${name}Client`);
52
- const QueryClient = pascal(`${name}QueryClient`);
53
-
54
- const body = [];
55
-
56
- const clientImports = [];
57
-
58
- QueryMsg && clientImports.push(QueryClient);
59
-
60
- // check that there are commands within the exec msg
61
- const shouldGenerateMutationHooks =
62
- ExecuteMsg &&
63
- options?.version === 'v4' &&
64
- options?.mutations &&
65
- getMessageProperties(ExecuteMsg).length > 0;
66
-
67
- if (shouldGenerateMutationHooks) {
68
- clientImports.push(ExecuteClient);
69
- }
70
-
71
- // general contract imports
72
- body.push(w.importStmt(Object.keys(typeHash), `./${TypesFile}`));
73
-
74
- // client imports
75
- body.push(w.importStmt(clientImports, `./${ContractFile}`));
76
-
77
- // query messages
78
- if (QueryMsg) {
79
- [].push.apply(
80
- body,
81
- w.createReactQueryHooks({
82
- context,
83
- queryMsg: QueryMsg,
84
- contractName: name,
85
- QueryClient
86
- })
87
- );
88
- }
89
-
90
- if (shouldGenerateMutationHooks) {
91
- [].push.apply(
92
- body,
93
- w.createReactQueryMutationHooks({
94
- context,
95
- execMsg: ExecuteMsg,
96
- contractName: name,
97
- ExecuteClient
98
- })
99
- );
100
- }
101
-
102
- if (typeHash.hasOwnProperty('Coin')) {
103
- // @ts-ignore
104
- delete context.utils.Coin;
105
- }
106
-
107
- return [
108
- {
109
- type: 'react-query',
110
- localname,
111
- body
112
- }
113
- ];
114
- }
115
- }
@@ -1,89 +0,0 @@
1
- import { pascal } from 'case';
2
- import * as w from 'wasm-ast-types';
3
- import { findAndParseTypes, findQueryMsg } from '../utils';
4
- import {
5
- ContractInfo,
6
- RenderContext,
7
- RenderContextBase,
8
- UtilMapping,
9
- RenderOptions
10
- } from 'wasm-ast-types';
11
- import { BuilderFileType } from '../builder';
12
- import { BuilderPluginBase } from './plugin-base';
13
-
14
- export class RecoilPlugin extends BuilderPluginBase<RenderOptions> {
15
- utils: UtilMapping = {
16
- selectorFamily: 'recoil',
17
- };
18
- initContext(
19
- contract: ContractInfo,
20
- options?: RenderOptions
21
- ): RenderContextBase<RenderOptions> {
22
- return new RenderContext(contract, options, this.builder.builderContext);
23
- }
24
-
25
- async doRender(
26
- name: string,
27
- context: RenderContext
28
- ): Promise<
29
- {
30
- type: BuilderFileType;
31
- pluginType?: string;
32
- localname: string;
33
- body: any[];
34
- }[]
35
- > {
36
- const { enabled } = this.option.recoil;
37
-
38
- if (!enabled) {
39
- return;
40
- }
41
-
42
- const { schemas } = context.contract;
43
-
44
- const localname = pascal(name) + '.recoil.ts';
45
- const ContractFile = pascal(name) + '.client';
46
- const TypesFile = pascal(name) + '.types';
47
-
48
- const QueryMsg = findQueryMsg(schemas);
49
- const typeHash = await findAndParseTypes(schemas);
50
-
51
- let QueryClient = null;
52
- let ReadOnlyInstance = null;
53
-
54
- const body = [];
55
-
56
- body.push(w.importStmt(['cosmWasmClient'], './chain'));
57
-
58
- body.push(w.importStmt(Object.keys(typeHash), `./${TypesFile}`));
59
-
60
- // query messages
61
- if (QueryMsg) {
62
- QueryClient = pascal(`${name}QueryClient`);
63
- ReadOnlyInstance = pascal(`${name}ReadOnlyInterface`);
64
-
65
- body.push(w.importStmt([QueryClient], `./${ContractFile}`));
66
-
67
- body.push(w.createRecoilQueryClientType());
68
- body.push(w.createRecoilQueryClient(context, name, QueryClient));
69
-
70
- [].push.apply(
71
- body,
72
- w.createRecoilSelectors(context, name, QueryClient, QueryMsg)
73
- );
74
- }
75
-
76
- if (typeHash.hasOwnProperty('Coin')) {
77
- // @ts-ignore
78
- delete context.utils.Coin;
79
- }
80
-
81
- return [
82
- {
83
- type: 'recoil',
84
- localname,
85
- body
86
- }
87
- ];
88
- }
89
- }
@@ -1,74 +0,0 @@
1
- import * as t from '@babel/types';
2
- import { clean } from '../utils/clean';
3
- import { pascal } from 'case';
4
- import { findExecuteMsg, findAndParseTypes, findQueryMsg } from '../utils';
5
- import {
6
- ContractInfo,
7
- RenderContext,
8
- RenderContextBase,
9
- RenderOptions
10
- } from 'wasm-ast-types';
11
- import { BuilderFileType } from '../builder';
12
- import { BuilderPluginBase } from './plugin-base';
13
-
14
- export class TypesPlugin extends BuilderPluginBase<RenderOptions> {
15
- initContext(
16
- contract: ContractInfo,
17
- options?: RenderOptions
18
- ): RenderContextBase<RenderOptions> {
19
- return new RenderContext(contract, options, this.builder.builderContext);
20
- }
21
-
22
- async doRender(
23
- name: string,
24
- context: RenderContext
25
- ): Promise<
26
- {
27
- type: BuilderFileType;
28
- pluginType?: string;
29
- localname: string;
30
- body: any[];
31
- }[]
32
- > {
33
- const { enabled } = this.option.types;
34
-
35
- if (!enabled) {
36
- return;
37
- }
38
-
39
- const { schemas } = context.contract;
40
- const options = this.option.types;
41
-
42
- const localname = pascal(name) + '.types.ts';
43
- const ExecuteMsg = findExecuteMsg(schemas);
44
- const typeHash = await findAndParseTypes(schemas);
45
-
46
- const body = [];
47
-
48
- // TYPES
49
- Object.values(typeHash).forEach((type: t.Node) => {
50
- body.push(clean(type));
51
- });
52
-
53
- // alias the ExecuteMsg
54
- if (options.aliasExecuteMsg && ExecuteMsg) {
55
- body.push(
56
- t.exportNamedDeclaration(
57
- t.tsTypeAliasDeclaration(
58
- t.identifier(`${name}ExecuteMsg`),
59
- null,
60
- t.tsTypeReference(t.identifier('ExecuteMsg'))
61
- )
62
- )
63
- );
64
- }
65
-
66
- return [
67
- {
68
- type: 'type',
69
- localname,
70
- body
71
- }
72
- ];
73
- }
74
- }
@@ -1,73 +0,0 @@
1
- import * as t from "@babel/types";
2
- import { parse, ParserPlugin } 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
-
10
- export const writeAstToFile = (
11
- outPath: string,
12
- program: t.Statement[],
13
- filename: string,
14
- removeUnusedImports = false,
15
- isTsDisable = false,
16
- isEslintDisable = false
17
- ) => {
18
- const ast = t.program(program);
19
- const content = generate(ast).code;
20
-
21
- if (removeUnusedImports) {
22
- const plugins: ParserPlugin[] = ["typescript"];
23
- const newAst = parse(content, {
24
- sourceType: "module",
25
- plugins,
26
- });
27
- traverse(newAst, unused);
28
- const content2 = generate(newAst).code;
29
- writeContentToFile(
30
- outPath,
31
- content2,
32
- filename,
33
- isTsDisable,
34
- isEslintDisable
35
- );
36
- } else {
37
- writeContentToFile(
38
- outPath,
39
- content,
40
- filename,
41
- isTsDisable,
42
- isEslintDisable
43
- );
44
- }
45
- };
46
-
47
- export const writeContentToFile = (
48
- outPath: string,
49
- content: string,
50
- filename: string,
51
- isTsDisable = false,
52
- isEslintDisable = false
53
- ) => {
54
- let esLintPrefix = "";
55
- let tsLintPrefix = "";
56
-
57
- let nameWithoutPath = filename.replace(outPath, "");
58
- // strip off leading slash
59
- if (nameWithoutPath.startsWith("/"))
60
- nameWithoutPath = nameWithoutPath.replace(/^\//, "");
61
-
62
- if (isTsDisable) {
63
- tsLintPrefix = `//@ts-nocheck\n`;
64
- }
65
-
66
- if (isEslintDisable) {
67
- esLintPrefix = `/* eslint-disable */\n`;
68
- }
69
-
70
- const text = tsLintPrefix + esLintPrefix + content;
71
- mkdirp(dirname(filename));
72
- writeFileSync(filename, text);
73
- };
@@ -1 +0,0 @@
1
- export * from './schemas';