forked from lgwebdream/fe-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi2code.js
More file actions
81 lines (75 loc) · 2.14 KB
/
Copy pathapi2code.js
File metadata and controls
81 lines (75 loc) · 2.14 KB
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
const inquirer = require('inquirer');
const generateInterface = require('../lib/api2code/generateInterface');
const generateCRUD = require('../lib/api2code/generateCRUD');
const loadConfig = require('../lib/loadConfig');
const { removeEmpty, getCwdPath } = require('../lib/utils');
const config = loadConfig();
const handleTargetMap = {
interface: generateInterface,
CRUD: generateCRUD,
};
// main questions
const promptList = [
{
type: 'list',
name: 'target',
message: 'Please select the type of generation',
choices: Object.keys(handleTargetMap),
},
{
type: 'confirm',
name: 'skip',
message: 'skip all the files generated before ? ',
},
{
type: 'list',
name: 'language',
message: 'Please select the coding language you used',
choices: ['JavaScript', 'Typescript'],
when: ({ target }) => target === 'CRUD',
},
{
type: 'list',
name: 'requestLib',
message: 'Please select the request library you used',
choices: ['axios', 'jQuery', 'fetch' /** graphQL */],
when: ({ target }) => target === 'CRUD',
},
{
type: 'list',
name: 'codeStyle',
message: 'Please select the style for code',
choices: ['code-snippets', 'service'],
when: ({ target }) => target === 'CRUD',
},
];
// main program
const api2code = program => {
program
.command('api2code')
.alias('a2c')
.description('🌽 api translation typescript')
.option('-u, --url <url>', 'api addres(domain or ip)', config.request.url)
.option('-p, --path <path>', 'api path')
.option(
'-b, --body <body>',
'data json path for http body, only post method.',
)
.option('-i, --input <input>', 'input json file')
.action(options => {
const { url, output, path, body, input } = options;
inquirer.prompt(promptList).then(({ target, ...props }) => {
handleTargetMap[target](
removeEmpty({
url,
path,
output,
input: input && resolve(process.cwd(), input),
body: body && resolve(process.cwd(), body),
...props
}),
);
});
});
};
module.exports = api2code;