Visitar URL original
feat: add api2code input processing and load config file · dashnowords/fe-code@7a818f8 · GitHub
Skip to content

Commit 7a818f8

Browse files
committed
feat: add api2code input processing and load config file
1 parent 16c2068 commit 7a818f8

9 files changed

Lines changed: 77 additions & 47 deletions

File tree

‎.lintstagedrc.json‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
{
2-
"*.{ts,js}": "eslint --fix"
2+
"*.{ts,js}": [
3+
"eslint --fix"
4+
]
35
}

‎bin/fe‎

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,43 @@
11
#!/usr/bin/env node
22
const { program } = require('commander');
33
const figlet = require('figlet');
4-
const versionStr = figlet.textSync('Fe Code');
54
const Printer = require('@darkobits/lolcatjs');
65
const shell = require('shelljs');
7-
const _version = require('../package.json').version;
86
const chalk = require('chalk');
97
const inquirer = require('inquirer');
108
const ora = require('ora');
119
const download = require('download-git-repo');
10+
const { version } = require('../package.json');
1211
const api2code = require('../scripts/api2code');
1312
const vue2code = require('../scripts/vue2code');
1413
const react2code = require('../scripts/react2code');
1514
const envir2code = require('../scripts/envir2code');
16-
program
17-
.version(
18-
Printer.default.fromString(
19-
` \n 前端代码生成器${_version}\n www.yidengfe.com \n${versionStr}`
20-
)
21-
)
22-
.option('-c, --config <path>', '配置路径', './fecode.cong.js');
15+
const loadConfig = require('../lib/loadConfig');
16+
17+
const versionStr = figlet.textSync('Fe Code');
18+
19+
program.version(
20+
Printer.default.fromString(
21+
` \n 前端代码生成器${version}\n www.yidengfe.com \n${versionStr}`,
22+
),
23+
);
24+
// .option('-c, --config <path>', '配置路径', './fecoderc.json');
2325

24-
//接口生成代码部分
26+
// 接口生成代码部分
2527
api2code(program);
26-
//生成Vue代码部分
28+
// 生成Vue代码部分
2729
vue2code(program);
28-
//生成react代码部分
30+
// 生成react代码部分
2931
react2code(program);
30-
//生成运行环境代码部分
32+
// 生成运行环境代码部分
3133
envir2code(program);
3234
program
3335
.usage('[对应Commands]')
3436
.arguments('<cmd>')
35-
.action(function (cmd, otherParmas) {
37+
.action((cmd, otherParmas) => {
3638
const whiteList = ['api2ts'];
3739
if (whiteList.indexOf(cmd) < 0) {
38-
console.log('🐝 ' + chalk.blue(`${cmd}`) + ' ' + chalk.red('暂未支持~'));
40+
console.log(`🐝 ${chalk.blue(`${cmd}`)} ${chalk.red('暂未支持~')}`);
3941
}
4042
});
4143
program.parse(process.argv);

‎docs/zh-CN.md‎

Whitespace-only changes.

‎lib/api2code/generateCRUD/index.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const generateCRUD = options => {
2+
console.log('CRUD', options);
3+
};
4+
5+
module.exports = generateCRUD;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const generateInterface = options => {
2+
console.log('Interface', options);
3+
};
4+
5+
module.exports = generateInterface;

‎lib/loadConfig.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const { cosmiconfigSync } = require('cosmiconfig');
2+
3+
const loadConfig = () => {
4+
const explorer = cosmiconfigSync('fe-code', {
5+
searchPlaces: [
6+
'.fecoderc',
7+
'.fecoderc.json',
8+
'.fecoderc.yaml',
9+
'.fecoderc.yml',
10+
'.fecoderc.js',
11+
'.fecoderc.cjs',
12+
'fe-code.config.js',
13+
'fe-code.config.cjs',
14+
],
15+
});
16+
17+
return explorer.search().config;
18+
};
19+
20+
module.exports = loadConfig;

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
"@darkobits/lolcatjs": "^3.1.3",
1818
"chalk": "^4.1.1",
1919
"commander": "^5.1.0",
20+
"cosmiconfig": "^7.0.0",
2021
"download-git-repo": "^3.0.2",
2122
"figlet": "^1.4.0",
2223
"fs-extra": "^10.0.0",
2324
"inquirer": "^7.1.0",
24-
"json2ts": "^0.0.7",
2525
"ora": "^4.0.4",
2626
"shelljs": "^0.8.4"
2727
},

‎scripts/api2code.js‎

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
1-
// const json2ts = require('json2ts');
2-
const chalk = require('chalk');
1+
const inquirer = require('inquirer');
2+
const generateInterface = require('../lib/api2code/generateInterface');
3+
const generateCRUD = require('../lib/api2code/generateCRUD');
4+
5+
const handleTargetMap = {
6+
interface: generateInterface,
7+
CRUD: generateCRUD,
8+
};
39

410
const api2code = program => {
511
program
6-
.command('api2code <type>')
12+
.command('api2code')
713
.alias('a2c')
8-
.description('🌽 API接口对TypeScript转化')
9-
.option('-u, --url <url>', 'api接口地址')
10-
.option('-o, --output <output>', '生成文件地址', './')
11-
.action((type, options) => {
12-
if (type !== 'ts' || type !== 'js') {
13-
console.log(`🐝 ${chalk.red('✘ 请正确传递代码模板引擎ts或js')}`);
14-
return;
15-
}
16-
console.log('read config from %s', program.opts().config);
17-
console.log(
18-
'exec "%s" using %s mode and config %s',
19-
type,
20-
options.url,
21-
program.opts().config,
22-
);
14+
.description('🌽 API对TypeScript转化')
15+
.requiredOption('-u, --url <url>', 'api地址')
16+
.requiredOption('-o, --output <output>', '生成文件路径')
17+
.action(options => {
18+
inquirer
19+
.prompt([
20+
{
21+
type: 'list',
22+
name: 'target',
23+
message: 'Please select the type of generation.',
24+
choices: Object.keys(handleTargetMap),
25+
},
26+
])
27+
.then(({ target }) => {
28+
const { url, output } = options;
29+
handleTargetMap[target]({ url, output });
30+
});
2331
});
2432
};
2533
module.exports = api2code;

‎yarn.lock‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,13 +1904,6 @@ json-stable-stringify-without-jsonify@^1.0.1:
19041904
resolved "https://registry.yarnpkg.com/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz#9db7b59496ad3f3cfef30a75142d2d930ad72651"
19051905
integrity sha1-nbe1lJatPzz+8wp1FC0tkwrXJlE=
19061906

1907-
json2ts@^0.0.7:
1908-
version "0.0.7"
1909-
resolved "https://registry.yarnpkg.com/json2ts/-/json2ts-0.0.7.tgz#ab0ee646916a87522093633f1ff0a88474aaf9e5"
1910-
integrity sha1-qw7mRpFqh1Igk2M/H/CohHSq+eU=
1911-
dependencies:
1912-
underscore "^1.8.3"
1913-
19141907
json5@^2.2.0:
19151908
version "2.2.0"
19161909
resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.0.tgz#2dfefe720c6ba525d9ebd909950f0515316c89a3"
@@ -3272,11 +3265,6 @@ unbzip2-stream@^1.0.9:
32723265
buffer "^5.2.1"
32733266
through "^2.3.8"
32743267

3275-
underscore@^1.8.3:
3276-
version "1.10.2"
3277-
resolved "https://registry.yarnpkg.com/underscore/-/underscore-1.10.2.tgz#73d6aa3668f3188e4adb0f1943bd12cfd7efaaaf"
3278-
integrity sha512-N4P+Q/BuyuEKFJ43B9gYuOj4TQUHXX+j2FqguVOpjkssLUUrnJofCcBccJSCoeturDoZU6GorDTHSvUDlSQbTg==
3279-
32803268
universalify@^2.0.0:
32813269
version "2.0.0"
32823270
resolved "https://registry.yarnpkg.com/universalify/-/universalify-2.0.0.tgz#75a4984efedc4b08975c5aeb73f530d02df25717"

0 commit comments

Comments
 (0)