@cosmwasm/ts-codegen 0.7.4 → 0.8.2
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +238 -64
- package/main/builder/builder.js +456 -0
- package/main/builder/index.js +18 -0
- package/main/bundler/bundler.js +74 -0
- package/main/bundler/index.js +18 -0
- package/main/cmds.js +3 -9
- package/main/commands/create-boilerplate.js +195 -0
- package/main/commands/generate.js +91 -16
- package/main/generators/{ts-client.js → client.js} +24 -24
- package/main/generators/{from-partial.js → message-composer.js} +29 -21
- package/main/generators/react-query.js +27 -21
- package/main/generators/recoil.js +31 -15
- package/main/generators/types.js +95 -0
- package/main/index.js +71 -17
- package/main/types.js +1 -0
- package/main/utils/schemas.js +5 -3
- package/module/builder/builder.js +178 -0
- package/module/builder/index.js +1 -0
- package/module/bundler/bundler.js +36 -0
- package/module/bundler/index.js +1 -0
- package/module/cmds.js +2 -6
- package/module/commands/create-boilerplate.js +116 -0
- package/module/commands/generate.js +82 -8
- package/module/generators/{ts-client.js → client.js} +17 -17
- package/module/generators/{from-partial.js → message-composer.js} +23 -17
- package/module/generators/react-query.js +20 -15
- package/module/generators/recoil.js +23 -9
- package/module/generators/types.js +40 -0
- package/module/index.js +12 -5
- package/module/types.js +1 -0
- package/module/utils/schemas.js +3 -2
- package/package.json +5 -3
- package/types/builder/builder.d.ts +39 -0
- package/types/builder/index.d.ts +1 -0
- package/types/bundler/bundler.d.ts +4 -0
- package/types/bundler/index.d.ts +1 -0
- package/types/commands/{from-partial.d.ts → client.d.ts} +0 -0
- package/types/commands/message-composer.d.ts +2 -0
- package/types/commands/types.d.ts +2 -0
- package/types/generators/client.d.ts +3 -0
- package/types/generators/message-composer.d.ts +3 -0
- package/types/generators/react-query.d.ts +2 -1
- package/types/generators/recoil.d.ts +2 -1
- package/types/generators/types.d.ts +3 -0
- package/types/index.d.ts +10 -5
- package/types/types.d.ts +1 -0
- package/types/utils/schemas.d.ts +8 -4
- package/main/commands/from-partial.js +0 -78
- package/main/commands/react-query.js +0 -98
- package/main/commands/recoil.js +0 -78
- package/main/utils/imports.js +0 -26
- package/module/commands/from-partial.js +0 -33
- package/module/commands/react-query.js +0 -54
- package/module/commands/recoil.js +0 -33
- package/module/utils/imports.js +0 -10
- package/types/clean.d.ts +0 -1
- package/types/cleanse.d.ts +0 -1
- package/types/commands/boilerplate.d.ts +0 -2
- package/types/cosmwasm-typescript-gen.d.ts +0 -2
- package/types/from-partial.d.ts +0 -2
- package/types/generate.d.ts +0 -2
- package/types/generators/from-partial.d.ts +0 -2
- package/types/generators/ts-client.d.ts +0 -2
- package/types/header.d.ts +0 -1
- package/types/imports.d.ts +0 -1
- package/types/parse.d.ts +0 -1
- package/types/prompt.d.ts +0 -3
- package/types/react-query.d.ts +0 -2
- package/types/recoil.d.ts +0 -2
- package/types/utils/imports.d.ts +0 -1
- package/types/utils.d.ts +0 -10
@@ -11,6 +11,8 @@ exports["default"] = void 0;
|
|
11
11
|
|
12
12
|
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
13
13
|
|
14
|
+
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
15
|
+
|
14
16
|
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
15
17
|
|
16
18
|
var _case = require("case");
|
@@ -36,42 +38,56 @@ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "functio
|
|
36
38
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
37
39
|
|
38
40
|
var _default = /*#__PURE__*/function () {
|
39
|
-
var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(name, schemas, outPath) {
|
40
|
-
var
|
41
|
+
var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(name, schemas, outPath, recoilOptions) {
|
42
|
+
var context, options, localname, ContractFile, TypesFile, QueryMsg, typeHash, QueryClient, ReadOnlyInstance, body, imports, code;
|
41
43
|
return _regenerator["default"].wrap(function _callee$(_context) {
|
42
44
|
while (1) {
|
43
45
|
switch (_context.prev = _context.next) {
|
44
46
|
case 0:
|
45
|
-
|
46
|
-
|
47
|
+
context = new w.RenderContext((0, _utils.getDefinitionSchema)(schemas), {
|
48
|
+
recoil: recoilOptions !== null && recoilOptions !== void 0 ? recoilOptions : {}
|
49
|
+
});
|
50
|
+
options = context.options.recoil;
|
51
|
+
localname = (0, _case.pascal)(name) + '.recoil.ts';
|
52
|
+
ContractFile = (0, _case.pascal)(name) + '.client';
|
53
|
+
TypesFile = (0, _case.pascal)(name) + '.types';
|
47
54
|
QueryMsg = (0, _utils.findQueryMsg)(schemas);
|
48
|
-
_context.next =
|
55
|
+
_context.next = 8;
|
49
56
|
return (0, _utils.findAndParseTypes)(schemas);
|
50
57
|
|
51
|
-
case
|
58
|
+
case 8:
|
52
59
|
typeHash = _context.sent;
|
53
60
|
QueryClient = null;
|
54
61
|
ReadOnlyInstance = null;
|
55
62
|
body = [];
|
56
|
-
body.push(w.importStmt(['selectorFamily'], 'recoil'));
|
57
63
|
body.push(w.importStmt(['cosmWasmClient'], './chain'));
|
58
|
-
body.push(w.importStmt(Object.keys(typeHash), "./".concat(
|
59
|
-
context = new w.RenderContext((0, _utils.getDefinitionSchema)(schemas)); // query messages
|
64
|
+
body.push(w.importStmt(Object.keys(typeHash), "./".concat(TypesFile))); // query messages
|
60
65
|
|
61
66
|
if (QueryMsg) {
|
62
67
|
QueryClient = (0, _case.pascal)("".concat(name, "QueryClient"));
|
63
68
|
ReadOnlyInstance = (0, _case.pascal)("".concat(name, "ReadOnlyInterface"));
|
64
|
-
body.push(w.importStmt([QueryClient], "./".concat(
|
69
|
+
body.push(w.importStmt([QueryClient], "./".concat(ContractFile)));
|
65
70
|
body.push(w.createRecoilQueryClientType());
|
66
71
|
body.push(w.createRecoilQueryClient(context, name, QueryClient));
|
67
72
|
[].push.apply(body, w.createRecoilSelectors(context, name, QueryClient, QueryMsg));
|
68
73
|
}
|
69
74
|
|
70
|
-
|
71
|
-
|
72
|
-
|
75
|
+
if (typeHash.hasOwnProperty('Coin')) {
|
76
|
+
delete context.utils.Coin;
|
77
|
+
}
|
73
78
|
|
74
|
-
|
79
|
+
imports = context.getImports();
|
80
|
+
code = _header.header + (0, _generator["default"])(t.program([].concat((0, _toConsumableArray2["default"])(imports), body))).code;
|
81
|
+
(0, _mkdirp.sync)(outPath);
|
82
|
+
(0, _fs.writeFileSync)((0, _path.join)(outPath, localname), code);
|
83
|
+
return _context.abrupt("return", [{
|
84
|
+
type: 'recoil',
|
85
|
+
contract: name,
|
86
|
+
localname: localname,
|
87
|
+
filename: (0, _path.join)(outPath, localname)
|
88
|
+
}]);
|
89
|
+
|
90
|
+
case 21:
|
75
91
|
case "end":
|
76
92
|
return _context.stop();
|
77
93
|
}
|
@@ -79,7 +95,7 @@ var _default = /*#__PURE__*/function () {
|
|
79
95
|
}, _callee);
|
80
96
|
}));
|
81
97
|
|
82
|
-
return function (_x, _x2, _x3) {
|
98
|
+
return function (_x, _x2, _x3, _x4) {
|
83
99
|
return _ref.apply(this, arguments);
|
84
100
|
};
|
85
101
|
}();
|
@@ -0,0 +1,95 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4
|
+
|
5
|
+
var _typeof = require("@babel/runtime/helpers/typeof");
|
6
|
+
|
7
|
+
Object.defineProperty(exports, "__esModule", {
|
8
|
+
value: true
|
9
|
+
});
|
10
|
+
exports["default"] = void 0;
|
11
|
+
|
12
|
+
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
13
|
+
|
14
|
+
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
15
|
+
|
16
|
+
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
17
|
+
|
18
|
+
var _case = require("case");
|
19
|
+
|
20
|
+
var _header = require("../utils/header");
|
21
|
+
|
22
|
+
var _path = require("path");
|
23
|
+
|
24
|
+
var _mkdirp = require("mkdirp");
|
25
|
+
|
26
|
+
var t = _interopRequireWildcard(require("@babel/types"));
|
27
|
+
|
28
|
+
var _fs = require("fs");
|
29
|
+
|
30
|
+
var _generator = _interopRequireDefault(require("@babel/generator"));
|
31
|
+
|
32
|
+
var _clean = require("../utils/clean");
|
33
|
+
|
34
|
+
var _utils = require("../utils");
|
35
|
+
|
36
|
+
var _wasmAstTypes = require("wasm-ast-types");
|
37
|
+
|
38
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
39
|
+
|
40
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
41
|
+
|
42
|
+
var _default = /*#__PURE__*/function () {
|
43
|
+
var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(name, schemas, outPath, tsTypesOptions) {
|
44
|
+
var context, options, localname, ExecuteMsg, typeHash, body, imports, code, filename;
|
45
|
+
return _regenerator["default"].wrap(function _callee$(_context) {
|
46
|
+
while (1) {
|
47
|
+
switch (_context.prev = _context.next) {
|
48
|
+
case 0:
|
49
|
+
context = new _wasmAstTypes.RenderContext((0, _utils.getDefinitionSchema)(schemas), {
|
50
|
+
tsClient: tsTypesOptions !== null && tsTypesOptions !== void 0 ? tsTypesOptions : {}
|
51
|
+
});
|
52
|
+
options = context.options.types;
|
53
|
+
localname = (0, _case.pascal)(name) + '.types.ts';
|
54
|
+
ExecuteMsg = (0, _utils.findExecuteMsg)(schemas);
|
55
|
+
_context.next = 6;
|
56
|
+
return (0, _utils.findAndParseTypes)(schemas);
|
57
|
+
|
58
|
+
case 6:
|
59
|
+
typeHash = _context.sent;
|
60
|
+
body = []; // TYPES
|
61
|
+
|
62
|
+
Object.values(typeHash).forEach(function (type) {
|
63
|
+
body.push((0, _clean.clean)(type));
|
64
|
+
}); // alias the ExecuteMsg
|
65
|
+
|
66
|
+
if (options.aliasExecuteMsg && ExecuteMsg) {
|
67
|
+
body.push(t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier("".concat(name, "ExecuteMsg")), null, t.tsTypeReference(t.identifier('ExecuteMsg')))));
|
68
|
+
}
|
69
|
+
|
70
|
+
imports = context.getImports();
|
71
|
+
code = _header.header + (0, _generator["default"])(t.program([].concat((0, _toConsumableArray2["default"])(imports), body))).code;
|
72
|
+
(0, _mkdirp.sync)(outPath);
|
73
|
+
filename = (0, _path.join)(outPath, localname);
|
74
|
+
(0, _fs.writeFileSync)(filename, code);
|
75
|
+
return _context.abrupt("return", [{
|
76
|
+
type: 'type',
|
77
|
+
contract: name,
|
78
|
+
localname: localname,
|
79
|
+
filename: filename
|
80
|
+
}]);
|
81
|
+
|
82
|
+
case 16:
|
83
|
+
case "end":
|
84
|
+
return _context.stop();
|
85
|
+
}
|
86
|
+
}
|
87
|
+
}, _callee);
|
88
|
+
}));
|
89
|
+
|
90
|
+
return function (_x, _x2, _x3, _x4) {
|
91
|
+
return _ref.apply(this, arguments);
|
92
|
+
};
|
93
|
+
}();
|
94
|
+
|
95
|
+
exports["default"] = _default;
|
package/main/index.js
CHANGED
@@ -6,39 +6,67 @@ Object.defineProperty(exports, "__esModule", {
|
|
6
6
|
value: true
|
7
7
|
});
|
8
8
|
var _exportNames = {
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
generateTypes: true,
|
10
|
+
generateClient: true,
|
11
|
+
generateMessageComposer: true,
|
12
|
+
generateReactQuery: true,
|
13
|
+
generateRecoil: true
|
13
14
|
};
|
14
|
-
|
15
|
+
exports["default"] = void 0;
|
16
|
+
Object.defineProperty(exports, "generateClient", {
|
15
17
|
enumerable: true,
|
16
18
|
get: function get() {
|
17
|
-
return
|
19
|
+
return _client["default"];
|
18
20
|
}
|
19
21
|
});
|
20
|
-
Object.defineProperty(exports, "
|
22
|
+
Object.defineProperty(exports, "generateMessageComposer", {
|
23
|
+
enumerable: true,
|
24
|
+
get: function get() {
|
25
|
+
return _messageComposer["default"];
|
26
|
+
}
|
27
|
+
});
|
28
|
+
Object.defineProperty(exports, "generateReactQuery", {
|
21
29
|
enumerable: true,
|
22
30
|
get: function get() {
|
23
31
|
return _reactQuery["default"];
|
24
32
|
}
|
25
33
|
});
|
26
|
-
Object.defineProperty(exports, "
|
34
|
+
Object.defineProperty(exports, "generateRecoil", {
|
27
35
|
enumerable: true,
|
28
36
|
get: function get() {
|
29
37
|
return _recoil["default"];
|
30
38
|
}
|
31
39
|
});
|
32
|
-
Object.defineProperty(exports, "
|
40
|
+
Object.defineProperty(exports, "generateTypes", {
|
33
41
|
enumerable: true,
|
34
42
|
get: function get() {
|
35
|
-
return
|
43
|
+
return _types["default"];
|
36
44
|
}
|
37
45
|
});
|
38
46
|
|
39
|
-
var
|
47
|
+
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
48
|
+
|
49
|
+
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
50
|
+
|
51
|
+
var _builder = require("./builder");
|
52
|
+
|
53
|
+
Object.keys(_builder).forEach(function (key) {
|
54
|
+
if (key === "default" || key === "__esModule") return;
|
55
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
56
|
+
if (key in exports && exports[key] === _builder[key]) return;
|
57
|
+
Object.defineProperty(exports, key, {
|
58
|
+
enumerable: true,
|
59
|
+
get: function get() {
|
60
|
+
return _builder[key];
|
61
|
+
}
|
62
|
+
});
|
63
|
+
});
|
64
|
+
|
65
|
+
var _types = _interopRequireDefault(require("./generators/types"));
|
66
|
+
|
67
|
+
var _client = _interopRequireDefault(require("./generators/client"));
|
40
68
|
|
41
|
-
var
|
69
|
+
var _messageComposer = _interopRequireDefault(require("./generators/message-composer"));
|
42
70
|
|
43
71
|
var _reactQuery = _interopRequireDefault(require("./generators/react-query"));
|
44
72
|
|
@@ -58,16 +86,42 @@ Object.keys(_utils).forEach(function (key) {
|
|
58
86
|
});
|
59
87
|
});
|
60
88
|
|
61
|
-
var
|
89
|
+
var _bundler = require("./bundler");
|
62
90
|
|
63
|
-
Object.keys(
|
91
|
+
Object.keys(_bundler).forEach(function (key) {
|
64
92
|
if (key === "default" || key === "__esModule") return;
|
65
93
|
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
66
|
-
if (key in exports && exports[key] ===
|
94
|
+
if (key in exports && exports[key] === _bundler[key]) return;
|
67
95
|
Object.defineProperty(exports, key, {
|
68
96
|
enumerable: true,
|
69
97
|
get: function get() {
|
70
|
-
return
|
98
|
+
return _bundler[key];
|
71
99
|
}
|
72
100
|
});
|
73
|
-
});
|
101
|
+
});
|
102
|
+
|
103
|
+
var _default = /*#__PURE__*/function () {
|
104
|
+
var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(input) {
|
105
|
+
var builder;
|
106
|
+
return _regenerator["default"].wrap(function _callee$(_context) {
|
107
|
+
while (1) {
|
108
|
+
switch (_context.prev = _context.next) {
|
109
|
+
case 0:
|
110
|
+
builder = new _builder.TSBuilder(input);
|
111
|
+
_context.next = 3;
|
112
|
+
return builder.build();
|
113
|
+
|
114
|
+
case 3:
|
115
|
+
case "end":
|
116
|
+
return _context.stop();
|
117
|
+
}
|
118
|
+
}
|
119
|
+
}, _callee);
|
120
|
+
}));
|
121
|
+
|
122
|
+
return function (_x) {
|
123
|
+
return _ref.apply(this, arguments);
|
124
|
+
};
|
125
|
+
}();
|
126
|
+
|
127
|
+
exports["default"] = _default;
|
package/main/types.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
"use strict";
|
package/main/utils/schemas.js
CHANGED
@@ -27,15 +27,17 @@ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (O
|
|
27
27
|
|
28
28
|
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) { (0, _defineProperty2["default"])(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; }
|
29
29
|
|
30
|
+
;
|
31
|
+
|
30
32
|
var readSchemas = /*#__PURE__*/function () {
|
31
33
|
var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(_ref) {
|
32
|
-
var schemaDir,
|
34
|
+
var schemaDir, schemaOptions, _ref$clean, clean, fn, files, schemas;
|
33
35
|
|
34
36
|
return _regenerator["default"].wrap(function _callee$(_context) {
|
35
37
|
while (1) {
|
36
38
|
switch (_context.prev = _context.next) {
|
37
39
|
case 0:
|
38
|
-
schemaDir = _ref.schemaDir,
|
40
|
+
schemaDir = _ref.schemaDir, schemaOptions = _ref.schemaOptions, _ref$clean = _ref.clean, clean = _ref$clean === void 0 ? true : _ref$clean;
|
39
41
|
fn = clean ? _cleanse.cleanse : function (str) {
|
40
42
|
return str;
|
41
43
|
};
|
@@ -44,7 +46,7 @@ var readSchemas = /*#__PURE__*/function () {
|
|
44
46
|
return JSON.parse((0, _fs.readFileSync)(file, 'utf-8'));
|
45
47
|
});
|
46
48
|
|
47
|
-
if (!
|
49
|
+
if (!(schemaOptions !== null && schemaOptions !== void 0 && schemaOptions.packed)) {
|
48
50
|
_context.next = 8;
|
49
51
|
break;
|
50
52
|
}
|
@@ -0,0 +1,178 @@
|
|
1
|
+
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
2
|
+
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
|
+
import { defaultOptions } from "wasm-ast-types";
|
9
|
+
import { header } from '../utils/header';
|
10
|
+
import { join } from "path";
|
11
|
+
import { writeFileSync } from 'fs';
|
12
|
+
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
|
+
import { basename } from 'path';
|
19
|
+
import { readSchemas } from '../utils';
|
20
|
+
import deepmerge from 'deepmerge';
|
21
|
+
import { pascal } from "case";
|
22
|
+
import { createFileBundle, recursiveModuleBundle } from "../bundler";
|
23
|
+
import generate from '@babel/generator';
|
24
|
+
import * as t from '@babel/types';
|
25
|
+
const defaultOpts = {
|
26
|
+
bundle: {
|
27
|
+
enabled: true,
|
28
|
+
scope: 'contracts',
|
29
|
+
bundleFile: 'bundle.ts'
|
30
|
+
}
|
31
|
+
};
|
32
|
+
;
|
33
|
+
;
|
34
|
+
;
|
35
|
+
export class TSBuilder {
|
36
|
+
constructor({
|
37
|
+
contracts,
|
38
|
+
outPath,
|
39
|
+
options
|
40
|
+
}) {
|
41
|
+
_defineProperty(this, "contracts", void 0);
|
42
|
+
|
43
|
+
_defineProperty(this, "outPath", void 0);
|
44
|
+
|
45
|
+
_defineProperty(this, "options", void 0);
|
46
|
+
|
47
|
+
_defineProperty(this, "files", []);
|
48
|
+
|
49
|
+
this.contracts = contracts;
|
50
|
+
this.outPath = outPath;
|
51
|
+
this.options = deepmerge(deepmerge(defaultOptions, defaultOpts), options ?? {});
|
52
|
+
}
|
53
|
+
|
54
|
+
getContracts() {
|
55
|
+
return this.contracts.map(contractOpt => {
|
56
|
+
if (typeof contractOpt === 'string') {
|
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 schemas = await readSchemas({
|
81
|
+
schemaDir: contract.dir
|
82
|
+
});
|
83
|
+
const files = await generateTypes(contract.name, schemas, this.outPath, options);
|
84
|
+
[].push.apply(this.files, files);
|
85
|
+
}
|
86
|
+
|
87
|
+
async renderClient(contract) {
|
88
|
+
const _this$options$client = this.options.client,
|
89
|
+
{
|
90
|
+
enabled
|
91
|
+
} = _this$options$client,
|
92
|
+
options = _objectWithoutProperties(_this$options$client, _excluded2);
|
93
|
+
|
94
|
+
if (!enabled) return;
|
95
|
+
const schemas = await readSchemas({
|
96
|
+
schemaDir: contract.dir
|
97
|
+
});
|
98
|
+
const files = await generateClient(contract.name, schemas, this.outPath, options);
|
99
|
+
[].push.apply(this.files, files);
|
100
|
+
}
|
101
|
+
|
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
|
+
|
109
|
+
if (!enabled) return;
|
110
|
+
const schemas = await readSchemas({
|
111
|
+
schemaDir: contract.dir
|
112
|
+
});
|
113
|
+
const files = await generateRecoil(contract.name, schemas, this.outPath, options);
|
114
|
+
[].push.apply(this.files, files);
|
115
|
+
}
|
116
|
+
|
117
|
+
async renderReactQuery(contract) {
|
118
|
+
const _this$options$reactQu = this.options.reactQuery,
|
119
|
+
{
|
120
|
+
enabled
|
121
|
+
} = _this$options$reactQu,
|
122
|
+
options = _objectWithoutProperties(_this$options$reactQu, _excluded4);
|
123
|
+
|
124
|
+
if (!enabled) return;
|
125
|
+
const schemas = await readSchemas({
|
126
|
+
schemaDir: contract.dir
|
127
|
+
});
|
128
|
+
const files = await generateReactQuery(contract.name, schemas, this.outPath, options);
|
129
|
+
[].push.apply(this.files, files);
|
130
|
+
}
|
131
|
+
|
132
|
+
async renderMessageComposer(contract) {
|
133
|
+
const _this$options$message = this.options.messageComposer,
|
134
|
+
{
|
135
|
+
enabled
|
136
|
+
} = _this$options$message,
|
137
|
+
options = _objectWithoutProperties(_this$options$message, _excluded5);
|
138
|
+
|
139
|
+
if (!enabled) return;
|
140
|
+
const schemas = await readSchemas({
|
141
|
+
schemaDir: contract.dir
|
142
|
+
});
|
143
|
+
const files = await generateMessageComposer(contract.name, schemas, this.outPath, options);
|
144
|
+
[].push.apply(this.files, files);
|
145
|
+
}
|
146
|
+
|
147
|
+
async build() {
|
148
|
+
const contracts = this.getContracts();
|
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);
|
157
|
+
}
|
158
|
+
|
159
|
+
if (this.options.bundle.enabled) {
|
160
|
+
this.bundle();
|
161
|
+
}
|
162
|
+
}
|
163
|
+
|
164
|
+
async bundle() {
|
165
|
+
const allFiles = this.files;
|
166
|
+
const bundleFile = this.options.bundle.bundleFile;
|
167
|
+
const bundleVariables = {};
|
168
|
+
const importPaths = [];
|
169
|
+
allFiles.forEach(file => {
|
170
|
+
createFileBundle(`${this.options.bundle.scope}.${file.contract}`, file.localname, bundleFile, importPaths, bundleVariables);
|
171
|
+
});
|
172
|
+
const ast = recursiveModuleBundle(bundleVariables);
|
173
|
+
const code = generate(t.program([...importPaths, ...ast])).code;
|
174
|
+
mkdirp(this.outPath);
|
175
|
+
writeFileSync(join(this.outPath, bundleFile), header + code);
|
176
|
+
}
|
177
|
+
|
178
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './builder';
|
@@ -0,0 +1,36 @@
|
|
1
|
+
import * as t from '@babel/types';
|
2
|
+
import * as dotty from 'dotty';
|
3
|
+
import { relative, dirname, extname } from 'path';
|
4
|
+
export const recursiveModuleBundle = obj => {
|
5
|
+
return Object.keys(obj).map(key => {
|
6
|
+
if (obj[key]?.__export) {
|
7
|
+
// e.g. abci
|
8
|
+
// 1. create variable for abci
|
9
|
+
// 2. splat ALL _0, parms into abci
|
10
|
+
// 3. export that variable
|
11
|
+
const nmspc = t.variableDeclaration('const', [t.variableDeclarator(t.identifier(key), t.objectExpression(Object.keys(obj[key]).filter(a => a !== '__export').filter(a => a.startsWith('_')).map(a => t.spreadElement(t.identifier(a)))))]);
|
12
|
+
const others = Object.keys(obj[key]).filter(a => a !== '__export').filter(a => !a.startsWith('_'));
|
13
|
+
|
14
|
+
if (others.length) {
|
15
|
+
throw new Error('namespace and package not supported, yet.');
|
16
|
+
} // return nmspc;
|
17
|
+
|
18
|
+
|
19
|
+
return t.exportNamedDeclaration(nmspc, []);
|
20
|
+
} else {
|
21
|
+
// you can make a namespace for obj[key]
|
22
|
+
// e.g. libs
|
23
|
+
return t.exportNamedDeclaration(t.tsModuleDeclaration(t.identifier(key), t.tsModuleBlock(recursiveModuleBundle(obj[key]))));
|
24
|
+
}
|
25
|
+
});
|
26
|
+
};
|
27
|
+
export const importNamespace = (ident, path) => t.importDeclaration([t.importNamespaceSpecifier(t.identifier(ident))], t.stringLiteral(path.replace(extname(path), '')));
|
28
|
+
let counter = 0;
|
29
|
+
export const createFileBundle = (pkg, filename, bundleFile, importPaths, bundleVariables) => {
|
30
|
+
let rel = relative(dirname(bundleFile), filename);
|
31
|
+
if (!rel.startsWith('.')) rel = `./${rel}`;
|
32
|
+
const variable = `_${counter++}`;
|
33
|
+
importPaths.push(importNamespace(variable, rel));
|
34
|
+
dotty.put(bundleVariables, pkg + '.__export', true);
|
35
|
+
dotty.put(bundleVariables, pkg + '.' + variable, true);
|
36
|
+
};
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './bundler';
|
package/module/cmds.js
CHANGED
@@ -1,10 +1,6 @@
|
|
1
|
-
import
|
1
|
+
import _create_boilerplate from './commands/create-boilerplate';
|
2
2
|
import _generate from './commands/generate';
|
3
|
-
import _react_query from './commands/react-query';
|
4
|
-
import _recoil from './commands/recoil';
|
5
3
|
const Commands = {};
|
6
|
-
Commands['
|
4
|
+
Commands['create-boilerplate'] = _create_boilerplate;
|
7
5
|
Commands['generate'] = _generate;
|
8
|
-
Commands['react-query'] = _react_query;
|
9
|
-
Commands['recoil'] = _recoil;
|
10
6
|
export { Commands };
|