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
65 lines (56 loc) · 1.71 KB
/
api2code.js
File metadata and controls
65 lines (56 loc) · 1.71 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
const { resolve } = require('path');
const inquirer = require('inquirer');
const generateInterface = require('../lib/api2code/generateInterface');
const generateCRUD = require('../lib/api2code/generateCRUD');
const loadConfig = require('../lib/loadConfig');
const { removeEmpty } = require('../lib/utils');
const config = loadConfig();
const handleTargetMap = {
interface: generateInterface,
CRUD: generateCRUD,
};
const promptList = [
{
type: 'list',
name: 'target',
message: 'Please select the type of generation.',
choices: Object.keys(handleTargetMap),
},
];
const chooseHttpMethod = {
type: 'list',
name: 'httpMethod',
message: 'Please choose your HTTP method.',
choices: ['get', 'post'],
};
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')
.requiredOption('-o, --output <output>', 'path of generation file')
.action(options => {
const { url, output, path, body, input } = options;
path && promptList.push(chooseHttpMethod);
inquirer.prompt(promptList).then(({ target, httpMethod }) => {
handleTargetMap[target](
removeEmpty({
url,
path,
output,
httpMethod,
input: input && resolve(process.cwd(), input),
body: body && resolve(process.cwd(), body),
}),
);
});
});
};
module.exports = api2code;