@gct-paas/cli 0.0.1-dev.8 → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/actions/gen-api/api-manage.cjs +46 -0
- package/dist/actions/gen-api/api-manage.d.ts +36 -0
- package/dist/actions/gen-api/gen-api.cjs +157 -0
- package/dist/actions/gen-api/gen-api.d.ts +58 -0
- package/dist/actions/gen-api/model-manage.cjs +127 -0
- package/dist/actions/gen-api/model-manage.d.ts +124 -0
- package/dist/actions/index.cjs +12 -0
- package/dist/actions/index.d.ts +1 -0
- package/dist/commands/gen-api/gen-api.cjs +16 -0
- package/dist/commands/gen-api/gen-api.d.ts +44 -0
- package/dist/commands/index.cjs +2 -0
- package/dist/core/handlebars/constant/index.cjs +8 -0
- package/dist/core/handlebars/constant/index.d.ts +2 -0
- package/dist/core/handlebars/handlebars-engine.cjs +134 -0
- package/dist/core/handlebars/handlebars-engine.d.ts +74 -0
- package/dist/core/handlebars/helpers/and/and.cjs +21 -0
- package/dist/core/handlebars/helpers/and/and.d.ts +13 -0
- package/dist/core/handlebars/helpers/camel-case/camel-case.cjs +20 -0
- package/dist/core/handlebars/helpers/camel-case/camel-case.d.ts +13 -0
- package/dist/core/handlebars/helpers/concat/concat.cjs +17 -0
- package/dist/core/handlebars/helpers/concat/concat.d.ts +13 -0
- package/dist/core/handlebars/helpers/eq/eq.cjs +19 -0
- package/dist/core/handlebars/helpers/eq/eq.d.ts +14 -0
- package/dist/core/handlebars/helpers/format-import/format-import.cjs +41 -0
- package/dist/core/handlebars/helpers/format-import/format-import.d.ts +13 -0
- package/dist/core/handlebars/helpers/format-import-item/format-import-item.cjs +40 -0
- package/dist/core/handlebars/helpers/format-import-item/format-import-item.d.ts +13 -0
- package/dist/core/handlebars/helpers/format-js-type/format-js-type.cjs +41 -0
- package/dist/core/handlebars/helpers/format-js-type/format-js-type.d.ts +14 -0
- package/dist/core/handlebars/helpers/gt/gt.cjs +19 -0
- package/dist/core/handlebars/helpers/gt/gt.d.ts +14 -0
- package/dist/core/handlebars/helpers/gte/gte.cjs +19 -0
- package/dist/core/handlebars/helpers/gte/gte.d.ts +14 -0
- package/dist/core/handlebars/helpers/helper-base.cjs +19 -0
- package/dist/core/handlebars/helpers/helper-base.d.ts +22 -0
- package/dist/core/handlebars/helpers/includes/includes.cjs +18 -0
- package/dist/core/handlebars/helpers/includes/includes.d.ts +13 -0
- package/dist/core/handlebars/helpers/index.cjs +52 -0
- package/dist/core/handlebars/helpers/index.d.ts +6 -0
- package/dist/core/handlebars/helpers/json/json.cjs +16 -0
- package/dist/core/handlebars/helpers/json/json.d.ts +13 -0
- package/dist/core/handlebars/helpers/lower-case/lower-case.cjs +19 -0
- package/dist/core/handlebars/helpers/lower-case/lower-case.d.ts +13 -0
- package/dist/core/handlebars/helpers/lt/lt.cjs +19 -0
- package/dist/core/handlebars/helpers/lt/lt.d.ts +14 -0
- package/dist/core/handlebars/helpers/lte/lte.cjs +19 -0
- package/dist/core/handlebars/helpers/lte/lte.d.ts +14 -0
- package/dist/core/handlebars/helpers/neq/neq.cjs +19 -0
- package/dist/core/handlebars/helpers/neq/neq.d.ts +14 -0
- package/dist/core/handlebars/helpers/not/not.cjs +21 -0
- package/dist/core/handlebars/helpers/not/not.d.ts +14 -0
- package/dist/core/handlebars/helpers/not-includes/not-includes.cjs +18 -0
- package/dist/core/handlebars/helpers/not-includes/not-includes.d.ts +13 -0
- package/dist/core/handlebars/helpers/or/or.cjs +23 -0
- package/dist/core/handlebars/helpers/or/or.d.ts +13 -0
- package/dist/core/handlebars/helpers/pascal-case/pascal-case.cjs +20 -0
- package/dist/core/handlebars/helpers/pascal-case/pascal-case.d.ts +13 -0
- package/dist/core/handlebars/helpers/snake-case/snake-case.cjs +20 -0
- package/dist/core/handlebars/helpers/snake-case/snake-case.d.ts +13 -0
- package/dist/core/handlebars/helpers/spinal-case/spinal-case.cjs +20 -0
- package/dist/core/handlebars/helpers/spinal-case/spinal-case.d.ts +13 -0
- package/dist/core/handlebars/helpers/upper-case/upper-case.cjs +19 -0
- package/dist/core/handlebars/helpers/upper-case/upper-case.d.ts +13 -0
- package/dist/core/handlebars/index.cjs +27 -0
- package/dist/core/handlebars/index.d.ts +2 -0
- package/dist/core/handlebars/utils/helper/helper.cjs +41 -0
- package/dist/core/handlebars/utils/helper/helper.d.ts +33 -0
- package/dist/core/handlebars/utils/index.cjs +20 -0
- package/dist/core/handlebars/utils/index.d.ts +17 -0
- package/dist/core/index.cjs +27 -0
- package/dist/core/index.d.ts +2 -0
- package/dist/core/interface/index.cjs +1 -0
- package/dist/core/interface/index.d.ts +1 -0
- package/dist/index.cjs +1 -1
- package/es/actions/gen-api/api-manage.d.ts +36 -0
- package/es/actions/gen-api/api-manage.mjs +38 -0
- package/es/actions/gen-api/gen-api.d.ts +58 -0
- package/es/actions/gen-api/gen-api.mjs +164 -0
- package/es/actions/gen-api/model-manage.d.ts +124 -0
- package/es/actions/gen-api/model-manage.mjs +126 -0
- package/es/actions/index.d.ts +1 -0
- package/es/actions/index.mjs +1 -0
- package/es/commands/gen-api/gen-api.d.ts +44 -0
- package/es/commands/gen-api/gen-api.mjs +15 -0
- package/es/commands/index.mjs +2 -0
- package/es/core/handlebars/constant/index.d.ts +2 -0
- package/es/core/handlebars/constant/index.mjs +2 -0
- package/es/core/handlebars/handlebars-engine.d.ts +74 -0
- package/es/core/handlebars/handlebars-engine.mjs +122 -0
- package/es/core/handlebars/helpers/and/and.d.ts +13 -0
- package/es/core/handlebars/helpers/and/and.mjs +14 -0
- package/es/core/handlebars/helpers/camel-case/camel-case.d.ts +13 -0
- package/es/core/handlebars/helpers/camel-case/camel-case.mjs +13 -0
- package/es/core/handlebars/helpers/concat/concat.d.ts +13 -0
- package/es/core/handlebars/helpers/concat/concat.mjs +10 -0
- package/es/core/handlebars/helpers/eq/eq.d.ts +14 -0
- package/es/core/handlebars/helpers/eq/eq.mjs +12 -0
- package/es/core/handlebars/helpers/format-import/format-import.d.ts +13 -0
- package/es/core/handlebars/helpers/format-import/format-import.mjs +34 -0
- package/es/core/handlebars/helpers/format-import-item/format-import-item.d.ts +13 -0
- package/es/core/handlebars/helpers/format-import-item/format-import-item.mjs +33 -0
- package/es/core/handlebars/helpers/format-js-type/format-js-type.d.ts +14 -0
- package/es/core/handlebars/helpers/format-js-type/format-js-type.mjs +34 -0
- package/es/core/handlebars/helpers/gt/gt.d.ts +14 -0
- package/es/core/handlebars/helpers/gt/gt.mjs +12 -0
- package/es/core/handlebars/helpers/gte/gte.d.ts +14 -0
- package/es/core/handlebars/helpers/gte/gte.mjs +12 -0
- package/es/core/handlebars/helpers/helper-base.d.ts +22 -0
- package/es/core/handlebars/helpers/helper-base.mjs +11 -0
- package/es/core/handlebars/helpers/includes/includes.d.ts +13 -0
- package/es/core/handlebars/helpers/includes/includes.mjs +11 -0
- package/es/core/handlebars/helpers/index.d.ts +6 -0
- package/es/core/handlebars/helpers/index.mjs +46 -0
- package/es/core/handlebars/helpers/json/json.d.ts +13 -0
- package/es/core/handlebars/helpers/json/json.mjs +13 -0
- package/es/core/handlebars/helpers/lower-case/lower-case.d.ts +13 -0
- package/es/core/handlebars/helpers/lower-case/lower-case.mjs +12 -0
- package/es/core/handlebars/helpers/lt/lt.d.ts +14 -0
- package/es/core/handlebars/helpers/lt/lt.mjs +12 -0
- package/es/core/handlebars/helpers/lte/lte.d.ts +14 -0
- package/es/core/handlebars/helpers/lte/lte.mjs +12 -0
- package/es/core/handlebars/helpers/neq/neq.d.ts +14 -0
- package/es/core/handlebars/helpers/neq/neq.mjs +12 -0
- package/es/core/handlebars/helpers/not/not.d.ts +14 -0
- package/es/core/handlebars/helpers/not/not.mjs +12 -0
- package/es/core/handlebars/helpers/not-includes/not-includes.d.ts +13 -0
- package/es/core/handlebars/helpers/not-includes/not-includes.mjs +11 -0
- package/es/core/handlebars/helpers/or/or.d.ts +13 -0
- package/es/core/handlebars/helpers/or/or.mjs +16 -0
- package/es/core/handlebars/helpers/pascal-case/pascal-case.d.ts +13 -0
- package/es/core/handlebars/helpers/pascal-case/pascal-case.mjs +13 -0
- package/es/core/handlebars/helpers/snake-case/snake-case.d.ts +13 -0
- package/es/core/handlebars/helpers/snake-case/snake-case.mjs +13 -0
- package/es/core/handlebars/helpers/spinal-case/spinal-case.d.ts +13 -0
- package/es/core/handlebars/helpers/spinal-case/spinal-case.mjs +13 -0
- package/es/core/handlebars/helpers/upper-case/upper-case.d.ts +13 -0
- package/es/core/handlebars/helpers/upper-case/upper-case.mjs +12 -0
- package/es/core/handlebars/index.d.ts +2 -0
- package/es/core/handlebars/index.mjs +2 -0
- package/es/core/handlebars/utils/helper/helper.d.ts +33 -0
- package/es/core/handlebars/utils/helper/helper.mjs +54 -0
- package/es/core/handlebars/utils/index.d.ts +17 -0
- package/es/core/handlebars/utils/index.mjs +7 -0
- package/es/core/index.d.ts +2 -0
- package/es/core/index.mjs +2 -0
- package/es/core/interface/index.d.ts +1 -0
- package/es/core/interface/index.mjs +0 -0
- package/es/index.mjs +1 -1
- package/package.json +11 -7
|
@@ -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';
|
|
@@ -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
|
+
}
|
package/dist/commands/index.cjs
CHANGED
|
@@ -5,8 +5,10 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.CommandLoader = void 0;
|
|
7
7
|
var _consola = require("consola");
|
|
8
|
+
var _genApi = require("./gen-api/gen-api.cjs");
|
|
8
9
|
class CommandLoader {
|
|
9
10
|
static load(program) {
|
|
11
|
+
new _genApi.GenApiCommand().load(program);
|
|
10
12
|
this.handleInvalidCommand(program);
|
|
11
13
|
}
|
|
12
14
|
static handleInvalidCommand(program) {
|