@gct-paas/cli 0.0.1-dev.9 → 0.1.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 (149) hide show
  1. package/dist/actions/gen-api/api-manage.cjs +46 -0
  2. package/dist/actions/gen-api/api-manage.d.ts +36 -0
  3. package/dist/actions/gen-api/gen-api.cjs +157 -0
  4. package/dist/actions/gen-api/gen-api.d.ts +58 -0
  5. package/dist/actions/gen-api/model-manage.cjs +127 -0
  6. package/dist/actions/gen-api/model-manage.d.ts +124 -0
  7. package/dist/actions/index.cjs +12 -0
  8. package/dist/actions/index.d.ts +1 -0
  9. package/dist/commands/gen-api/gen-api.cjs +16 -0
  10. package/dist/commands/gen-api/gen-api.d.ts +44 -0
  11. package/dist/commands/index.cjs +2 -0
  12. package/dist/core/handlebars/constant/index.cjs +8 -0
  13. package/dist/core/handlebars/constant/index.d.ts +2 -0
  14. package/dist/core/handlebars/handlebars-engine.cjs +134 -0
  15. package/dist/core/handlebars/handlebars-engine.d.ts +74 -0
  16. package/dist/core/handlebars/helpers/and/and.cjs +21 -0
  17. package/dist/core/handlebars/helpers/and/and.d.ts +13 -0
  18. package/dist/core/handlebars/helpers/camel-case/camel-case.cjs +20 -0
  19. package/dist/core/handlebars/helpers/camel-case/camel-case.d.ts +13 -0
  20. package/dist/core/handlebars/helpers/concat/concat.cjs +17 -0
  21. package/dist/core/handlebars/helpers/concat/concat.d.ts +13 -0
  22. package/dist/core/handlebars/helpers/eq/eq.cjs +19 -0
  23. package/dist/core/handlebars/helpers/eq/eq.d.ts +14 -0
  24. package/dist/core/handlebars/helpers/format-import/format-import.cjs +41 -0
  25. package/dist/core/handlebars/helpers/format-import/format-import.d.ts +13 -0
  26. package/dist/core/handlebars/helpers/format-import-item/format-import-item.cjs +40 -0
  27. package/dist/core/handlebars/helpers/format-import-item/format-import-item.d.ts +13 -0
  28. package/dist/core/handlebars/helpers/format-js-type/format-js-type.cjs +41 -0
  29. package/dist/core/handlebars/helpers/format-js-type/format-js-type.d.ts +14 -0
  30. package/dist/core/handlebars/helpers/gt/gt.cjs +19 -0
  31. package/dist/core/handlebars/helpers/gt/gt.d.ts +14 -0
  32. package/dist/core/handlebars/helpers/gte/gte.cjs +19 -0
  33. package/dist/core/handlebars/helpers/gte/gte.d.ts +14 -0
  34. package/dist/core/handlebars/helpers/helper-base.cjs +19 -0
  35. package/dist/core/handlebars/helpers/helper-base.d.ts +22 -0
  36. package/dist/core/handlebars/helpers/includes/includes.cjs +18 -0
  37. package/dist/core/handlebars/helpers/includes/includes.d.ts +13 -0
  38. package/dist/core/handlebars/helpers/index.cjs +52 -0
  39. package/dist/core/handlebars/helpers/index.d.ts +6 -0
  40. package/dist/core/handlebars/helpers/json/json.cjs +16 -0
  41. package/dist/core/handlebars/helpers/json/json.d.ts +13 -0
  42. package/dist/core/handlebars/helpers/lower-case/lower-case.cjs +19 -0
  43. package/dist/core/handlebars/helpers/lower-case/lower-case.d.ts +13 -0
  44. package/dist/core/handlebars/helpers/lt/lt.cjs +19 -0
  45. package/dist/core/handlebars/helpers/lt/lt.d.ts +14 -0
  46. package/dist/core/handlebars/helpers/lte/lte.cjs +19 -0
  47. package/dist/core/handlebars/helpers/lte/lte.d.ts +14 -0
  48. package/dist/core/handlebars/helpers/neq/neq.cjs +19 -0
  49. package/dist/core/handlebars/helpers/neq/neq.d.ts +14 -0
  50. package/dist/core/handlebars/helpers/not/not.cjs +21 -0
  51. package/dist/core/handlebars/helpers/not/not.d.ts +14 -0
  52. package/dist/core/handlebars/helpers/not-includes/not-includes.cjs +18 -0
  53. package/dist/core/handlebars/helpers/not-includes/not-includes.d.ts +13 -0
  54. package/dist/core/handlebars/helpers/or/or.cjs +23 -0
  55. package/dist/core/handlebars/helpers/or/or.d.ts +13 -0
  56. package/dist/core/handlebars/helpers/pascal-case/pascal-case.cjs +20 -0
  57. package/dist/core/handlebars/helpers/pascal-case/pascal-case.d.ts +13 -0
  58. package/dist/core/handlebars/helpers/snake-case/snake-case.cjs +20 -0
  59. package/dist/core/handlebars/helpers/snake-case/snake-case.d.ts +13 -0
  60. package/dist/core/handlebars/helpers/spinal-case/spinal-case.cjs +20 -0
  61. package/dist/core/handlebars/helpers/spinal-case/spinal-case.d.ts +13 -0
  62. package/dist/core/handlebars/helpers/upper-case/upper-case.cjs +19 -0
  63. package/dist/core/handlebars/helpers/upper-case/upper-case.d.ts +13 -0
  64. package/dist/core/handlebars/index.cjs +27 -0
  65. package/dist/core/handlebars/index.d.ts +2 -0
  66. package/dist/core/handlebars/utils/helper/helper.cjs +41 -0
  67. package/dist/core/handlebars/utils/helper/helper.d.ts +33 -0
  68. package/dist/core/handlebars/utils/index.cjs +20 -0
  69. package/dist/core/handlebars/utils/index.d.ts +17 -0
  70. package/dist/core/index.cjs +27 -0
  71. package/dist/core/index.d.ts +2 -0
  72. package/dist/core/interface/index.cjs +1 -0
  73. package/dist/core/interface/index.d.ts +1 -0
  74. package/dist/index.cjs +1 -1
  75. package/es/actions/gen-api/api-manage.d.ts +36 -0
  76. package/es/actions/gen-api/api-manage.mjs +38 -0
  77. package/es/actions/gen-api/gen-api.d.ts +58 -0
  78. package/es/actions/gen-api/gen-api.mjs +164 -0
  79. package/es/actions/gen-api/model-manage.d.ts +124 -0
  80. package/es/actions/gen-api/model-manage.mjs +126 -0
  81. package/es/actions/index.d.ts +1 -0
  82. package/es/actions/index.mjs +1 -0
  83. package/es/commands/gen-api/gen-api.d.ts +44 -0
  84. package/es/commands/gen-api/gen-api.mjs +15 -0
  85. package/es/commands/index.mjs +2 -0
  86. package/es/core/handlebars/constant/index.d.ts +2 -0
  87. package/es/core/handlebars/constant/index.mjs +2 -0
  88. package/es/core/handlebars/handlebars-engine.d.ts +74 -0
  89. package/es/core/handlebars/handlebars-engine.mjs +122 -0
  90. package/es/core/handlebars/helpers/and/and.d.ts +13 -0
  91. package/es/core/handlebars/helpers/and/and.mjs +14 -0
  92. package/es/core/handlebars/helpers/camel-case/camel-case.d.ts +13 -0
  93. package/es/core/handlebars/helpers/camel-case/camel-case.mjs +13 -0
  94. package/es/core/handlebars/helpers/concat/concat.d.ts +13 -0
  95. package/es/core/handlebars/helpers/concat/concat.mjs +10 -0
  96. package/es/core/handlebars/helpers/eq/eq.d.ts +14 -0
  97. package/es/core/handlebars/helpers/eq/eq.mjs +12 -0
  98. package/es/core/handlebars/helpers/format-import/format-import.d.ts +13 -0
  99. package/es/core/handlebars/helpers/format-import/format-import.mjs +34 -0
  100. package/es/core/handlebars/helpers/format-import-item/format-import-item.d.ts +13 -0
  101. package/es/core/handlebars/helpers/format-import-item/format-import-item.mjs +33 -0
  102. package/es/core/handlebars/helpers/format-js-type/format-js-type.d.ts +14 -0
  103. package/es/core/handlebars/helpers/format-js-type/format-js-type.mjs +34 -0
  104. package/es/core/handlebars/helpers/gt/gt.d.ts +14 -0
  105. package/es/core/handlebars/helpers/gt/gt.mjs +12 -0
  106. package/es/core/handlebars/helpers/gte/gte.d.ts +14 -0
  107. package/es/core/handlebars/helpers/gte/gte.mjs +12 -0
  108. package/es/core/handlebars/helpers/helper-base.d.ts +22 -0
  109. package/es/core/handlebars/helpers/helper-base.mjs +11 -0
  110. package/es/core/handlebars/helpers/includes/includes.d.ts +13 -0
  111. package/es/core/handlebars/helpers/includes/includes.mjs +11 -0
  112. package/es/core/handlebars/helpers/index.d.ts +6 -0
  113. package/es/core/handlebars/helpers/index.mjs +46 -0
  114. package/es/core/handlebars/helpers/json/json.d.ts +13 -0
  115. package/es/core/handlebars/helpers/json/json.mjs +13 -0
  116. package/es/core/handlebars/helpers/lower-case/lower-case.d.ts +13 -0
  117. package/es/core/handlebars/helpers/lower-case/lower-case.mjs +12 -0
  118. package/es/core/handlebars/helpers/lt/lt.d.ts +14 -0
  119. package/es/core/handlebars/helpers/lt/lt.mjs +12 -0
  120. package/es/core/handlebars/helpers/lte/lte.d.ts +14 -0
  121. package/es/core/handlebars/helpers/lte/lte.mjs +12 -0
  122. package/es/core/handlebars/helpers/neq/neq.d.ts +14 -0
  123. package/es/core/handlebars/helpers/neq/neq.mjs +12 -0
  124. package/es/core/handlebars/helpers/not/not.d.ts +14 -0
  125. package/es/core/handlebars/helpers/not/not.mjs +12 -0
  126. package/es/core/handlebars/helpers/not-includes/not-includes.d.ts +13 -0
  127. package/es/core/handlebars/helpers/not-includes/not-includes.mjs +11 -0
  128. package/es/core/handlebars/helpers/or/or.d.ts +13 -0
  129. package/es/core/handlebars/helpers/or/or.mjs +16 -0
  130. package/es/core/handlebars/helpers/pascal-case/pascal-case.d.ts +13 -0
  131. package/es/core/handlebars/helpers/pascal-case/pascal-case.mjs +13 -0
  132. package/es/core/handlebars/helpers/snake-case/snake-case.d.ts +13 -0
  133. package/es/core/handlebars/helpers/snake-case/snake-case.mjs +13 -0
  134. package/es/core/handlebars/helpers/spinal-case/spinal-case.d.ts +13 -0
  135. package/es/core/handlebars/helpers/spinal-case/spinal-case.mjs +13 -0
  136. package/es/core/handlebars/helpers/upper-case/upper-case.d.ts +13 -0
  137. package/es/core/handlebars/helpers/upper-case/upper-case.mjs +12 -0
  138. package/es/core/handlebars/index.d.ts +2 -0
  139. package/es/core/handlebars/index.mjs +2 -0
  140. package/es/core/handlebars/utils/helper/helper.d.ts +33 -0
  141. package/es/core/handlebars/utils/helper/helper.mjs +54 -0
  142. package/es/core/handlebars/utils/index.d.ts +17 -0
  143. package/es/core/handlebars/utils/index.mjs +7 -0
  144. package/es/core/index.d.ts +2 -0
  145. package/es/core/index.mjs +2 -0
  146. package/es/core/interface/index.d.ts +1 -0
  147. package/es/core/interface/index.mjs +0 -0
  148. package/es/index.mjs +1 -1
  149. package/package.json +10 -7
@@ -0,0 +1,2 @@
1
+ export * from './handlebars';
2
+ export * from './interface';
@@ -0,0 +1 @@
1
+ "use strict";
@@ -0,0 +1 @@
1
+ export type IObject = Record<string | symbol, any>;
package/dist/index.cjs CHANGED
@@ -11,7 +11,7 @@ var _commander = require("commander");
11
11
  var _commands = require("./commands/index.cjs");
12
12
  var _utils = require("./utils/index.cjs");
13
13
  var _package = _interopRequireDefault(require("../package.json"));
14
- var _core = require("./core");
14
+ var _core = require("./core/index.cjs");
15
15
  Object.keys(_core).forEach(function (key) {
16
16
  if (key === "default" || key === "__esModule") return;
17
17
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
@@ -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,38 @@
1
+ export class EntityApiManage {
2
+ constructor(name) {
3
+ this.name = name;
4
+ }
5
+ cache = /* @__PURE__ */ new Map();
6
+ /**
7
+ * 新增方法
8
+ *
9
+ * @param {string} method 方法名称
10
+ * @param {string} mode 请求方式
11
+ * @param {IObject} cfg 从 swagger 获取的配置
12
+ */
13
+ add(method, mode, cfg) {
14
+ const tag = `${method}:${mode}`;
15
+ this.cache.set(tag, cfg);
16
+ }
17
+ }
18
+ export class ApiManage {
19
+ cache = /* @__PURE__ */ new Map();
20
+ /**
21
+ * 新增 api
22
+ *
23
+ * @param {string} pathStr 请求路径
24
+ * @param {IObject} cfg swagger 配置
25
+ */
26
+ add(pathStr, cfg) {
27
+ const entityName = pathStr.split("/")[3];
28
+ const method = pathStr.split("/")[4];
29
+ if (!this.cache.has(entityName)) {
30
+ this.cache.set(entityName, new EntityApiManage(entityName));
31
+ }
32
+ const entity = this.cache.get(entityName);
33
+ const modes = Object.keys(cfg);
34
+ modes.forEach((mode) => {
35
+ entity.add(method, mode, cfg[mode]);
36
+ });
37
+ }
38
+ }
@@ -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,164 @@
1
+ import path from "path";
2
+ import fse from "fs-extra";
3
+ import * as rm from "rimraf";
4
+ import glob from "fast-glob";
5
+ import log from "consola";
6
+ import got from "got";
7
+ import { HandlebarsEngine, MACRO_FOLDER } from "../../core/index.mjs";
8
+ import { ModelManage } from "./model-manage.mjs";
9
+ export class GenApiAction {
10
+ // private baseUrl = 'http://paas.paasdev.gct-paas.com';
11
+ engine = new HandlebarsEngine();
12
+ modelManage = new ModelManage();
13
+ configs = {
14
+ platform: "/gct-platform/v2/api-docs?group=%E7%AE%A1%E7%90%86%E5%B9%B3%E5%8F%B0API",
15
+ apaas: "/gct-apaas/v2/api-docs?group=apaas%E5%B9%B3%E5%8F%B0API"
16
+ };
17
+ /**
18
+ * 生成 api
19
+ *
20
+ * @param {string} baseUrl swagger 服务地址
21
+ * @param {string} tag 要发布的 api 标识
22
+ * @param {string} template 模板目录
23
+ * @param {string} output 输出目录
24
+ * @param {boolean} clean 是否清理输出目录后再生成
25
+ */
26
+ async run(baseUrl, tag, template, output, clean) {
27
+ const cwd = process.cwd();
28
+ const tempPath = path.resolve(cwd, template);
29
+ const outPath = path.resolve(cwd, output);
30
+ if (clean === true && fse.existsSync(outPath)) {
31
+ rm.sync(outPath);
32
+ }
33
+ await this.registerMacros(tempPath, this.engine);
34
+ await this.registerTempFiles(tempPath, this.engine);
35
+ await this.write(
36
+ tag,
37
+ path.join(baseUrl, this.configs[tag]),
38
+ outPath,
39
+ this.engine
40
+ );
41
+ }
42
+ /**
43
+ * 基于模型编译输出文件
44
+ *
45
+ * @protected
46
+ * @param {string} url
47
+ * @param {string} outPath
48
+ * @param {HandlebarsEngine} engine
49
+ * @returns {*} {Promise<void>}
50
+ */
51
+ async write(tag, url, outPath, engine) {
52
+ const cfg = await got(url).json();
53
+ const model = this.transformModel(cfg);
54
+ this.modelManage.init(tag, model);
55
+ const all = [];
56
+ engine.temps.forEach((template, key) => {
57
+ if (key === "") {
58
+ return;
59
+ }
60
+ const outFilePath = path.join(outPath, key);
61
+ const tags = engine.parseContextTags(key);
62
+ if (tags.length === 0) {
63
+ log.info(`\u751F\u6210\u6587\u4EF6\uFF1A${outFilePath}`);
64
+ all.push(
65
+ fse.outputFile(
66
+ outFilePath,
67
+ engine.render(template, {
68
+ entities: Array.from(this.modelManage.entities.values()),
69
+ definitions: model.definitions
70
+ }),
71
+ "utf-8"
72
+ )
73
+ );
74
+ } else {
75
+ if (tags.includes("api")) {
76
+ this.modelManage.entities.forEach((model2) => {
77
+ const filePath = engine.render(outFilePath, model2);
78
+ log.info(`\u751F\u6210\u6587\u4EF6\uFF1A${filePath}`);
79
+ all.push(
80
+ fse.outputFile(filePath, engine.render(template, model2), "utf-8")
81
+ );
82
+ });
83
+ }
84
+ }
85
+ });
86
+ await Promise.all(all);
87
+ }
88
+ /**
89
+ * 注册所有的宏
90
+ *
91
+ * @protected
92
+ * @param {string} tempPath
93
+ * @param {HandlebarsEngine} engine
94
+ * @returns {*} {Promise<void>}
95
+ */
96
+ async registerMacros(tempPath, engine) {
97
+ const macroFiles = glob.sync(path.join(MACRO_FOLDER, "**/*.hbs"), {
98
+ cwd: tempPath,
99
+ dot: true,
100
+ absolute: true
101
+ });
102
+ if (macroFiles.length > 0) {
103
+ await Promise.all(
104
+ macroFiles.map((macroFilePath) => {
105
+ return new Promise((resolve) => {
106
+ fse.readFile(macroFilePath, "utf-8").then((file) => {
107
+ const key = path.relative(tempPath, macroFilePath);
108
+ log.info(`\u6CE8\u518C\u5B8F\uFF1A${key}`);
109
+ engine.registerMacro(key, file);
110
+ resolve();
111
+ });
112
+ });
113
+ })
114
+ );
115
+ }
116
+ }
117
+ /**
118
+ * 注册所有模板文件
119
+ *
120
+ * @protected
121
+ * @param {string} tempPath
122
+ * @param {HandlebarsEngine} engine
123
+ * @returns {*} {Promise<void>}
124
+ */
125
+ async registerTempFiles(tempPath, engine) {
126
+ const tempFiles = glob.sync("**/*.hbs", {
127
+ cwd: tempPath,
128
+ dot: true,
129
+ absolute: true,
130
+ ignore: [path.join(MACRO_FOLDER, "**/*.hbs")]
131
+ });
132
+ if (tempFiles.length > 0) {
133
+ await Promise.all(
134
+ tempFiles.map((tempFilePath) => {
135
+ return new Promise((resolve) => {
136
+ fse.readFile(tempFilePath, "utf-8").then((file) => {
137
+ const key = path.relative(tempPath, tempFilePath);
138
+ log.info(`\u6CE8\u518C\u6A21\u677F\uFF1A${key}`);
139
+ engine.registerTemp(key, file);
140
+ resolve();
141
+ });
142
+ });
143
+ })
144
+ );
145
+ }
146
+ }
147
+ /**
148
+ * 针对加载的配置进行处理
149
+ *
150
+ * @protected
151
+ * @param {IObject} model
152
+ * @returns {*} {IObject}
153
+ */
154
+ transformModel(model) {
155
+ const keys = Object.keys(model.definitions);
156
+ keys.forEach((key) => {
157
+ const item = model.definitions[key];
158
+ if (item.properties) {
159
+ item.propertySize = Object.keys(item.properties).length;
160
+ }
161
+ });
162
+ return model;
163
+ }
164
+ }
@@ -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,126 @@
1
+ import { ApiManage } from "./api-manage.mjs";
2
+ export class ModelManage {
3
+ /**
4
+ * 所有实体模型
5
+ *
6
+ * @type {Map<string, IEntityModel>}
7
+ */
8
+ entities = /* @__PURE__ */ new Map();
9
+ /**
10
+ * 所有用到的实体定义
11
+ *
12
+ * @type {Map<string, IObject>}
13
+ */
14
+ definitions = /* @__PURE__ */ new Map();
15
+ /**
16
+ * 目前接口中的基础数据类型
17
+ *
18
+ * @protected
19
+ */
20
+ basicDataType = [
21
+ "any",
22
+ "number",
23
+ "string",
24
+ "null",
25
+ "boolean",
26
+ "Void",
27
+ "object"
28
+ ];
29
+ init(tag, cfg) {
30
+ const apiManage = new ApiManage();
31
+ const paths = Object.keys(cfg.paths).filter((pathStr) => {
32
+ return !pathStr.startsWith("/external") || pathStr === "/external/api/tenant/info/byPortOrDomain";
33
+ });
34
+ paths.forEach((pathStr) => {
35
+ const pathCfg = cfg.paths[pathStr];
36
+ apiManage.add(pathStr, pathCfg);
37
+ });
38
+ const entityNames = apiManage.cache.keys();
39
+ for (const entityName of entityNames) {
40
+ const entitySet = /* @__PURE__ */ new Set();
41
+ const entityManage = apiManage.cache.get(entityName);
42
+ const methods = [];
43
+ const tags = entityManage.cache.keys();
44
+ for (const tag2 of tags) {
45
+ const [methodName, mode] = tag2.split(":");
46
+ const config = entityManage.cache.get(tag2);
47
+ let requestEntity = void 0;
48
+ const param = config.parameters.find((param2) => {
49
+ if (param2.in === "body") {
50
+ return param2;
51
+ }
52
+ });
53
+ if (param && param.schema) {
54
+ const refKey = this.getOriginRef(param.schema);
55
+ requestEntity = cfg.definitions[refKey];
56
+ if (requestEntity && Object.keys(requestEntity).length > 0) {
57
+ this.definitions.set(refKey, requestEntity);
58
+ entitySet.add(refKey);
59
+ }
60
+ }
61
+ let responseEntity = void 0;
62
+ if (config.responses["200"].schema) {
63
+ const refKey = this.getOriginRef(config.responses["200"].schema);
64
+ responseEntity = cfg.definitions[refKey];
65
+ if (responseEntity && Object.keys(responseEntity).length > 0) {
66
+ this.definitions.set(refKey, responseEntity);
67
+ entitySet.add(refKey);
68
+ }
69
+ }
70
+ const queries = config.parameters.filter(
71
+ (param2) => {
72
+ if (param2.in === "query") {
73
+ return param2;
74
+ }
75
+ }
76
+ );
77
+ if (methodName === "{id}") {
78
+ queries.push({
79
+ name: "id",
80
+ in: "query",
81
+ description: "\u6570\u636E\u4E3B\u952E",
82
+ required: true,
83
+ type: "string"
84
+ });
85
+ }
86
+ methods.push({
87
+ name: methodName !== "undefined" ? methodName : "",
88
+ mode,
89
+ model: config,
90
+ requestEntity,
91
+ responseEntity,
92
+ queries
93
+ });
94
+ }
95
+ this.entities.set(entityName, {
96
+ tag,
97
+ entityName,
98
+ api: entityName,
99
+ model: cfg.definitions[entityName],
100
+ methods,
101
+ entityRefs: Array.from(entitySet.values()).filter((item) => {
102
+ return this.basicDataType.findIndex((type) => {
103
+ return item.indexOf(`\xAB${type}\xBB`) !== -1;
104
+ }) === -1 && !this.basicDataType.includes(item);
105
+ })
106
+ });
107
+ }
108
+ }
109
+ /**
110
+ * 获取接口的引用对象
111
+ *
112
+ * @protected
113
+ * @param {IObject} data
114
+ * @returns {*} {string}
115
+ */
116
+ getOriginRef(data) {
117
+ const { $ref, originalRef, type } = 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
+ }
@@ -0,0 +1 @@
1
+ export { GenApiAction } from './gen-api/gen-api';
@@ -0,0 +1 @@
1
+ export { GenApiAction } from "./gen-api/gen-api.mjs";
@@ -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,15 @@
1
+ import { GenApiAction } from "../../actions/index.mjs";
2
+ export class GenApiCommand {
3
+ load(program) {
4
+ 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));
5
+ }
6
+ async action(args) {
7
+ await new GenApiAction().run(
8
+ args.url,
9
+ args.tag,
10
+ args.template,
11
+ args.output,
12
+ args.clean
13
+ );
14
+ }
15
+ }
@@ -1,6 +1,8 @@
1
1
  import { consola } from "consola";
2
+ import { GenApiCommand } from "./gen-api/gen-api.mjs";
2
3
  export class CommandLoader {
3
4
  static load(program) {
5
+ new GenApiCommand().load(program);
4
6
  this.handleInvalidCommand(program);
5
7
  }
6
8
  static handleInvalidCommand(program) {
@@ -0,0 +1,2 @@
1
+ export declare const TEMP_EXTENSION = ".hbs";
2
+ export declare const MACRO_FOLDER = "@macro";
@@ -0,0 +1,2 @@
1
+ export const TEMP_EXTENSION = ".hbs";
2
+ export const MACRO_FOLDER = "@macro";