Skip to content

Commit

Permalink
feat: 完成指令编写包括init, ls, rm, add,配置h5-vue-template
Browse files Browse the repository at this point in the history
  • Loading branch information
ly4work committed Mar 11, 2021
1 parent 903efe4 commit 9d75d1b
Show file tree
Hide file tree
Showing 20 changed files with 11,143 additions and 55 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Mac 下安装

```
$ sudo npm i gravi-cli -g
或 sudo yarn global add gravi-cli
或 sudo yarn global add gravi-cli
```

### 查看帮助信息
Expand Down
43 changes: 27 additions & 16 deletions bin/gravi.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
* 2021/03/10
*/

var program = require('commander'); // 命令行工具
var chalk = require('chalk'); // 命令行输出美化
var didYouMean = require('didyoumean'); // 简易的智能匹配引擎
var semver = require('semver'); // npm的语义版本包
var enhanceErrorMessages = require('../lib/util/enhanceErrorMessages.js');
var requiredNodeVersion = require('../package.json').engines.node;
const program = require('commander'); // 命令行工具
const chalk = require('chalk'); // 命令行输出美化
const didYouMean = require('didyoumean'); // 简易的智能匹配引擎
const semver = require('semver'); // npm的语义版本包
const enhanceErrorMessages = require('../lib/util/enhanceErrorMessages.js');
const requiredNodeVersion = require('../package.json').engines.node;
const validateArgsLenEnum = require('../lib/constant/validateArgsLenEnum');
const processCodeEnum = require('../lib/constant/processCodeEnum');
const dymConfig = require('../lib/constant/dymConfig');

didYouMean.threshold = 0.6;
// 设置匹配阈值
didYouMean.threshold = dymConfig.THRESHOLD;

main();

Expand All @@ -38,7 +42,7 @@ function checkNodeVersion(expect, cliName) {
)
);
// 退出进程
process.exit(1);
process.exit(processCodeEnum.UncaughtException);
}
}

Expand All @@ -49,11 +53,11 @@ function checkNodeVersion(expect, cliName) {
* 2021/03/10
*/
function suggestCommands(cmd) {
var avaliableCommands = program.commands.map(function(cmd) {
const avaliableCommands = program.commands.map(function(cmd) {
return cmd._name;
});
// 简易智能匹配用户命令
var suggestion = didYouMean(cmd, avaliableCommands);
const suggestion = didYouMean(cmd, avaliableCommands);
if (suggestion) {
console.log(
' ' + chalk.red('Did you mean ' + chalk.yellow(suggestion) + '?')
Expand Down Expand Up @@ -103,7 +107,7 @@ function main() {
.description('init a new project from a template')
.action(function(templateName, projectName, cmd) {
// 输入参数校验
validateArgsLen(process.argv.length, 5);
validateArgsLen(process.argv.length, validateArgsLenEnum.Init);
require('../lib/commands/cmd-init')(lowercase(templateName), projectName);
});

Expand All @@ -112,28 +116,31 @@ function main() {
.command('add <template-name> <git-repo-address>')
.description('add a project template')
.action(function(templateName, gitRepoAddress, cmd) {
validateArgsLen(process.argv.length, 5);
validateArgsLen(process.argv.length, validateArgsLenEnum.Add);
require('../lib/commands/cmd-add')(
lowercase(templateName),
gitRepoAddress
);
});

// 5. ls 列出支持的项目模板
program
.command('ls')
.description('list all available project template')
.action(function(cmd) {
validateArgsLen(process.argv.length, 3);
validateArgsLen(process.argv.length, validateArgsLenEnum.List);
require('../lib/commands/cmd-list')();
});

// 6. rm 删除一个项目模板
program
.command('rm <template-name>')
.description('remove a project template')
.action(function(templateName, cmd) {
validateArgsLen(process.argv.length, 4);
validateArgsLen(process.argv.length, validateArgsLenEnum.Remove);
require('../lib/cmd-remove')(templateName);
});

// 7. 处理非法命令
program.arguments('<command>').action(function(cmd) {
// 不退出输出帮助信息
Expand All @@ -143,12 +150,16 @@ function main() {
program.outputHelp();
suggestCommands(cmd);
});

// 8. 重写commander某些事件
enhanceErrorMessages('missingArgument', function(argsName) {
return 'Missing required argument ' + chalk.yellow('<' + argsName + '>');
});
program.parse(process.argv); // 把命令行参数传给commander解析
// 9. 输入gravi显示帮助信息

// 9. 把命令行参数传给commander解析
program.parse(process.argv);

// 10. 输入gravi显示帮助信息
if (!process.argv.slice(2).length) {
program.outputHelp();
}
Expand Down
21 changes: 19 additions & 2 deletions lib/commands/cmd-add.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
/**
* Gravi Bin CDM : gravi add
* 增加模板
* @author liyang
* 2021/03/10
*/

const chalk = require('chalk');
const isGitUrl = require('is-git-url');
const { stopSpinner } = require('../util/spinner');
const { log } = require('../util/logger');
const {
readTemplateJson,
writeTemplateJson
} = require('../util/IOTemplateData');
} = require('../util/ioTemplateData');

/**
* addProjectTemplate 新增项目模板
* @param {string} templateName 模板名称
* @param {string} gitRepoAddress git地址
* @author liyang
* 2021/03/11
*/
async function addProjectTemplate(templateName, gitRepoAddress) {
const templateGitRepoJson = readTemplateJson();
if (templateGitRepoJson[templateName]) {
Expand Down Expand Up @@ -35,10 +49,13 @@ async function addProjectTemplate(templateName, gitRepoAddress) {
log();
}
/**
* format
* getRealGitRepo format git地址format规则
* https => https://github.com/NuoHui/node_code_constructor.git
* ssh => git@github.com:NuoHui/node_code_constructor.git
* want => github:NuoHui/node_code_constructor
* @param {string} gitRepo git地址
* @author liyang
* 2021/03/11
*/
function getRealGitRepo(gitRepo) {
const sshRegExp = /^git@github.com:(.+)\/(.+).git$/;
Expand Down
25 changes: 20 additions & 5 deletions lib/commands/cmd-init.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* Gravi Bin CDM : gravi init
* 初始化项目
* @author liyang
* 2021/03/10
*/

const program = require('commander');
const chalk = require('chalk');
const inquirer = require('inquirer');
Expand All @@ -10,9 +17,17 @@ const exec = require('child_process').exec;
const { pauseSpinner } = require('../util/spinner');
const Creator = require('../Creator');
const { clearConsole } = require('../util/clearConsole');
const { readTemplateJson } = require('../util/IOTemplateData');
const { readTemplateJson } = require('../util/ioTemplateData');

async function create(templateName, projectName, options) {
/**
* init 初始项目
* @param {string} templateName 模板名称
* @param {string} projectName 项目名称
* @param {string} options 额外参数
* @author liyang
* 2021/03/11
*/
async function init(templateName, projectName, options) {
// TODO options方便后续扩展
// 项目模板白名单
const templateGitRepoJson = readTemplateJson();
Expand Down Expand Up @@ -92,13 +107,13 @@ async function create(templateName, projectName, options) {
}

// 目录不存在
process.env.EASY_CLI_TEMPLATE_NAME = templateName;
const creator = new Creator(name, targetDir);
process.env.GRAVI_CLI_TEMPLATE_NAME = templateName;
const creator = new Creator({ name, targetDir });
await creator.create(options);
}

module.exports = (templateName, projectName, ...args) => {
return create(templateName, projectName, ...args).catch(err => {
return init(templateName, projectName, ...args).catch(err => {
pauseSpinner();
console.error(err);
process.exit(1);
Expand Down
4 changes: 2 additions & 2 deletions lib/commands/cmd-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

const chalk = require('chalk');
const { readTemplateJson } = require('../util/IOTemplateData');
const { readTemplateJson } = require('../util/ioTemplateData');
const { stopSpinner } = require('../util/spinner');
const { log } = require('../util/logger');

Expand All @@ -22,7 +22,7 @@ async function listAllTemplate() {
stopSpinner();
log();
log(
`➡️ Template name ${chalk.yellow(key)}, Github address ${chalk.yellow(
`➡️ Template name ${chalk.cyan(key)}, Github address ${chalk.yellow(
templateGitRepoJson[key]['github']
)}`
);
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/cmd-remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const { log } = require('./util/logger');
const {
readTemplateJson,
writeTemplateJson
} = require('../util/IOTemplateData');
} = require('../util/ioTemplateData');

/**
* deleteTemplate 删除模板
Expand Down
10 changes: 3 additions & 7 deletions lib/config/templateGItRepo.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
{
"node": {
"github": "https://github.com/NuoHui/node_code_constructor.git",
"download": "github:NuoHui/node_code_constructor"
},
"vue": {
"github": "https://github.com/NuoHui/vue_code_constructor.git",
"download": "github:NuoHui/vue_code_constructor"
"h5-vue": {
"github": "https://github.com/ly4work/gravi-h5-vue-template.git",
"download": "github:ly4work/gravi-h5-vue-template.git"
}
}
6 changes: 0 additions & 6 deletions lib/config/templateGitRepo.yml

This file was deleted.

8 changes: 8 additions & 0 deletions lib/constant/dymConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* dym 智能匹配引擎配置
* @author liyang
* 2021/03/11
*/
module.exports = {
THRESHOLD: 0.6
};
46 changes: 46 additions & 0 deletions lib/constant/processCodeEnum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* processCodeEnum 进程终止 状态码
* @author
* 2021/
*/

module.exports = {
// 0 成功退出
Success: 0,

// 1 未捕获的致命异常 Uncaught Fatal Exception
UncaughtException: 1,

// 2 未使用 Unused (bash保留) reserved by Bash for builtin misuse
UnusedBash: 2,

// 3 解析错误 Internal JavaScript Parse Error
JSParseError: 3,

// 4 评估失败 Internal JavaScript Evaluation Failure
JSEvaluationError: 4,

// 5 致命错误 Fatal Error
FatalError: 5,

// 6 未正确的异常处理 Non-function Internal Exception Handler
NonFunctionException: 6,

// 7 异常处理函数运行时失败 Internal Exception Handler Run-Time Failure
HandlerRunTimeException: 7,

// 8 未使用 未捕获 Unused exit code 8 sometimes indicated an uncaught exception
UnusedNode: 8,

// 9 无效的参数 Invalid Argument
InvalidArgs: 9,

// 10 运行时失败 Internal JavaScript Run-Time Failure
RunTimeException: 10,

// 12 无效的调试参数 Invalid Debug Argument
InvalidDebugArgs: 12,

// 128 信号退出 Signal Exits
SignalExits: 128
};
19 changes: 19 additions & 0 deletions lib/constant/validateArgsLenEnum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* validateArgsLenEnum 枚举-指令参数个数校验
* @author liyang
* 2021/03/11
*/

module.exports = {
// init
Init: 5,

// add
Add: 5,

// ls
List: 3,

// rm
Remove: 4
};

0 comments on commit 9d75d1b

Please sign in to comment.