@cosmwasm/ts-codegen 0.34.2 → 0.35.1

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 (82) hide show
  1. package/README.md +83 -20
  2. package/main/builder/builder.js +145 -247
  3. package/main/builder/index.js +0 -2
  4. package/main/bundler/bundler.js +3 -16
  5. package/main/bundler/index.js +0 -2
  6. package/main/cli.js +20 -38
  7. package/main/cmds.js +0 -5
  8. package/main/commands/create-boilerplate.js +132 -160
  9. package/main/commands/generate.js +168 -192
  10. package/main/commands/install.js +89 -123
  11. package/main/file.js +13 -25
  12. package/main/generators/client.js +55 -81
  13. package/main/generators/create-helpers.js +0 -14
  14. package/main/generators/message-composer.js +42 -68
  15. package/main/generators/msg-builder.js +49 -78
  16. package/main/generators/react-query.js +68 -92
  17. package/main/generators/recoil.js +47 -71
  18. package/main/generators/types.js +45 -73
  19. package/main/helpers/index.js +0 -6
  20. package/main/index.js +8 -30
  21. package/main/plugins/client.js +56 -90
  22. package/main/plugins/index.js +0 -2
  23. package/main/plugins/{msg-builder.js → message-builder.js} +56 -93
  24. package/main/plugins/message-composer.js +42 -76
  25. package/main/plugins/plugin-base.js +40 -70
  26. package/main/plugins/provider-bundle.js +42 -81
  27. package/main/plugins/provider.js +58 -103
  28. package/main/plugins/react-query.js +67 -99
  29. package/main/plugins/recoil.js +45 -82
  30. package/main/plugins/types.js +34 -68
  31. package/main/ts-codegen.js +7 -15
  32. package/main/utils/clean.js +8 -17
  33. package/main/utils/cleanse.js +10 -25
  34. package/main/utils/files.js +2 -22
  35. package/main/utils/header.js +0 -3
  36. package/main/utils/index.js +0 -2
  37. package/main/utils/parse.js +2 -7
  38. package/main/utils/prompt.js +16 -36
  39. package/main/utils/schemas.js +86 -133
  40. package/main/utils/unused.js +4 -15
  41. package/module/builder/builder.js +12 -33
  42. package/module/bundler/bundler.js +3 -3
  43. package/module/cli.js +0 -1
  44. package/module/commands/create-boilerplate.js +6 -19
  45. package/module/commands/generate.js +14 -19
  46. package/module/commands/install.js +14 -17
  47. package/module/file.js +0 -3
  48. package/module/generators/client.js +6 -7
  49. package/module/generators/create-helpers.js +0 -4
  50. package/module/generators/message-composer.js +4 -5
  51. package/module/generators/msg-builder.js +10 -14
  52. package/module/generators/react-query.js +8 -9
  53. package/module/generators/recoil.js +2 -3
  54. package/module/generators/types.js +4 -6
  55. package/module/plugins/client.js +4 -10
  56. package/module/plugins/{msg-builder.js → message-builder.js} +10 -18
  57. package/module/plugins/message-composer.js +4 -10
  58. package/module/plugins/plugin-base.js +10 -10
  59. package/module/plugins/provider-bundle.js +0 -10
  60. package/module/plugins/provider.js +0 -14
  61. package/module/plugins/react-query.js +8 -13
  62. package/module/plugins/recoil.js +2 -9
  63. package/module/plugins/types.js +4 -7
  64. package/module/ts-codegen.js +0 -2
  65. package/module/utils/clean.js +8 -13
  66. package/module/utils/cleanse.js +10 -21
  67. package/module/utils/files.js +2 -6
  68. package/module/utils/parse.js +2 -4
  69. package/module/utils/prompt.js +0 -5
  70. package/module/utils/schemas.js +2 -13
  71. package/module/utils/unused.js +4 -6
  72. package/package.json +3 -3
  73. package/src/builder/builder.ts +7 -6
  74. package/src/commands/generate.ts +14 -13
  75. package/src/generators/msg-builder.ts +7 -9
  76. package/src/plugins/{msg-builder.ts → message-builder.ts} +6 -6
  77. package/src/plugins/message-composer.ts +2 -2
  78. package/types/src/builder/builder.d.ts +3 -2
  79. package/types/src/generators/msg-builder.d.ts +2 -2
  80. package/types/src/generators/msg-builder.ts +2 -2
  81. package/types/src/plugins/message-builder.d.ts +12 -0
  82. package/types/src/plugins/msg-builder.d.ts +1 -1
@@ -1,9 +1,6 @@
1
1
  import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2
-
3
2
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
4
-
5
3
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
6
-
7
4
  import { sync as glob } from 'glob';
8
5
  import { readFileSync } from 'fs';
9
6
  import { cleanse } from './cleanse';
@@ -17,7 +14,6 @@ export const readSchemas = async ({
17
14
  const fn = clean ? cleanse : str => str;
18
15
  const files = glob(schemaDir + '/**/*.json').filter(file => !file.match(/\/raw\//));
19
16
  const schemas = files.map(file => JSON.parse(readFileSync(file, 'utf-8')));
20
-
21
17
  if (schemas.length > 1) {
22
18
  // legacy
23
19
  // TODO add console.warn here
@@ -25,15 +21,12 @@ export const readSchemas = async ({
25
21
  schemas: fn(schemas)
26
22
  };
27
23
  }
28
-
29
24
  if (schemas.length === 0) {
30
25
  throw new Error('Error [too few files]: requires one schema file per contract');
31
26
  }
32
-
33
27
  if (schemas.length !== 1) {
34
28
  throw new Error('Error [too many files]: CosmWasm v1.1 schemas supports one file');
35
29
  }
36
-
37
30
  const idlObject = schemas[0];
38
31
  const {
39
32
  contract_name,
@@ -46,15 +39,14 @@ export const readSchemas = async ({
46
39
  migrate,
47
40
  sudo
48
41
  } = idlObject;
49
-
50
42
  if (typeof idl_version !== 'string') {
51
43
  // legacy
52
44
  return {
53
45
  schemas: fn(schemas)
54
46
  };
55
- } // TODO use contract_name, etc.
56
-
47
+ }
57
48
 
49
+ // TODO use contract_name, etc.
58
50
  return {
59
51
  schemas: [...Object.values(fn({
60
52
  instantiate,
@@ -78,7 +70,6 @@ export const findExecuteMsg = schemas => {
78
70
  export const findAndParseTypes = async schemas => {
79
71
  const Types = schemas;
80
72
  const allTypes = [];
81
-
82
73
  for (const typ in Types) {
83
74
  if (Types[typ].definitions) {
84
75
  for (const key of Object.keys(Types[typ].definitions)) {
@@ -86,11 +77,9 @@ export const findAndParseTypes = async schemas => {
86
77
  Types[typ].definitions[key].title = key;
87
78
  }
88
79
  }
89
-
90
80
  const result = await compile(Types[typ], Types[typ].title);
91
81
  allTypes.push(result);
92
82
  }
93
-
94
83
  const typeHash = parser(allTypes);
95
84
  return typeHash;
96
85
  };
@@ -1,28 +1,27 @@
1
1
  //@ts-nocheck
2
- import * as t from '@babel/types'; // https://github.com/chuyik/babel-plugin-danger-remove-unused-import
2
+
3
+ import * as t from '@babel/types';
4
+
5
+ // https://github.com/chuyik/babel-plugin-danger-remove-unused-import
3
6
  // https://github.com/chuyik/babel-plugin-danger-remove-unused-import/blob/c5454c21e94698a2464a12baa5590761932a71a8/License#L1
4
7
 
5
8
  export const unused = {
6
9
  Program: {
7
10
  exit: path => {
8
11
  const UnRefBindings = new Map();
9
-
10
12
  for (const [name, binding] of Object.entries(path.scope.bindings)) {
11
13
  if (!binding.path.parentPath || binding.kind !== 'module') continue;
12
14
  const source = binding.path.parentPath.get('source');
13
15
  const importName = source.node.value;
14
16
  if (!t.isStringLiteral(source)) continue;
15
17
  const key = `${importName}(${source.node.loc && source.node.loc.start.line})`;
16
-
17
18
  if (!UnRefBindings.has(key)) {
18
19
  UnRefBindings.set(key, binding);
19
20
  }
20
-
21
21
  if (binding.referenced) {
22
22
  UnRefBindings.set(key, null);
23
23
  } else {
24
24
  const nodeType = binding.path.node.type;
25
-
26
25
  if (nodeType === 'ImportSpecifier') {
27
26
  binding.path.remove();
28
27
  } else if (nodeType === 'ImportDefaultSpecifier') {
@@ -34,7 +33,6 @@ export const unused = {
34
33
  }
35
34
  }
36
35
  }
37
-
38
36
  UnRefBindings.forEach((binding, key) => {
39
37
  if (binding && binding.path.parentPath) {
40
38
  binding.path.parentPath.remove();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cosmwasm/ts-codegen",
3
- "version": "0.34.2",
3
+ "version": "0.35.1",
4
4
  "description": "@cosmwasm/ts-codegen converts your CosmWasm smart contracts into dev-friendly TypeScript classes so you can focus on shipping code.",
5
5
  "author": "Dan Lynch <pyramation@gmail.com>",
6
6
  "homepage": "https://github.com/cosmwasm/ts-codegen",
@@ -96,7 +96,7 @@
96
96
  "parse-package-name": "1.0.0",
97
97
  "rimraf": "3.0.2",
98
98
  "shelljs": "0.8.5",
99
- "wasm-ast-types": "^0.25.0"
99
+ "wasm-ast-types": "^0.26.1"
100
100
  },
101
- "gitHead": "0c404a0d94627610256c63f79dc2ffc3b741a845"
101
+ "gitHead": "0cad0afb48264cc726c06a6ff7a1453e7cabb4b5"
102
102
  }
@@ -17,7 +17,7 @@ import generate from '@babel/generator';
17
17
  import * as t from '@babel/types';
18
18
  import { ReactQueryPlugin } from "../plugins/react-query";
19
19
  import { RecoilPlugin } from "../plugins/recoil";
20
- import { MsgBuilderPlugin } from "../plugins/msg-builder";
20
+ import { MessageBuilderPlugin } from "../plugins/message-builder";
21
21
  import { MessageComposerPlugin } from "../plugins/message-composer";
22
22
  import { ClientPlugin } from "../plugins/client";
23
23
  import { TypesPlugin } from "../plugins/types";
@@ -30,7 +30,8 @@ const defaultOpts: TSBuilderOptions = {
30
30
  enabled: true,
31
31
  scope: 'contracts',
32
32
  bundleFile: 'bundle.ts'
33
- }
33
+ },
34
+ useShorthandCtor: true
34
35
  }
35
36
 
36
37
  export interface TSBuilderInput {
@@ -55,13 +56,13 @@ export type TSBuilderOptions = {
55
56
  bundle?: BundleOptions;
56
57
  /**
57
58
  * Enable using shorthand constructor.
58
- * Default: false
59
+ * Default: true
59
60
  */
60
61
  useShorthandCtor?: boolean;
61
62
  useContractsHooks?: UseContractsOptions;
62
63
  } & RenderOptions;
63
64
 
64
- export type BuilderFileType = 'type' | 'client' | 'recoil' | 'react-query' | 'message-composer' | 'msg-builder' | 'plugin';
65
+ export type BuilderFileType = 'type' | 'client' | 'recoil' | 'react-query' | 'message-composer' | 'message-builder' | 'plugin';
65
66
 
66
67
  export interface BuilderFile {
67
68
  type: BuilderFileType;
@@ -109,7 +110,7 @@ export class TSBuilder {
109
110
  new MessageComposerPlugin(this.options),
110
111
  new ReactQueryPlugin(this.options),
111
112
  new RecoilPlugin(this.options),
112
- new MsgBuilderPlugin(this.options),
113
+ new MessageBuilderPlugin(this.options),
113
114
  new ContractsContextProviderPlugin(this.options),
114
115
  ]);
115
116
  }
@@ -189,7 +190,7 @@ export class TSBuilder {
189
190
  }, this.builderContext);
190
191
 
191
192
  if (helpers && helpers.length) {
192
- [].push.apply(this.files, helpers);
193
+ [].push.apply(this.files, helpers);
193
194
  }
194
195
 
195
196
  if (this.options.bundle.enabled) {
@@ -64,7 +64,7 @@ export default async (argv) => {
64
64
  type: 'list',
65
65
  name: 'version',
66
66
  message: 'which react-query version?',
67
- default: 'v3',
67
+ default: 'v4',
68
68
  choices: ['v3', 'v4']
69
69
  },
70
70
  {
@@ -91,17 +91,17 @@ export default async (argv) => {
91
91
  };
92
92
  const { mutations } = await prompt(questions3, argv);
93
93
 
94
- const queryFactoryQuestions = [];
94
+ const queryFactoryQuestions = [];
95
95
  if (queryKeys) {
96
- [].push.apply(queryFactoryQuestions, [
97
- // Only can use queryFactory if queryKeys is enabled
98
- {
99
- type: 'confirm',
100
- name: 'queryFactory',
101
- message: 'queryFactory? ',
102
- default: false
103
- }
104
- ])
96
+ [].push.apply(queryFactoryQuestions, [
97
+ // Only can use queryFactory if queryKeys is enabled
98
+ {
99
+ type: 'confirm',
100
+ name: 'queryFactory',
101
+ message: 'queryFactory? ',
102
+ default: false
103
+ }
104
+ ])
105
105
  };
106
106
  const { queryFactory } = await prompt(queryFactoryQuestions, argv);
107
107
  ///////// END REACT QUERY
@@ -151,8 +151,9 @@ export default async (argv) => {
151
151
  messageComposer: {
152
152
  enabled: plugin.includes('message-composer')
153
153
  },
154
- msgBuilder: {
155
- enabled: plugin.includes('msg-builder')
154
+ messageBuilder: {
155
+
156
+ enabled: plugin.includes('message-builder')
156
157
  },
157
158
  bundle: {
158
159
  enabled: bundle,
@@ -8,23 +8,21 @@ import { writeFileSync } from "fs";
8
8
  import generate from "@babel/generator";
9
9
  import { ContractInfo, getMessageProperties } from "wasm-ast-types";
10
10
  import { findAndParseTypes, findExecuteMsg, findQueryMsg } from '../utils';
11
- import { RenderContext, MsgBuilderOptions } from 'wasm-ast-types';
11
+ import { RenderContext, MessageBuilderOptions } from 'wasm-ast-types';
12
12
  import { BuilderFile } from "../builder";
13
- import babelTraverse from '@babel/traverse';
14
- import { parse as babelParse } from '@babel/parser'
15
13
 
16
14
  export default async (
17
15
  name: string,
18
16
  contractInfo: ContractInfo,
19
17
  outPath: string,
20
- msgBuilderOptions?: MsgBuilderOptions
18
+ messageBuilderOptions?: MessageBuilderOptions
21
19
  ): Promise<BuilderFile[]> => {
22
20
  const { schemas } = contractInfo;
23
21
  const context = new RenderContext(contractInfo, {
24
- msgBuilder: msgBuilderOptions ?? {},
22
+ messageBuilder: messageBuilderOptions ?? {},
25
23
  });
26
24
 
27
- const localname = pascal(name) + ".msg-builder.ts";
25
+ const localname = pascal(name) + ".message-builder.ts";
28
26
  const TypesFile = pascal(name) + ".types";
29
27
  const ExecuteMsg = findExecuteMsg(schemas);
30
28
  const typeHash = await findAndParseTypes(schemas);
@@ -41,7 +39,7 @@ export default async (
41
39
  const className = pascal(`${name}ExecuteMsgBuilder`);
42
40
 
43
41
  body.push(
44
- w.createMsgBuilderClass(context, className, ExecuteMsg)
42
+ w.createMessageBuilderClass(context, className, ExecuteMsg)
45
43
  );
46
44
  }
47
45
  }
@@ -54,7 +52,7 @@ export default async (
54
52
  const className = pascal(`${name}QueryMsgBuilder`);
55
53
 
56
54
  body.push(
57
- w.createMsgBuilderClass(context, className, QueryMsg)
55
+ w.createMessageBuilderClass(context, className, QueryMsg)
58
56
  );
59
57
  }
60
58
  }
@@ -71,7 +69,7 @@ export default async (
71
69
 
72
70
  return [
73
71
  {
74
- type: "msg-builder",
72
+ type: "message-builder",
75
73
  contract: name,
76
74
  localname,
77
75
  filename: join(outPath, localname),
@@ -11,7 +11,7 @@ import {
11
11
  import { BuilderFileType } from '../builder';
12
12
  import { BuilderPluginBase } from './plugin-base';
13
13
 
14
- export class MsgBuilderPlugin extends BuilderPluginBase<RenderOptions> {
14
+ export class MessageBuilderPlugin extends BuilderPluginBase<RenderOptions> {
15
15
  initContext(
16
16
  contract: ContractInfo,
17
17
  options?: RenderOptions
@@ -30,7 +30,7 @@ export class MsgBuilderPlugin extends BuilderPluginBase<RenderOptions> {
30
30
  body: any[];
31
31
  }[]
32
32
  > {
33
- const { enabled } = this.option.msgBuilder;
33
+ const { enabled } = this.option.messageBuilder;
34
34
 
35
35
  if (!enabled) {
36
36
  return;
@@ -38,7 +38,7 @@ export class MsgBuilderPlugin extends BuilderPluginBase<RenderOptions> {
38
38
 
39
39
  const { schemas } = context.contract;
40
40
 
41
- const localname = pascal(name) + '.msg-builder.ts';
41
+ const localname = pascal(name) + '.message-builder.ts';
42
42
  const TypesFile = pascal(name) + '.types';
43
43
  const ExecuteMsg = findExecuteMsg(schemas);
44
44
  const typeHash = await findAndParseTypes(schemas);
@@ -54,7 +54,7 @@ export class MsgBuilderPlugin extends BuilderPluginBase<RenderOptions> {
54
54
  if (children.length > 0) {
55
55
  const className = pascal(`${name}ExecuteMsgBuilder`);
56
56
 
57
- body.push(w.createMsgBuilderClass(context, className, ExecuteMsg));
57
+ body.push(w.createMessageBuilderClass(context, className, ExecuteMsg));
58
58
  }
59
59
  }
60
60
 
@@ -65,7 +65,7 @@ export class MsgBuilderPlugin extends BuilderPluginBase<RenderOptions> {
65
65
  if (children.length > 0) {
66
66
  const className = pascal(`${name}QueryMsgBuilder`);
67
67
 
68
- body.push(w.createMsgBuilderClass(context, className, QueryMsg));
68
+ body.push(w.createMessageBuilderClass(context, className, QueryMsg));
69
69
  }
70
70
  }
71
71
 
@@ -76,7 +76,7 @@ export class MsgBuilderPlugin extends BuilderPluginBase<RenderOptions> {
76
76
 
77
77
  return [
78
78
  {
79
- type: 'msg-builder',
79
+ type: 'message-builder',
80
80
  localname,
81
81
  body
82
82
  }
@@ -54,8 +54,8 @@ export class MessageComposerPlugin extends BuilderPluginBase<RenderOptions> {
54
54
  if (ExecuteMsg) {
55
55
  const children = getMessageProperties(ExecuteMsg);
56
56
  if (children.length > 0) {
57
- const TheClass = pascal(`${name}MessageComposer`);
58
- const Interface = pascal(`${name}Message`);
57
+ const TheClass = pascal(`${name}MsgComposer`);
58
+ const Interface = pascal(`${name}Msg`);
59
59
 
60
60
  body.push(
61
61
  w.createMessageComposerInterface(context, Interface, ExecuteMsg)
@@ -15,15 +15,16 @@ export interface BundleOptions {
15
15
  export interface UseContractsOptions {
16
16
  enabled?: boolean;
17
17
  }
18
- export declare type TSBuilderOptions = {
18
+ export type TSBuilderOptions = {
19
19
  bundle?: BundleOptions;
20
20
  /**
21
21
  * Enable using shorthand constructor.
22
+ * Default: true
22
23
  */
23
24
  useShorthandCtor?: boolean;
24
25
  useContractsHooks?: UseContractsOptions;
25
26
  } & RenderOptions;
26
- export declare type BuilderFileType = 'type' | 'client' | 'recoil' | 'react-query' | 'message-composer' | 'msg-builder' | 'plugin';
27
+ export type BuilderFileType = 'type' | 'client' | 'recoil' | 'react-query' | 'message-composer' | 'message-builder' | 'plugin';
27
28
  export interface BuilderFile {
28
29
  type: BuilderFileType;
29
30
  pluginType?: string;
@@ -1,5 +1,5 @@
1
1
  import { ContractInfo } from "wasm-ast-types";
2
- import { MsgBuilderOptions } from 'wasm-ast-types';
2
+ import { MessageBuilderOptions } from 'wasm-ast-types';
3
3
  import { BuilderFile } from "../builder";
4
- declare const _default: (name: string, contractInfo: ContractInfo, outPath: string, msgBuilderOptions?: MsgBuilderOptions) => Promise<BuilderFile[]>;
4
+ declare const _default: (name: string, contractInfo: ContractInfo, outPath: string, messageBuilderOptions?: MessageBuilderOptions) => Promise<BuilderFile[]>;
5
5
  export default _default;
@@ -1,5 +1,5 @@
1
1
  import { ContractInfo } from "wasm-ast-types";
2
- import { MsgBuilderOptions } from "wasm-ast-types";
2
+ import { MessageBuilderOptions } from "wasm-ast-types";
3
3
  import { BuilderFile } from "../builder";
4
- declare const _default: (name: string, contractInfo: ContractInfo, outPath: string, msgBuilderOptions?: MsgBuilderOptions) => Promise<BuilderFile[]>;
4
+ declare const _default: (name: string, contractInfo: ContractInfo, outPath: string, messageBuilderOptions?: MessageBuilderOptions) => Promise<BuilderFile[]>;
5
5
  export default _default;
@@ -0,0 +1,12 @@
1
+ import { RenderContext, RenderContextBase, ContractInfo, RenderOptions } from 'wasm-ast-types';
2
+ import { BuilderFileType } from '../builder';
3
+ import { BuilderPluginBase } from './plugin-base';
4
+ export declare class MessageBuilderPlugin extends BuilderPluginBase<RenderOptions> {
5
+ initContext(contract: ContractInfo, options?: RenderOptions): RenderContextBase<RenderOptions>;
6
+ doRender(name: string, context: RenderContext): Promise<{
7
+ type: BuilderFileType;
8
+ pluginType?: string;
9
+ localname: string;
10
+ body: any[];
11
+ }[]>;
12
+ }
@@ -1,7 +1,7 @@
1
1
  import { RenderContext, RenderContextBase, ContractInfo, RenderOptions } from 'wasm-ast-types';
2
2
  import { BuilderFileType } from '../builder';
3
3
  import { BuilderPluginBase } from './plugin-base';
4
- export declare class MsgBuilderPlugin extends BuilderPluginBase<RenderOptions> {
4
+ export declare class MessageBuilderPlugin extends BuilderPluginBase<RenderOptions> {
5
5
  initContext(contract: ContractInfo, options?: RenderOptions): RenderContextBase<RenderOptions>;
6
6
  doRender(name: string, context: RenderContext): Promise<{
7
7
  type: BuilderFileType;