@cosmwasm/ts-codegen 1.12.0 → 1.13.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 (53) hide show
  1. package/README.md +115 -121
  2. package/builder/builder.js +26 -24
  3. package/bundler/bundler.js +27 -17
  4. package/cli.js +2 -2
  5. package/commands/create-boilerplate.js +30 -19
  6. package/commands/generate.js +26 -35
  7. package/commands/install.js +22 -18
  8. package/esm/builder/builder.js +10 -18
  9. package/esm/bundler/bundler.js +10 -10
  10. package/esm/cli.js +2 -2
  11. package/esm/commands/create-boilerplate.js +12 -11
  12. package/esm/commands/generate.js +26 -35
  13. package/esm/commands/install.js +23 -19
  14. package/esm/file.js +4 -3
  15. package/esm/plugins/client.js +1 -1
  16. package/esm/plugins/message-builder.js +4 -4
  17. package/esm/plugins/message-composer.js +1 -1
  18. package/esm/plugins/plugin-base.js +3 -2
  19. package/esm/plugins/provider-bundle.js +1 -1
  20. package/esm/plugins/provider.js +1 -1
  21. package/esm/plugins/react-query.js +8 -7
  22. package/esm/plugins/recoil.js +4 -4
  23. package/esm/plugins/types.js +3 -3
  24. package/esm/ts-codegen.js +2 -1
  25. package/esm/utils/clean.js +1 -1
  26. package/esm/utils/cleanse.js +9 -5
  27. package/esm/utils/package.js +1 -1
  28. package/esm/utils/parse.js +5 -7
  29. package/esm/utils/prompt.js +2 -2
  30. package/esm/utils/schemas.js +32 -19
  31. package/esm/utils/unused.js +3 -4
  32. package/file.js +7 -3
  33. package/package.json +13 -13
  34. package/plugins/client.js +18 -8
  35. package/plugins/message-builder.js +20 -10
  36. package/plugins/message-composer.js +18 -8
  37. package/plugins/plugin-base.js +19 -8
  38. package/plugins/provider-bundle.js +17 -7
  39. package/plugins/provider.js +17 -7
  40. package/plugins/react-query.js +24 -13
  41. package/plugins/recoil.js +20 -10
  42. package/plugins/types.js +19 -9
  43. package/ts-codegen.js +5 -1
  44. package/utils/clean.js +1 -1
  45. package/utils/cleanse.d.ts +1 -0
  46. package/utils/cleanse.js +12 -7
  47. package/utils/files.js +17 -7
  48. package/utils/package.js +2 -3
  49. package/utils/parse.js +5 -7
  50. package/utils/prompt.js +2 -2
  51. package/utils/schemas.d.ts +5 -1
  52. package/utils/schemas.js +34 -20
  53. package/utils/unused.js +20 -11
package/utils/cleanse.js CHANGED
@@ -1,18 +1,23 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.cleanse = void 0;
3
+ exports.cleanse = exports.cleanFor = void 0;
4
4
  const case_1 = require("case");
5
5
  const cleanFor = (str) => {
6
6
  /*
7
7
  1. look at first char after _for_
8
8
  2. ONLY if you find capitals after, modify it
9
9
  */
10
- while (/_[a-z]+_[A-Z]/.test(str)) {
11
- const m = str.match(/(_[a-z]+_)[A-Z]/);
12
- str = str.replace(m[1], (0, case_1.pascal)(m[1]));
10
+ while (true) {
11
+ const match = str.match(/(_[a-z]+_)[A-Z]/);
12
+ if (!match)
13
+ break;
14
+ // this replace is unsafe as it replaces the same text but maybe
15
+ // in a different location than the match
16
+ str = str.replace(match[1], (0, case_1.pascal)(match[1]));
13
17
  }
14
18
  return str;
15
19
  };
20
+ exports.cleanFor = cleanFor;
16
21
  const cleanNullable = (str) => {
17
22
  if (/^Nullable_/.test(str)) {
18
23
  str = str.replace(/^Nullable_/, 'Nullable');
@@ -49,10 +54,10 @@ const cleanse = (obj) => {
49
54
  }
50
55
  }
51
56
  for (let attr in obj) {
52
- if (obj.hasOwnProperty(attr)) {
57
+ if (Object.prototype.hasOwnProperty.call(obj, attr)) {
53
58
  if (/_for_/.test(attr)) {
54
59
  // @ts-ignore
55
- copy[cleanFor(attr)] = (0, exports.cleanse)(obj[attr]);
60
+ copy[(0, exports.cleanFor)(attr)] = (0, exports.cleanse)(obj[attr]);
56
61
  }
57
62
  else if (/^Nullable_/.test(attr)) {
58
63
  // @ts-ignore
@@ -64,7 +69,7 @@ const cleanse = (obj) => {
64
69
  case '$ref':
65
70
  if (typeof obj[attr] === 'string') {
66
71
  // @ts-ignore
67
- copy[attr] = (0, exports.cleanse)(cleanNullable(cleanFor(obj[attr])));
72
+ copy[attr] = (0, exports.cleanse)(cleanNullable((0, exports.cleanFor)(obj[attr])));
68
73
  }
69
74
  else {
70
75
  // @ts-ignore
package/utils/files.js CHANGED
@@ -15,13 +15,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
15
15
  }) : function(o, v) {
16
16
  o["default"] = v;
17
17
  });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
25
35
  var __importDefault = (this && this.__importDefault) || function (mod) {
26
36
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
37
  };
package/utils/package.js CHANGED
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.readAndParsePackageJson = void 0;
3
+ exports.readAndParsePackageJson = readAndParsePackageJson;
4
4
  const fs_1 = require("fs");
5
5
  const path_1 = require("path");
6
- // need to search due to the dist/ folder and src/, etc.
6
+ // need to search due to the dist/ folder and src/, etc.
7
7
  function findPackageJson(currentDir) {
8
8
  const filePath = (0, path_1.join)(currentDir, 'package.json');
9
9
  // Check if package.json exists in the current directory
@@ -27,4 +27,3 @@ function readAndParsePackageJson() {
27
27
  const pkg = JSON.parse(str);
28
28
  return pkg;
29
29
  }
30
- exports.readAndParsePackageJson = readAndParsePackageJson;
package/utils/parse.js CHANGED
@@ -8,18 +8,16 @@ const parser_1 = require("@babel/parser");
8
8
  const traverse_1 = __importDefault(require("@babel/traverse"));
9
9
  const parser = (codes) => {
10
10
  const hash = {};
11
- codes.forEach(code => {
12
- const plugins = [
13
- 'typescript',
14
- ];
11
+ codes.forEach((code) => {
12
+ const plugins = ['typescript'];
15
13
  const ast = (0, parser_1.parse)(code, {
16
14
  sourceType: 'module',
17
- plugins
15
+ plugins,
18
16
  });
19
17
  const visitor = visitorFn({
20
18
  addType(key, node) {
21
19
  hash[key] = node;
22
- }
20
+ },
23
21
  });
24
22
  (0, traverse_1.default)(ast, visitor);
25
23
  });
@@ -39,5 +37,5 @@ const visitorFn = (parser) => ({
39
37
  },
40
38
  TSInterfaceDeclaration(path) {
41
39
  parser.addType(path.node.id.name, path.parentPath.node);
42
- }
40
+ },
43
41
  });
package/utils/prompt.js CHANGED
@@ -41,7 +41,7 @@ const transform = (questions) => {
41
41
  return {
42
42
  ...q,
43
43
  type: 'autocomplete',
44
- source: (0, exports.getFuzzySearch)(choices)
44
+ source: (0, exports.getFuzzySearch)(choices),
45
45
  };
46
46
  }
47
47
  else if (q.type === 'fuzzy:objects') {
@@ -50,7 +50,7 @@ const transform = (questions) => {
50
50
  return {
51
51
  ...q,
52
52
  type: 'autocomplete',
53
- source: (0, exports.getFuzzySearchNames)(choices)
53
+ source: (0, exports.getFuzzySearchNames)(choices),
54
54
  };
55
55
  }
56
56
  else {
@@ -4,7 +4,11 @@ interface ReadSchemaOpts {
4
4
  schemaDir: string;
5
5
  clean?: boolean;
6
6
  }
7
- export declare const readSchemas: ({ schemaDir, clean }: ReadSchemaOpts) => Promise<ContractInfo>;
7
+ /**
8
+ * Takes a schema directory and returns a list of relevant file paths
9
+ */
10
+ export declare const findSchemaFiles: (schemaDir: string) => Promise<string[]>;
11
+ export declare const readSchemas: ({ schemaDir, clean, }: ReadSchemaOpts) => Promise<ContractInfo>;
8
12
  export declare const findQueryMsg: (schemas: JSONSchema[]) => QueryMsg;
9
13
  export declare const findExecuteMsg: (schemas: JSONSchema[]) => ExecuteMsg;
10
14
  export declare const findAndParseTypes: (schemas: JSONSchema[]) => Promise<Record<string, any>>;
package/utils/schemas.js CHANGED
@@ -1,40 +1,54 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.findAndParseTypes = exports.findExecuteMsg = exports.findQueryMsg = exports.readSchemas = void 0;
3
+ exports.findAndParseTypes = exports.findExecuteMsg = exports.findQueryMsg = exports.readSchemas = exports.findSchemaFiles = void 0;
4
4
  const json_schema_to_typescript_1 = require("@pyramation/json-schema-to-typescript");
5
5
  const fs_1 = require("fs");
6
6
  const glob_1 = require("glob");
7
7
  const cleanse_1 = require("./cleanse");
8
8
  const parse_1 = require("./parse");
9
- ;
10
- const readSchemas = async ({ schemaDir, clean = true }) => {
11
- const fn = clean ? cleanse_1.cleanse : (schema) => schema;
12
- const files = (0, glob_1.sync)(schemaDir + '/**/*.json')
13
- .filter(file => !file.match(/\/raw\//));
14
- const schemas = files
15
- .map(file => JSON.parse((0, fs_1.readFileSync)(file, 'utf-8')));
16
- if (schemas.length > 1) {
9
+ /**
10
+ * Takes a schema directory and returns a list of relevant file paths
11
+ */
12
+ const findSchemaFiles = async (schemaDir) => {
13
+ const files = (0, glob_1.globSync)(schemaDir + '/**/*.json')
14
+ .filter((file) => !file.includes('/raw/')) // raw JSON Schema files that are also included in the main <contract_name>.json
15
+ .filter((file) => !file.includes('/cw_schema/')) // sub-folder for the new schema format for CosmWasm 3+
16
+ .sort();
17
+ return files;
18
+ };
19
+ exports.findSchemaFiles = findSchemaFiles;
20
+ const readSchemas = async ({ schemaDir, clean = true, }) => {
21
+ const fn = clean
22
+ ? cleanse_1.cleanse
23
+ : (schema) => schema;
24
+ const files = (await (0, exports.findSchemaFiles)(schemaDir)).map((path) => (0, fs_1.readFileSync)(path, 'utf-8'));
25
+ if (files.length > 1) {
17
26
  // legacy
18
27
  // TODO add console.warn here
28
+ console.warn('Found a multiple schema files. This mode will be removed in the next major version. Please migrate the schemas that contain a single <contract_name>.json IDL file (CosmWasm 1.1+).');
29
+ const schemas = files.map((file) => JSON.parse(file));
19
30
  return {
20
- schemas: fn(schemas)
31
+ schemas: fn(schemas),
21
32
  };
22
33
  }
23
- if (schemas.length === 0) {
34
+ if (files.length === 0) {
24
35
  throw new Error('Error [too few files]: requires one schema file per contract');
25
36
  }
26
- if (schemas.length !== 1) {
37
+ if (files.length !== 1) {
27
38
  throw new Error('Error [too many files]: CosmWasm v1.1 schemas supports one file');
28
39
  }
29
- const idlObject = schemas[0];
40
+ const idlObject = JSON.parse(files[0]);
30
41
  const {
31
42
  // contract_name,
32
43
  // contract_version,
33
- idl_version, responses, instantiate, execute, query, migrate, sudo } = idlObject;
44
+ idl_version, responses, instantiate, execute, query, migrate, sudo, } = idlObject;
34
45
  if (typeof idl_version !== 'string') {
35
46
  // legacy
47
+ // fall back to a single JSON Schema file
48
+ console.warn('Found a single schema file with missing idl_version. This mode will be removed in the next major version. Please migrate the schemas that contain a single <contract_name>.json IDL file (CosmWasm 1.1+).');
49
+ const schema = JSON.parse(files[0]);
36
50
  return {
37
- schemas: fn(schemas)
51
+ schemas: fn([schema]),
38
52
  };
39
53
  }
40
54
  // TODO use contract_name, etc.
@@ -43,25 +57,25 @@ const readSchemas = async ({ schemaDir, clean = true }) => {
43
57
  execute,
44
58
  query,
45
59
  migrate,
46
- sudo
60
+ sudo,
47
61
  };
48
62
  return {
49
63
  schemas: [
50
64
  ...Object.values(fn(idl)).filter(Boolean),
51
- ...Object.values(fn({ ...responses })).filter(Boolean)
65
+ ...Object.values(fn({ ...responses })).filter(Boolean),
52
66
  ],
53
67
  responses,
54
- idlObject
68
+ idlObject,
55
69
  };
56
70
  };
57
71
  exports.readSchemas = readSchemas;
58
72
  const findQueryMsg = (schemas) => {
59
- const queryMsg = schemas.find(schema => schema.title === 'QueryMsg');
73
+ const queryMsg = schemas.find((schema) => schema.title === 'QueryMsg');
60
74
  return queryMsg;
61
75
  };
62
76
  exports.findQueryMsg = findQueryMsg;
63
77
  const findExecuteMsg = (schemas) => {
64
- const executeMsg = schemas.find(schema => schema.title.startsWith('ExecuteMsg'));
78
+ const executeMsg = schemas.find((schema) => schema.title.startsWith('ExecuteMsg'));
65
79
  return executeMsg;
66
80
  };
67
81
  exports.findExecuteMsg = findExecuteMsg;
package/utils/unused.js CHANGED
@@ -16,13 +16,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
16
16
  }) : function(o, v) {
17
17
  o["default"] = v;
18
18
  });
19
- var __importStar = (this && this.__importStar) || function (mod) {
20
- if (mod && mod.__esModule) return mod;
21
- var result = {};
22
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
23
- __setModuleDefault(result, mod);
24
- return result;
25
- };
19
+ var __importStar = (this && this.__importStar) || (function () {
20
+ var ownKeys = function(o) {
21
+ ownKeys = Object.getOwnPropertyNames || function (o) {
22
+ var ar = [];
23
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
24
+ return ar;
25
+ };
26
+ return ownKeys(o);
27
+ };
28
+ return function (mod) {
29
+ if (mod && mod.__esModule) return mod;
30
+ var result = {};
31
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
32
+ __setModuleDefault(result, mod);
33
+ return result;
34
+ };
35
+ })();
26
36
  Object.defineProperty(exports, "__esModule", { value: true });
27
37
  exports.unused = void 0;
28
38
  const t = __importStar(require("@babel/types"));
@@ -39,8 +49,7 @@ exports.unused = {
39
49
  const importName = source.node.value;
40
50
  if (!t.isStringLiteral(source))
41
51
  continue;
42
- const key = `${importName}(${source.node.loc &&
43
- source.node.loc.start.line})`;
52
+ const key = `${importName}(${source.node.loc && source.node.loc.start.line})`;
44
53
  if (!UnRefBindings.has(key)) {
45
54
  UnRefBindings.set(key, binding);
46
55
  }
@@ -68,6 +77,6 @@ exports.unused = {
68
77
  binding.path.parentPath.remove();
69
78
  }
70
79
  });
71
- }
72
- }
80
+ },
81
+ },
73
82
  };