forked from lgwebdream/fe-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
56 lines (50 loc) · 1.63 KB
/
index.js
File metadata and controls
56 lines (50 loc) · 1.63 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
const { resolve, basename } = require('path');
const ora = require('ora');
const chalk = require('chalk');
const request = require('../../utils/request');
const { removeEmpty, path2CamelCase } = require('../../utils');
const output2File = require('../../utils/output2File');
const quicktypeJSON = require('../../utils/quicktypeJSON');
const spinner = ora('transforming...');
const outputInterface = (data, name, output) => {
quicktypeJSON('TypeScript', name, JSON.stringify(data))
.then(({ lines }) => {
output2File(resolve(process.cwd(), output), lines.join('\n')).then(() => {
spinner.stop();
// eslint-disable-next-line no-console
console.log(chalk.green('successfully 🎉 🎉 🎉'));
});
})
.catch(err => console.error(err));
};
const fromRequest = options => {
const babyData = options.body ? require(options.body) : null;
request(
removeEmpty({
method: options.httpMethod,
url: `${options.url}${options.path}`,
data: babyData,
}),
)
.then(({ data }) =>
outputInterface(data, path2CamelCase(options.path), options.output),
)
.catch(err => console.error(err));
};
const fromLocalJson = options => {
const dataJson = require(options.input);
const name = basename(options.input)
.split('.')[0]
.replace(/^(\w)/g, (all, letter) => letter.toUpperCase());
outputInterface(dataJson, name, options.output);
};
const generateInterface = options => {
spinner.start();
// 如果有 -i 则通过本地json生成interface
if (options.input) {
fromLocalJson(options);
return;
}
fromRequest(options);
};
module.exports = generateInterface;