@gct-paas/cli 0.0.1-dev.2 → 0.0.1-dev.21

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 (206) hide show
  1. package/LICENSE +21 -0
  2. package/dist/actions/gen-api/api-manage.cjs +46 -0
  3. package/dist/actions/gen-api/api-manage.d.ts +36 -0
  4. package/dist/actions/gen-api/gen-api.cjs +157 -0
  5. package/dist/actions/gen-api/gen-api.d.ts +58 -0
  6. package/dist/actions/gen-api/model-manage.cjs +127 -0
  7. package/dist/actions/gen-api/model-manage.d.ts +124 -0
  8. package/dist/actions/index.cjs +12 -0
  9. package/dist/actions/index.d.ts +1 -0
  10. package/dist/bin.cjs +5 -0
  11. package/dist/commands/gen-api/gen-api.cjs +16 -0
  12. package/dist/commands/gen-api/gen-api.d.ts +44 -0
  13. package/dist/commands/index.cjs +23 -0
  14. package/dist/core/handlebars/constant/index.cjs +8 -0
  15. package/dist/core/handlebars/constant/index.d.ts +2 -0
  16. package/dist/core/handlebars/handlebars-engine.cjs +134 -0
  17. package/dist/core/handlebars/handlebars-engine.d.ts +74 -0
  18. package/dist/core/handlebars/helpers/and/and.cjs +21 -0
  19. package/dist/core/handlebars/helpers/and/and.d.ts +13 -0
  20. package/dist/core/handlebars/helpers/camel-case/camel-case.cjs +20 -0
  21. package/dist/core/handlebars/helpers/camel-case/camel-case.d.ts +13 -0
  22. package/dist/core/handlebars/helpers/concat/concat.cjs +17 -0
  23. package/dist/core/handlebars/helpers/concat/concat.d.ts +13 -0
  24. package/dist/core/handlebars/helpers/eq/eq.cjs +19 -0
  25. package/dist/core/handlebars/helpers/eq/eq.d.ts +14 -0
  26. package/dist/core/handlebars/helpers/format-import/format-import.cjs +41 -0
  27. package/dist/core/handlebars/helpers/format-import/format-import.d.ts +13 -0
  28. package/dist/core/handlebars/helpers/format-import-item/format-import-item.cjs +40 -0
  29. package/dist/core/handlebars/helpers/format-import-item/format-import-item.d.ts +13 -0
  30. package/dist/core/handlebars/helpers/format-js-type/format-js-type.cjs +41 -0
  31. package/dist/core/handlebars/helpers/format-js-type/format-js-type.d.ts +14 -0
  32. package/dist/core/handlebars/helpers/gt/gt.cjs +19 -0
  33. package/dist/core/handlebars/helpers/gt/gt.d.ts +14 -0
  34. package/dist/core/handlebars/helpers/gte/gte.cjs +19 -0
  35. package/dist/core/handlebars/helpers/gte/gte.d.ts +14 -0
  36. package/dist/core/handlebars/helpers/helper-base.cjs +19 -0
  37. package/dist/core/handlebars/helpers/helper-base.d.ts +22 -0
  38. package/dist/core/handlebars/helpers/includes/includes.cjs +18 -0
  39. package/dist/core/handlebars/helpers/includes/includes.d.ts +13 -0
  40. package/dist/core/handlebars/helpers/index.cjs +52 -0
  41. package/dist/core/handlebars/helpers/index.d.ts +6 -0
  42. package/dist/core/handlebars/helpers/json/json.cjs +16 -0
  43. package/dist/core/handlebars/helpers/json/json.d.ts +13 -0
  44. package/dist/core/handlebars/helpers/lower-case/lower-case.cjs +19 -0
  45. package/dist/core/handlebars/helpers/lower-case/lower-case.d.ts +13 -0
  46. package/dist/core/handlebars/helpers/lt/lt.cjs +19 -0
  47. package/dist/core/handlebars/helpers/lt/lt.d.ts +14 -0
  48. package/dist/core/handlebars/helpers/lte/lte.cjs +19 -0
  49. package/dist/core/handlebars/helpers/lte/lte.d.ts +14 -0
  50. package/dist/core/handlebars/helpers/neq/neq.cjs +19 -0
  51. package/dist/core/handlebars/helpers/neq/neq.d.ts +14 -0
  52. package/dist/core/handlebars/helpers/not/not.cjs +21 -0
  53. package/dist/core/handlebars/helpers/not/not.d.ts +14 -0
  54. package/dist/core/handlebars/helpers/not-includes/not-includes.cjs +18 -0
  55. package/dist/core/handlebars/helpers/not-includes/not-includes.d.ts +13 -0
  56. package/dist/core/handlebars/helpers/or/or.cjs +23 -0
  57. package/dist/core/handlebars/helpers/or/or.d.ts +13 -0
  58. package/dist/core/handlebars/helpers/pascal-case/pascal-case.cjs +20 -0
  59. package/dist/core/handlebars/helpers/pascal-case/pascal-case.d.ts +13 -0
  60. package/dist/core/handlebars/helpers/snake-case/snake-case.cjs +20 -0
  61. package/dist/core/handlebars/helpers/snake-case/snake-case.d.ts +13 -0
  62. package/dist/core/handlebars/helpers/spinal-case/spinal-case.cjs +20 -0
  63. package/dist/core/handlebars/helpers/spinal-case/spinal-case.d.ts +13 -0
  64. package/dist/core/handlebars/helpers/upper-case/upper-case.cjs +19 -0
  65. package/dist/core/handlebars/helpers/upper-case/upper-case.d.ts +13 -0
  66. package/dist/core/handlebars/index.cjs +27 -0
  67. package/dist/core/handlebars/index.d.ts +2 -0
  68. package/dist/core/handlebars/utils/helper/helper.cjs +41 -0
  69. package/dist/core/handlebars/utils/helper/helper.d.ts +33 -0
  70. package/dist/core/handlebars/utils/index.cjs +20 -0
  71. package/dist/core/handlebars/utils/index.d.ts +17 -0
  72. package/dist/core/index.cjs +27 -0
  73. package/dist/core/index.d.ts +2 -7
  74. package/dist/core/interface/index.cjs +1 -0
  75. package/dist/core/interface/index.d.ts +1 -0
  76. package/dist/index.cjs +47 -0
  77. package/dist/interface/i-command/i-command.cjs +1 -0
  78. package/dist/interface/i-command/i-command.d.ts +0 -6
  79. package/dist/interface/index.cjs +16 -0
  80. package/dist/utils/index.cjs +18 -0
  81. package/dist/utils/local-binaries/local-binaries.cjs +19 -0
  82. package/es/actions/gen-api/api-manage.d.ts +36 -0
  83. package/es/actions/gen-api/api-manage.mjs +38 -0
  84. package/es/actions/gen-api/gen-api.d.ts +58 -0
  85. package/es/actions/gen-api/gen-api.mjs +164 -0
  86. package/es/actions/gen-api/model-manage.d.ts +124 -0
  87. package/es/actions/gen-api/model-manage.mjs +126 -0
  88. package/es/actions/index.d.ts +1 -0
  89. package/es/actions/index.mjs +1 -0
  90. package/es/bin.d.ts +2 -0
  91. package/es/commands/gen-api/gen-api.d.ts +44 -0
  92. package/es/commands/gen-api/gen-api.mjs +15 -0
  93. package/es/commands/index.d.ts +5 -0
  94. package/{dist → es}/commands/index.mjs +2 -0
  95. package/es/core/handlebars/constant/index.d.ts +2 -0
  96. package/es/core/handlebars/constant/index.mjs +2 -0
  97. package/es/core/handlebars/handlebars-engine.d.ts +74 -0
  98. package/es/core/handlebars/handlebars-engine.mjs +122 -0
  99. package/es/core/handlebars/helpers/and/and.d.ts +13 -0
  100. package/es/core/handlebars/helpers/and/and.mjs +14 -0
  101. package/es/core/handlebars/helpers/camel-case/camel-case.d.ts +13 -0
  102. package/es/core/handlebars/helpers/camel-case/camel-case.mjs +13 -0
  103. package/es/core/handlebars/helpers/concat/concat.d.ts +13 -0
  104. package/es/core/handlebars/helpers/concat/concat.mjs +10 -0
  105. package/es/core/handlebars/helpers/eq/eq.d.ts +14 -0
  106. package/es/core/handlebars/helpers/eq/eq.mjs +12 -0
  107. package/es/core/handlebars/helpers/format-import/format-import.d.ts +13 -0
  108. package/es/core/handlebars/helpers/format-import/format-import.mjs +34 -0
  109. package/es/core/handlebars/helpers/format-import-item/format-import-item.d.ts +13 -0
  110. package/es/core/handlebars/helpers/format-import-item/format-import-item.mjs +33 -0
  111. package/es/core/handlebars/helpers/format-js-type/format-js-type.d.ts +14 -0
  112. package/es/core/handlebars/helpers/format-js-type/format-js-type.mjs +34 -0
  113. package/es/core/handlebars/helpers/gt/gt.d.ts +14 -0
  114. package/es/core/handlebars/helpers/gt/gt.mjs +12 -0
  115. package/es/core/handlebars/helpers/gte/gte.d.ts +14 -0
  116. package/es/core/handlebars/helpers/gte/gte.mjs +12 -0
  117. package/es/core/handlebars/helpers/helper-base.d.ts +22 -0
  118. package/es/core/handlebars/helpers/helper-base.mjs +11 -0
  119. package/es/core/handlebars/helpers/includes/includes.d.ts +13 -0
  120. package/es/core/handlebars/helpers/includes/includes.mjs +11 -0
  121. package/es/core/handlebars/helpers/index.d.ts +6 -0
  122. package/es/core/handlebars/helpers/index.mjs +46 -0
  123. package/es/core/handlebars/helpers/json/json.d.ts +13 -0
  124. package/es/core/handlebars/helpers/json/json.mjs +13 -0
  125. package/es/core/handlebars/helpers/lower-case/lower-case.d.ts +13 -0
  126. package/es/core/handlebars/helpers/lower-case/lower-case.mjs +12 -0
  127. package/es/core/handlebars/helpers/lt/lt.d.ts +14 -0
  128. package/es/core/handlebars/helpers/lt/lt.mjs +12 -0
  129. package/es/core/handlebars/helpers/lte/lte.d.ts +14 -0
  130. package/es/core/handlebars/helpers/lte/lte.mjs +12 -0
  131. package/es/core/handlebars/helpers/neq/neq.d.ts +14 -0
  132. package/es/core/handlebars/helpers/neq/neq.mjs +12 -0
  133. package/es/core/handlebars/helpers/not/not.d.ts +14 -0
  134. package/es/core/handlebars/helpers/not/not.mjs +12 -0
  135. package/es/core/handlebars/helpers/not-includes/not-includes.d.ts +13 -0
  136. package/es/core/handlebars/helpers/not-includes/not-includes.mjs +11 -0
  137. package/es/core/handlebars/helpers/or/or.d.ts +13 -0
  138. package/es/core/handlebars/helpers/or/or.mjs +16 -0
  139. package/es/core/handlebars/helpers/pascal-case/pascal-case.d.ts +13 -0
  140. package/es/core/handlebars/helpers/pascal-case/pascal-case.mjs +13 -0
  141. package/es/core/handlebars/helpers/snake-case/snake-case.d.ts +13 -0
  142. package/es/core/handlebars/helpers/snake-case/snake-case.mjs +13 -0
  143. package/es/core/handlebars/helpers/spinal-case/spinal-case.d.ts +13 -0
  144. package/es/core/handlebars/helpers/spinal-case/spinal-case.mjs +13 -0
  145. package/es/core/handlebars/helpers/upper-case/upper-case.d.ts +13 -0
  146. package/es/core/handlebars/helpers/upper-case/upper-case.mjs +12 -0
  147. package/es/core/handlebars/index.d.ts +2 -0
  148. package/es/core/handlebars/index.mjs +2 -0
  149. package/es/core/handlebars/utils/helper/helper.d.ts +33 -0
  150. package/es/core/handlebars/utils/helper/helper.mjs +54 -0
  151. package/es/core/handlebars/utils/index.d.ts +17 -0
  152. package/es/core/handlebars/utils/index.mjs +7 -0
  153. package/es/core/index.d.ts +2 -0
  154. package/es/core/index.mjs +2 -0
  155. package/es/core/interface/index.d.ts +1 -0
  156. package/es/index.d.ts +2 -0
  157. package/es/interface/i-command/i-command.d.ts +22 -0
  158. package/es/interface/i-command/i-command.mjs +0 -0
  159. package/es/interface/index.d.ts +1 -0
  160. package/es/types/index.d.ts +1 -0
  161. package/es/utils/index.d.ts +1 -0
  162. package/es/utils/local-binaries/local-binaries.d.ts +3 -0
  163. package/package.json +28 -46
  164. package/dist/core/commitlint/commitlint-config/commitlint-config.d.ts +0 -9
  165. package/dist/core/commitlint/commitlint-config/commitlint-config.mjs +0 -28
  166. package/dist/core/commitlint/index.d.ts +0 -1
  167. package/dist/core/commitlint/index.mjs +0 -1
  168. package/dist/core/constants/index.d.ts +0 -8
  169. package/dist/core/constants/index.mjs +0 -2
  170. package/dist/core/eslint/eslint-config/eslint-config.d.ts +0 -9
  171. package/dist/core/eslint/eslint-config/eslint-config.mjs +0 -109
  172. package/dist/core/eslint/index.d.ts +0 -1
  173. package/dist/core/eslint/index.mjs +0 -1
  174. package/dist/core/index.mjs +0 -7
  175. package/dist/core/prettier/index.d.ts +0 -1
  176. package/dist/core/prettier/index.mjs +0 -1
  177. package/dist/core/prettier/prettier-config/prettier-config.d.ts +0 -22
  178. package/dist/core/prettier/prettier-config/prettier-config.mjs +0 -26
  179. package/dist/core/rollup/index.d.ts +0 -2
  180. package/dist/core/rollup/index.mjs +0 -2
  181. package/dist/core/rollup/rollup-config/dev-config.d.ts +0 -9
  182. package/dist/core/rollup/rollup-config/dev-config.mjs +0 -98
  183. package/dist/core/rollup/rollup-plugins/copy-file.d.ts +0 -37
  184. package/dist/core/rollup/rollup-plugins/copy-file.mjs +0 -18
  185. package/dist/core/rollup/rollup-plugins/ignore-compiler-file.d.ts +0 -8
  186. package/dist/core/rollup/rollup-plugins/ignore-compiler-file.mjs +0 -17
  187. package/dist/core/rollup/rollup-plugins/index.d.ts +0 -2
  188. package/dist/core/rollup/rollup-plugins/index.mjs +0 -2
  189. package/dist/core/stylelint/index.d.ts +0 -1
  190. package/dist/core/stylelint/index.mjs +0 -1
  191. package/dist/core/stylelint/stylelint-config/stylelint-config.d.ts +0 -9
  192. package/dist/core/stylelint/stylelint-config/stylelint-config.mjs +0 -21
  193. package/dist/core/util/index.d.ts +0 -21
  194. package/dist/core/util/index.mjs +0 -7
  195. package/dist/core/util/watcher/watcher.d.ts +0 -61
  196. package/dist/core/util/watcher/watcher.mjs +0 -108
  197. package/dist/core/vite/index.d.ts +0 -1
  198. package/dist/core/vite/index.mjs +0 -1
  199. package/dist/core/vite/vite-config/vite-config.d.ts +0 -9
  200. package/dist/core/vite/vite-config/vite-config.mjs +0 -47
  201. /package/{dist → es}/bin.mjs +0 -0
  202. /package/{dist/interface/i-command/i-command.mjs → es/core/interface/index.mjs} +0 -0
  203. /package/{dist → es}/index.mjs +0 -0
  204. /package/{dist → es}/interface/index.mjs +0 -0
  205. /package/{dist → es}/utils/index.mjs +0 -0
  206. /package/{dist → es}/utils/local-binaries/local-binaries.mjs +0 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 冠骋信息技术有限公司
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.EntityApiManage = exports.ApiManage = void 0;
7
+ class EntityApiManage {
8
+ constructor(name) {
9
+ this.name = name;
10
+ }
11
+ cache = /* @__PURE__ */new Map();
12
+ /**
13
+ * 新增方法
14
+ *
15
+ * @param {string} method 方法名称
16
+ * @param {string} mode 请求方式
17
+ * @param {IObject} cfg 从 swagger 获取的配置
18
+ */
19
+ add(method, mode, cfg) {
20
+ const tag = `${method}:${mode}`;
21
+ this.cache.set(tag, cfg);
22
+ }
23
+ }
24
+ exports.EntityApiManage = EntityApiManage;
25
+ class ApiManage {
26
+ cache = /* @__PURE__ */new Map();
27
+ /**
28
+ * 新增 api
29
+ *
30
+ * @param {string} pathStr 请求路径
31
+ * @param {IObject} cfg swagger 配置
32
+ */
33
+ add(pathStr, cfg) {
34
+ const entityName = pathStr.split("/")[3];
35
+ const method = pathStr.split("/")[4];
36
+ if (!this.cache.has(entityName)) {
37
+ this.cache.set(entityName, new EntityApiManage(entityName));
38
+ }
39
+ const entity = this.cache.get(entityName);
40
+ const modes = Object.keys(cfg);
41
+ modes.forEach(mode => {
42
+ entity.add(method, mode, cfg[mode]);
43
+ });
44
+ }
45
+ }
46
+ exports.ApiManage = ApiManage;
@@ -0,0 +1,36 @@
1
+ import { IObject } from '../../core';
2
+ /**
3
+ * api 管理
4
+ *
5
+ * @export
6
+ * @class EntityApiManage
7
+ */
8
+ export declare class EntityApiManage {
9
+ readonly name: string;
10
+ readonly cache: Map<string, IObject>;
11
+ constructor(name: string);
12
+ /**
13
+ * 新增方法
14
+ *
15
+ * @param {string} method 方法名称
16
+ * @param {string} mode 请求方式
17
+ * @param {IObject} cfg 从 swagger 获取的配置
18
+ */
19
+ add(method: string, mode: string, cfg: IObject): void;
20
+ }
21
+ /**
22
+ * api 清单管理
23
+ *
24
+ * @export
25
+ * @class ApiManage
26
+ */
27
+ export declare class ApiManage {
28
+ readonly cache: Map<string, EntityApiManage>;
29
+ /**
30
+ * 新增 api
31
+ *
32
+ * @param {string} pathStr 请求路径
33
+ * @param {IObject} cfg swagger 配置
34
+ */
35
+ add(pathStr: string, cfg: IObject): void;
36
+ }
@@ -0,0 +1,157 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.GenApiAction = void 0;
7
+ var _path = _interopRequireDefault(require("path"));
8
+ var _fsExtra = _interopRequireDefault(require("fs-extra"));
9
+ var rm = _interopRequireWildcard(require("rimraf"));
10
+ var _fastGlob = _interopRequireDefault(require("fast-glob"));
11
+ var _consola = _interopRequireDefault(require("consola"));
12
+ var _got = _interopRequireDefault(require("got"));
13
+ var _core = require("../../core/index.cjs");
14
+ var _modelManage = require("./model-manage.cjs");
15
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
16
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
17
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
18
+ class GenApiAction {
19
+ // private baseUrl = 'http://paas.paasdev.gct-paas.com';
20
+ engine = new _core.HandlebarsEngine();
21
+ modelManage = new _modelManage.ModelManage();
22
+ configs = {
23
+ platform: "/gct-platform/v2/api-docs?group=%E7%AE%A1%E7%90%86%E5%B9%B3%E5%8F%B0API",
24
+ apaas: "/gct-apaas/v2/api-docs?group=apaas%E5%B9%B3%E5%8F%B0API"
25
+ };
26
+ /**
27
+ * 生成 api
28
+ *
29
+ * @param {string} baseUrl swagger 服务地址
30
+ * @param {string} tag 要发布的 api 标识
31
+ * @param {string} template 模板目录
32
+ * @param {string} output 输出目录
33
+ * @param {boolean} clean 是否清理输出目录后再生成
34
+ */
35
+ async run(baseUrl, tag, template, output, clean) {
36
+ const cwd = process.cwd();
37
+ const tempPath = _path.default.resolve(cwd, template);
38
+ const outPath = _path.default.resolve(cwd, output);
39
+ if (clean === true && _fsExtra.default.existsSync(outPath)) {
40
+ rm.sync(outPath);
41
+ }
42
+ await this.registerMacros(tempPath, this.engine);
43
+ await this.registerTempFiles(tempPath, this.engine);
44
+ await this.write(tag, _path.default.join(baseUrl, this.configs[tag]), outPath, this.engine);
45
+ }
46
+ /**
47
+ * 基于模型编译输出文件
48
+ *
49
+ * @protected
50
+ * @param {string} url
51
+ * @param {string} outPath
52
+ * @param {HandlebarsEngine} engine
53
+ * @returns {*} {Promise<void>}
54
+ */
55
+ async write(tag, url, outPath, engine) {
56
+ const cfg = await (0, _got.default)(url).json();
57
+ const model = this.transformModel(cfg);
58
+ this.modelManage.init(tag, model);
59
+ const all = [];
60
+ engine.temps.forEach((template, key) => {
61
+ if (key === "") {
62
+ return;
63
+ }
64
+ const outFilePath = _path.default.join(outPath, key);
65
+ const tags = engine.parseContextTags(key);
66
+ if (tags.length === 0) {
67
+ _consola.default.info(`\u751F\u6210\u6587\u4EF6\uFF1A${outFilePath}`);
68
+ all.push(_fsExtra.default.outputFile(outFilePath, engine.render(template, {
69
+ entities: Array.from(this.modelManage.entities.values()),
70
+ definitions: model.definitions
71
+ }), "utf-8"));
72
+ } else {
73
+ if (tags.includes("api")) {
74
+ this.modelManage.entities.forEach(model2 => {
75
+ const filePath = engine.render(outFilePath, model2);
76
+ _consola.default.info(`\u751F\u6210\u6587\u4EF6\uFF1A${filePath}`);
77
+ all.push(_fsExtra.default.outputFile(filePath, engine.render(template, model2), "utf-8"));
78
+ });
79
+ }
80
+ }
81
+ });
82
+ await Promise.all(all);
83
+ }
84
+ /**
85
+ * 注册所有的宏
86
+ *
87
+ * @protected
88
+ * @param {string} tempPath
89
+ * @param {HandlebarsEngine} engine
90
+ * @returns {*} {Promise<void>}
91
+ */
92
+ async registerMacros(tempPath, engine) {
93
+ const macroFiles = _fastGlob.default.sync(_path.default.join(_core.MACRO_FOLDER, "**/*.hbs"), {
94
+ cwd: tempPath,
95
+ dot: true,
96
+ absolute: true
97
+ });
98
+ if (macroFiles.length > 0) {
99
+ await Promise.all(macroFiles.map(macroFilePath => {
100
+ return new Promise(resolve => {
101
+ _fsExtra.default.readFile(macroFilePath, "utf-8").then(file => {
102
+ const key = _path.default.relative(tempPath, macroFilePath);
103
+ _consola.default.info(`\u6CE8\u518C\u5B8F\uFF1A${key}`);
104
+ engine.registerMacro(key, file);
105
+ resolve();
106
+ });
107
+ });
108
+ }));
109
+ }
110
+ }
111
+ /**
112
+ * 注册所有模板文件
113
+ *
114
+ * @protected
115
+ * @param {string} tempPath
116
+ * @param {HandlebarsEngine} engine
117
+ * @returns {*} {Promise<void>}
118
+ */
119
+ async registerTempFiles(tempPath, engine) {
120
+ const tempFiles = _fastGlob.default.sync("**/*.hbs", {
121
+ cwd: tempPath,
122
+ dot: true,
123
+ absolute: true,
124
+ ignore: [_path.default.join(_core.MACRO_FOLDER, "**/*.hbs")]
125
+ });
126
+ if (tempFiles.length > 0) {
127
+ await Promise.all(tempFiles.map(tempFilePath => {
128
+ return new Promise(resolve => {
129
+ _fsExtra.default.readFile(tempFilePath, "utf-8").then(file => {
130
+ const key = _path.default.relative(tempPath, tempFilePath);
131
+ _consola.default.info(`\u6CE8\u518C\u6A21\u677F\uFF1A${key}`);
132
+ engine.registerTemp(key, file);
133
+ resolve();
134
+ });
135
+ });
136
+ }));
137
+ }
138
+ }
139
+ /**
140
+ * 针对加载的配置进行处理
141
+ *
142
+ * @protected
143
+ * @param {IObject} model
144
+ * @returns {*} {IObject}
145
+ */
146
+ transformModel(model) {
147
+ const keys = Object.keys(model.definitions);
148
+ keys.forEach(key => {
149
+ const item = model.definitions[key];
150
+ if (item.properties) {
151
+ item.propertySize = Object.keys(item.properties).length;
152
+ }
153
+ });
154
+ return model;
155
+ }
156
+ }
157
+ exports.GenApiAction = GenApiAction;
@@ -0,0 +1,58 @@
1
+ import { HandlebarsEngine, IObject } from '../../core';
2
+ /**
3
+ * 基于 handlebars 模板编写生成 api
4
+ *
5
+ * @export
6
+ * @class GenApiAction
7
+ */
8
+ export declare class GenApiAction {
9
+ private engine;
10
+ private modelManage;
11
+ private configs;
12
+ /**
13
+ * 生成 api
14
+ *
15
+ * @param {string} baseUrl swagger 服务地址
16
+ * @param {string} tag 要发布的 api 标识
17
+ * @param {string} template 模板目录
18
+ * @param {string} output 输出目录
19
+ * @param {boolean} clean 是否清理输出目录后再生成
20
+ */
21
+ run(baseUrl: string, tag: string, template: string, output: string, clean: boolean): Promise<void>;
22
+ /**
23
+ * 基于模型编译输出文件
24
+ *
25
+ * @protected
26
+ * @param {string} url
27
+ * @param {string} outPath
28
+ * @param {HandlebarsEngine} engine
29
+ * @returns {*} {Promise<void>}
30
+ */
31
+ protected write(tag: string, url: string, outPath: string, engine: HandlebarsEngine): Promise<void>;
32
+ /**
33
+ * 注册所有的宏
34
+ *
35
+ * @protected
36
+ * @param {string} tempPath
37
+ * @param {HandlebarsEngine} engine
38
+ * @returns {*} {Promise<void>}
39
+ */
40
+ protected registerMacros(tempPath: string, engine: HandlebarsEngine): Promise<void>;
41
+ /**
42
+ * 注册所有模板文件
43
+ *
44
+ * @protected
45
+ * @param {string} tempPath
46
+ * @param {HandlebarsEngine} engine
47
+ * @returns {*} {Promise<void>}
48
+ */
49
+ protected registerTempFiles(tempPath: string, engine: HandlebarsEngine): Promise<void>;
50
+ /**
51
+ * 针对加载的配置进行处理
52
+ *
53
+ * @protected
54
+ * @param {IObject} model
55
+ * @returns {*} {IObject}
56
+ */
57
+ protected transformModel(model: IObject): IObject;
58
+ }
@@ -0,0 +1,127 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.ModelManage = void 0;
7
+ var _apiManage = require("./api-manage.cjs");
8
+ class ModelManage {
9
+ /**
10
+ * 所有实体模型
11
+ *
12
+ * @type {Map<string, IEntityModel>}
13
+ */
14
+ entities = /* @__PURE__ */new Map();
15
+ /**
16
+ * 所有用到的实体定义
17
+ *
18
+ * @type {Map<string, IObject>}
19
+ */
20
+ definitions = /* @__PURE__ */new Map();
21
+ /**
22
+ * 目前接口中的基础数据类型
23
+ *
24
+ * @protected
25
+ */
26
+ basicDataType = ["any", "number", "string", "null", "boolean", "Void", "object"];
27
+ init(tag, cfg) {
28
+ const apiManage = new _apiManage.ApiManage();
29
+ const paths = Object.keys(cfg.paths).filter(pathStr => {
30
+ return !pathStr.startsWith("/external") || pathStr === "/external/api/tenant/info/byPortOrDomain";
31
+ });
32
+ paths.forEach(pathStr => {
33
+ const pathCfg = cfg.paths[pathStr];
34
+ apiManage.add(pathStr, pathCfg);
35
+ });
36
+ const entityNames = apiManage.cache.keys();
37
+ for (const entityName of entityNames) {
38
+ const entitySet = /* @__PURE__ */new Set();
39
+ const entityManage = apiManage.cache.get(entityName);
40
+ const methods = [];
41
+ const tags = entityManage.cache.keys();
42
+ for (const tag2 of tags) {
43
+ const [methodName, mode] = tag2.split(":");
44
+ const config = entityManage.cache.get(tag2);
45
+ let requestEntity = void 0;
46
+ const param = config.parameters.find(param2 => {
47
+ if (param2.in === "body") {
48
+ return param2;
49
+ }
50
+ });
51
+ if (param && param.schema) {
52
+ const refKey = this.getOriginRef(param.schema);
53
+ requestEntity = cfg.definitions[refKey];
54
+ if (requestEntity && Object.keys(requestEntity).length > 0) {
55
+ this.definitions.set(refKey, requestEntity);
56
+ entitySet.add(refKey);
57
+ }
58
+ }
59
+ let responseEntity = void 0;
60
+ if (config.responses["200"].schema) {
61
+ const refKey = this.getOriginRef(config.responses["200"].schema);
62
+ responseEntity = cfg.definitions[refKey];
63
+ if (responseEntity && Object.keys(responseEntity).length > 0) {
64
+ this.definitions.set(refKey, responseEntity);
65
+ entitySet.add(refKey);
66
+ }
67
+ }
68
+ const queries = config.parameters.filter(param2 => {
69
+ if (param2.in === "query") {
70
+ return param2;
71
+ }
72
+ });
73
+ if (methodName === "{id}") {
74
+ queries.push({
75
+ name: "id",
76
+ in: "query",
77
+ description: "\u6570\u636E\u4E3B\u952E",
78
+ required: true,
79
+ type: "string"
80
+ });
81
+ }
82
+ methods.push({
83
+ name: methodName !== "undefined" ? methodName : "",
84
+ mode,
85
+ model: config,
86
+ requestEntity,
87
+ responseEntity,
88
+ queries
89
+ });
90
+ }
91
+ this.entities.set(entityName, {
92
+ tag,
93
+ entityName,
94
+ api: entityName,
95
+ model: cfg.definitions[entityName],
96
+ methods,
97
+ entityRefs: Array.from(entitySet.values()).filter(item => {
98
+ return this.basicDataType.findIndex(type => {
99
+ return item.indexOf(`\xAB${type}\xBB`) !== -1;
100
+ }) === -1 && !this.basicDataType.includes(item);
101
+ })
102
+ });
103
+ }
104
+ }
105
+ /**
106
+ * 获取接口的引用对象
107
+ *
108
+ * @protected
109
+ * @param {IObject} data
110
+ * @returns {*} {string}
111
+ */
112
+ getOriginRef(data) {
113
+ const {
114
+ $ref,
115
+ originalRef,
116
+ type
117
+ } = data;
118
+ if (type) return type;
119
+ if (originalRef) return originalRef;
120
+ if ($ref) {
121
+ const _refs_ = $ref.split("/");
122
+ return _refs_[_refs_.length - 1];
123
+ }
124
+ return "any";
125
+ }
126
+ }
127
+ exports.ModelManage = ModelManage;
@@ -0,0 +1,124 @@
1
+ import { IObject } from '../../core';
2
+ /**
3
+ * 实体方法模型
4
+ *
5
+ * @export
6
+ * @interface IMethodModel
7
+ */
8
+ export interface IMethodModel {
9
+ /**
10
+ * 方法名称
11
+ *
12
+ * @type {string}
13
+ */
14
+ name: string;
15
+ /**
16
+ * 请求方式
17
+ *
18
+ * @type {string}
19
+ */
20
+ mode: string;
21
+ /**
22
+ * 方法模型
23
+ *
24
+ * @type {IObject}
25
+ */
26
+ model: IObject;
27
+ /**
28
+ * 请求实体
29
+ *
30
+ * @type {IObject}
31
+ */
32
+ requestEntity?: IObject;
33
+ /**
34
+ * 响应实体
35
+ *
36
+ * @type {IObject}
37
+ */
38
+ responseEntity?: IObject;
39
+ /**
40
+ * 查询参数
41
+ *
42
+ * @type {IObject[]}
43
+ */
44
+ queries: IObject[];
45
+ }
46
+ /**
47
+ * 实体模型
48
+ *
49
+ * @export
50
+ * @interface IEntityModel
51
+ */
52
+ export interface IEntityModel {
53
+ /**
54
+ * 实体名称
55
+ *
56
+ * @type {string}
57
+ */
58
+ entityName: string;
59
+ /**
60
+ * 平台标识 apaas, platform
61
+ *
62
+ * @type {string}
63
+ */
64
+ tag: string;
65
+ /**
66
+ * 实体名称(同name)
67
+ *
68
+ * @type {string}
69
+ */
70
+ api: string;
71
+ /**
72
+ * 实体模型
73
+ *
74
+ * @type {IObject}
75
+ */
76
+ model: IObject;
77
+ /**
78
+ * 实体方法模型
79
+ *
80
+ * @type {IMethodModel[]}
81
+ */
82
+ methods: IMethodModel[];
83
+ /**
84
+ * 实体引用实体名称集合
85
+ *
86
+ * @type {string[]}
87
+ */
88
+ entityRefs: string[];
89
+ }
90
+ /**
91
+ * 模型管理
92
+ *
93
+ * @export
94
+ * @class ModelManage
95
+ */
96
+ export declare class ModelManage {
97
+ /**
98
+ * 所有实体模型
99
+ *
100
+ * @type {Map<string, IEntityModel>}
101
+ */
102
+ readonly entities: Map<string, IEntityModel>;
103
+ /**
104
+ * 所有用到的实体定义
105
+ *
106
+ * @type {Map<string, IObject>}
107
+ */
108
+ readonly definitions: Map<string, IObject>;
109
+ /**
110
+ * 目前接口中的基础数据类型
111
+ *
112
+ * @protected
113
+ */
114
+ protected basicDataType: string[];
115
+ init(tag: string, cfg: IObject): void;
116
+ /**
117
+ * 获取接口的引用对象
118
+ *
119
+ * @protected
120
+ * @param {IObject} data
121
+ * @returns {*} {string}
122
+ */
123
+ protected getOriginRef(data: IObject): string;
124
+ }
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "GenApiAction", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _genApi.GenApiAction;
10
+ }
11
+ });
12
+ var _genApi = require("./gen-api/gen-api.cjs");
@@ -0,0 +1 @@
1
+ export { GenApiAction } from './gen-api/gen-api';
package/dist/bin.cjs ADDED
@@ -0,0 +1,5 @@
1
+ // #!/usr/bin/env node
2
+ "use strict";
3
+
4
+ var _index = require("./index.cjs");
5
+ (0, _index.bootstrap)();
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.GenApiCommand = void 0;
7
+ var _actions = require("../../actions/index.cjs");
8
+ class GenApiCommand {
9
+ load(program) {
10
+ program.command("gen-api").description("\u751F\u6210 api \u6587\u4EF6").option("--url <base-url>", "\u6307\u5B9Aapi\u83B7\u53D6\u7684swagger\u5730\u5740").option("-t --template <template>", "\u6307\u5B9A\u6A21\u677F\u76EE\u5F55").option("-o --output <output>", "\u6307\u5B9A\u8F93\u51FA\u76EE\u5F55").option("--tag <tag>", "\u6307\u5B9A\u5F53\u524D\u53D1\u5E03\u7684\u5206\u652F").option("--clean", "\u6E05\u7406\u8F93\u51FA\u76EE\u5F55").action(this.action.bind(this));
11
+ }
12
+ async action(args) {
13
+ await new _actions.GenApiAction().run(args.url, args.tag, args.template, args.output, args.clean);
14
+ }
15
+ }
16
+ exports.GenApiCommand = GenApiCommand;
@@ -0,0 +1,44 @@
1
+ import { Command } from 'commander';
2
+ import { ICommand } from '../../interface';
3
+ export interface GenApiOptions {
4
+ /**
5
+ * 模板目录
6
+ *
7
+ * @author chitanda
8
+ * @date 2021-12-19 20:12:32
9
+ * @type {string}
10
+ */
11
+ template: string;
12
+ /**
13
+ * 模板输出目录
14
+ *
15
+ * @author chitanda
16
+ * @date 2021-12-19 20:12:37
17
+ * @type {string}
18
+ */
19
+ output: string;
20
+ /**
21
+ * 清理输出目录
22
+ *
23
+ * @author chitanda
24
+ * @date 2021-12-21 10:12:47
25
+ * @type {boolean}
26
+ */
27
+ clean: boolean;
28
+ /**
29
+ * swagger 地址
30
+ *
31
+ * @type {string}
32
+ */
33
+ url: string;
34
+ /**
35
+ * api标识
36
+ *
37
+ * @type {string}
38
+ */
39
+ tag: string;
40
+ }
41
+ export declare class GenApiCommand implements ICommand {
42
+ load(program: Command): void;
43
+ action(args: GenApiOptions): Promise<void>;
44
+ }
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.CommandLoader = void 0;
7
+ var _consola = require("consola");
8
+ var _genApi = require("./gen-api/gen-api.cjs");
9
+ class CommandLoader {
10
+ static load(program) {
11
+ new _genApi.GenApiCommand().load(program);
12
+ this.handleInvalidCommand(program);
13
+ }
14
+ static handleInvalidCommand(program) {
15
+ program.on("command:*", () => {
16
+ _consola.consola.error(`\u672A\u652F\u6301\u7684\u547D\u4EE4: `, program.args.join(" "));
17
+ _consola.consola.log(`\u8BF7\u4F7F\u7528 --help \u67E5\u770B\u5DF2\u652F\u6301\u7684\u547D\u4EE4.
18
+ `);
19
+ process.exit(1);
20
+ });
21
+ }
22
+ }
23
+ exports.CommandLoader = CommandLoader;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.TEMP_EXTENSION = exports.MACRO_FOLDER = void 0;
7
+ const TEMP_EXTENSION = exports.TEMP_EXTENSION = ".hbs";
8
+ const MACRO_FOLDER = exports.MACRO_FOLDER = "@macro";
@@ -0,0 +1,2 @@
1
+ export declare const TEMP_EXTENSION = ".hbs";
2
+ export declare const MACRO_FOLDER = "@macro";