-
Notifications
You must be signed in to change notification settings - Fork 0
/
plopfile.js
389 lines (352 loc) · 9.29 KB
/
plopfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
const fetch = require("node-fetch");
const _ = require("lodash");
const fs = require("fs");
let path = require("path");
const systemUrl = "http://localhost:8081";
let eq = (a, b) => a === b;
let decapitalize = (s) => {
if (!s) {
return s;
}
return s[0].toLowerCase() + s.substr(1);
};
let capitalize = (s) => {
if (!s) {
return s;
}
return s[0].toUpperCase() + s.substr(1);
};
let pathDirname = (s) => {
if (!s) {
return s;
}
return require("path").dirname(s);
};
let pathBaseName = (s) => {
if (!s) {
return s;
}
return require("path").basename(s);
};
/**
* @typedef {Object} MethodDef
* @property {string} name - name
* @property {string} description - description
*/
/**
* @typedef {Object} HttpDef
* @property {string} url - url
* @property {string} method - method
*/
/**
* @typedef {Object} PropDef
* @property {string} type - type
* @property {string} name - name
* @property {string} description - description
*/
/**
* @typedef {Object} TypeDef
* @property {string} name - name
* @property {string} description - description
* @property {Array<PropDef>} props - prop list
*/
/**
* @typedef {Object} SnippetVO
* @property {TypeDef} paramType - paramType
* @property {TypeDef} resultType - resultType
* @property {MethodDef} method - method
* @property {HttpDef} http - http
*/
/**
* 渲染 hbs
* @param {SnippetVO} snippetVO
* @return {string}
*/
function renderApiSnippets(snippetVO) {
const Handlebars = require("handlebars");
const source = fs
.readFileSync(path.join(__dirname, "templates/api_snippets.js.hbs"))
.toString();
Handlebars.registerHelper("eq", eq);
let template = Handlebars.compile(source);
const result = template(snippetVO);
return result;
}
// 处理泛型在 swagger 中的表达,如:PageDTO«OrgDTO»
const simpleConvertKey = (k) => {
return k.replace(/«([^»]+)»/, "Of$1");
};
function parseSwaggerDefinition({ key, type, description, properties, items }) {
let props = [];
const simpleConvertType = (t) => {
if (t === "integer") {
return "number";
}
return simpleConvertKey(t);
};
// 直接递归构建 string
if (type === "object") {
_.keys(properties).map((pk) => {
const p = properties[pk];
const pProps = parseSwaggerDefinition({
...p,
key: pk,
});
if (pProps.length > 1) {
props.push(pProps);
} else {
props.push(...pProps);
}
});
} else if (type === "array" && items) {
// 这里涉及到一个递归的问题
const refDefinitionKey = getDefinitionKey(items);
if (!refDefinitionKey) {
props.push({
name: simpleConvertKey(key),
type: `Array<${simpleConvertType(items.type)}>`,
description: description || "",
});
} else {
// array<xxObject>
embeddedTypes.add(refDefinitionKey);
props.push({
name: simpleConvertKey(key),
type: `Array<${simpleConvertKey(refDefinitionKey)}>`,
description: description || "",
});
}
} else {
props.push({
name: simpleConvertKey(key),
type: simpleConvertType(type),
description: description || "",
});
}
return props;
}
let getSwaggerJson = (baseUrl) =>
fetch(baseUrl + "/v2/api-docs").then((request) => {
return request.json();
});
let interactiveFindPath = (pathText, swaggerJson) => {
const result = swaggerJson;
// 获取 swagger 定义
const path = pathText;
let paths = result.paths;
paths = _.keys(paths).map((p) => {
return {
key: p,
value: paths[p],
};
});
// console.log(paths);
// console.log(path);
const matchedPaths = paths.filter((p) => p.key.indexOf(path) >= 0);
// 重组
const apis = _.flatten(
matchedPaths.map((p) => {
const k = p.key;
const values = p.value;
const methods = _.keys(values);
return methods.map((m) => {
return {
key: m + " " + k,
value: values[m],
};
});
})
);
if (apis.length === 0) {
return Promise.resolve([result, null]);
}
if (apis.length === 1) {
return Promise.resolve([result, apis[0]]);
}
const inqurier = require("inquirer");
const choices = apis.map((p, i) => {
return { name: p.key + " " + p.value.summary, value: i };
});
return inqurier
.prompt([
{
name: "index",
type: "list",
choices: choices,
message: "匹配到多个 api,选择一个",
loop: false,
},
])
.then((answers) => {
const index = answers.index;
const api = apis[index];
console.log("selected api: ", api.key);
return Promise.resolve([result, api]);
});
};
const embeddedTypes = new Set();
const getMethodDef = (swagger, api) => {
// console.log(api);
const url = api.key.split(" ")[1];
const apiMethod = _.camelCase(_.slice(url.split("/"), -2).join(" "));
const desc = api.value.description || api.value.summary;
return {
name: apiMethod,
description: desc,
};
};
const getHttpDef = (swagger, api) => {
return {
url: api.key.split(" ")[1],
method: api.key.split(" ")[0],
};
};
const getTypeDef = (swagger, definitionKey) => {
const definition = swagger.definitions[definitionKey];
const props = parseSwaggerDefinition({
key: definitionKey,
...definition,
});
return {
name: simpleConvertKey(definitionKey),
description: definition.description,
props: props,
};
};
function getSnippetVO(swagger, api) {
if (!api) {
throw new Error("无法找到符合要求的 api");
}
const methodDef = getMethodDef(swagger, api);
const httpDef = getHttpDef(swagger, api);
const bodyParamKey = getBodyParamKey(api.value.parameters);
const bodyParamType = bodyParamKey && getTypeDef(swagger, bodyParamKey);
// console.log(JSON.stringify(bodyParamType));
const resultParamKey = getOKResultBeanType(api.value.responses);
const resultParamType = getTypeDef(swagger, resultParamKey);
// console.log(resultParamType);
const extraTypes = [];
if (embeddedTypes.size > 0) {
embeddedTypes.forEach((e) => {
extraTypes.push(getTypeDef(swagger, e));
});
}
return {
paramType: bodyParamType,
resultType: resultParamType,
method: methodDef,
http: httpDef,
extraTypes,
};
}
let querySwaggerApiDef = function (data) {
getSwaggerJson(systemUrl)
.then((result) => {
return interactiveFindPath(data.apiName, result);
})
.then(([swagger, api]) => {
if (!api) {
console.log("无法查到相关接口");
return;
}
return Promise.resolve(getSnippetVO(swagger, api));
})
.then((result) => {
let jsdoc = renderApiSnippets(result);
console.log(jsdoc);
const clipboardy = require("clipboardy");
clipboardy.writeSync(jsdoc);
console.log("已复制到粘贴板");
});
};
let getDefinitionKey = (refObject) => {
if (refObject && refObject["$ref"]) {
return getRefDefinitionKey(refObject["$ref"]);
}
return null;
};
let getRefDefinitionKey = (ref) => {
return ref.replace("#/definitions/", "");
};
let getBodyParam = (parameters) => {
return _.find(parameters, (p) => p.in === "body");
};
let getBodyParamKey = (parameters) => {
const bodyParam = getBodyParam(parameters);
if (!bodyParam) {
return null;
}
let paramSchema = bodyParam.schema["$ref"];
if (!paramSchema) {
return null;
}
const definition = paramSchema.replace("#/definitions/", "");
return definition;
};
let getOkResponse = (response) => {
if (response && response["200"]) {
return response["200"];
}
return null;
};
let getResultBeanType = (ref) => {
let schema = ref.replace("#/definitions/", "");
// 处理 ResultBean,这部分会手动配合 jsdoc 处理
if (schema.indexOf("ResultBean«") >= 0 && schema.endsWith("»")) {
schema = schema.slice("ResultBean«".length, schema.length - 1);
}
return schema;
};
let getOKResultBeanType = (response) => {
var okResponse = getOkResponse(response);
if (!okResponse) {
throw new Error("不存在 200 Response 说明,无法处理");
}
let schema = getResultBeanType(okResponse.schema["$ref"]);
return schema;
};
module.exports = function (plop) {
plop.setHelper("decapitalize", decapitalize);
plop.setHelper("capitalize", capitalize);
plop.setHelper("pathDirname", pathDirname);
plop.setHelper("pathBaseName", pathBaseName);
plop.setActionType("querySwaggerApiDef", querySwaggerApiDef);
plop.setGenerator("api", {
description: "打印 axios api 接口(仅支持规范的 POST 请求!)",
prompts: [
{
type: "input",
name: "apiName",
message: "api 路径关键字,如 user, usr/user",
},
],
actions() {
return [
{
type: "querySwaggerApiDef",
},
];
},
});
plop.setGenerator("view", {
description: "生成 vue 页面",
prompts: [
{
type: "input",
name: "viewName",
message:
"页面名(包含路径,如 login/index 生成 views/login/index.vue 页面)",
},
],
actions() {
return [
{
type: "add", //
skipIfExists: true,
path: `src/views/{{pathDirname viewName}}/{{pathBaseName viewName}}.vue`, // 要生成的文件路径
templateFile: "templates/vue_components.vue.hbs", //
},
];
},
});
};