Skip to content

Commit

Permalink
feat: bin指令创建
Browse files Browse the repository at this point in the history
  • Loading branch information
ly4work committed Mar 11, 2021
0 parents commit 37e6477
Show file tree
Hide file tree
Showing 33 changed files with 8,506 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true


[*.md]
trim_trailing_whitespace = false
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/dist/
/node_modules/
/test/
16 changes: 16 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
env: {
browser: true,
commonjs: true,
es6: true
},
extends: ['standard', 'plugin:prettier/recommended'],
globals: {},
parserOptions: {
ecmaVersion: 2018
},
plugins: ['prettier'],
rules: {
'no-useless-return': 0
}
};
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.DS_Store
node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
dist/
.idea/

# Editor directories and files
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
*.sln
test/e2e/mock-template-build
17 changes: 17 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
test
docs
example
coverage
.editorconfig
.eslintignore
.eslintrc.js
.gitignore
.prettierrc.js
.travis.yml
.commitlint.config.js
jest.config.js
package-lock.lock
.vscode
coverage.lcov
.github
CHANGELOG.md
5 changes: 5 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
semi: true,
singleQuote: true,
trailingComma: 'none'
};
18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
language: node_js
node_js:
- '8'
cache:
directories:
- node_modules
install:
- npm install
script:
- npm run lint
deploy:
provider: npm
email: '$NPM_EMAIL'
api_key: '$AUTH_TOKEN'
skip_cleanup: true
on:
branch: master
# after_success:
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 XYZ

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
89 changes: 89 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<hr>
<p>
<a><img src="https://img.shields.io/github/issues/ly4work/gravi.svg" /></a>
<a><img src="https://img.shields.io/github/forks/ly4work/gravi.svg" /></a>
<a><img src="https://img.shields.io/github/stars/ly4work/gravi.svg" /></a>
<a><img src="https://img.shields.io/badge/license-MIT-brightgreen.svg" /></a>
<a><img src="https://img.shields.io/badge/build-passing-green.svg" /></a>
<a><img src="https://img.shields.io/npm/v/gravi-cli.svg" /></a>
</p>
<hr>

# gravi-cli

一个快速上手的前端脚手架, 轻松创建项目模板, 快速开发基于React/Vue的PC端或移动端应用。

## Features

- 支持多类型项目模板, 模板都会集成代码扫描, 工作流等, 具体查看模板github地址。
- 支持添加项目模板, 删除项目模板(folk 作为自己的工具推荐使用)
- 支持自动检测脚手架更新

## Installation & Quick start

### 安装

Windows系统安装
```
$ npm i gravi-cli -g
或 yarn add global gravi-cli
```

Mac下安装
```
$ sudo npm i gravi-cli -g
或 sudo yarn add global gravi-cli
```

### 查看帮助信息

```
$ gravi
```


### 创建项目

```
# 指定项目名字创建项目
$ gravi init 模板名<template-name> 项目名字<project-name>
# 在当前目录创建项目
$ gravi init 模板名<template-name> .
```

### 查看所有支持的项目模板

```
$ gravi ls
```

### 添加项目模板

```
$ gravi add 模板名<template-name> 模板github仓库地址,支持ssh/https格式<git-repo-address>
```

### 删除项目模板

```
$ gravi rm 模板名<template-name>
```

### 发布到npm

执行pkg下的脚本, 自动发版并且生成changelog, travis就会执行检测后续自动发到npm.
```
npm run release
```


## Changelog

[Changelog](https://github.com/ly4work/gravi/blob/master/CHANGELOG.md)

## TODOLIST

- [ ] pc-admin-template
- [ ] h5-react-template
- [ ] h5-vue-template
155 changes: 155 additions & 0 deletions bin/gravi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#!/usr/bin/env node
/**
* Gravi Bin 指令
* @author liyang
* 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;

didYouMean.threshold = 0.6;

main();

/**
* checkNodeVersion 检查Node版本
* @param {string} expect 预期版本
* @param {string} cliName cli名称
* @author liyang
* 2021/03/10
*/
function checkNodeVersion(expect, cliName) {
// 检测node版本是否符合要求范围
if (!semver.satisfies(process.version, expect)) {
console.log(
chalk.red(
'You are using Node ' +
process.version +
', but this version of ' +
cliName +
' requires Node ' +
expect +
'.\nPlease upgrade your Node version.'
)
);
// 退出进程
process.exit(1);
}
}

/**
* suggestCommands 建议命令,当用户输入了错误指令时执行
* @param {string} cmd 命令
* @author liyang
* 2021/03/10
*/
function suggestCommands(cmd) {
var avaliableCommands = program.commands.map(function(cmd) {
return cmd._name;
});
// 简易智能匹配用户命令
var suggestion = didYouMean(cmd, avaliableCommands);
if (suggestion) {
console.log(
' ' + chalk.red('Did you mean ' + chalk.yellow(suggestion) + '?')
);
}
}

/**
* lowercase 字母小写化
* @param {string} str
* @author liyang
* 2021/03/10
*/
function lowercase(str) {
return str.toLocaleLowerCase();
}

/**
* validateArgsLen 校验参数数量
* @param {number} argvLen 参数数量
* @param {number} maxArgvLens 最大参数数量
* @author liyang
* 2021/03/10
*/
function validateArgsLen(argvLen, maxArgvLens) {
if (argvLen > maxArgvLens) {
console.log(
chalk.yellow(
'\n Info: You provided more than argument. the rest are ignored.'
)
);
}
}

function main() {
// 1. 检测node版本
checkNodeVersion(requiredNodeVersion, '@easy/cli');

// 2. --version 版本签名
program
.version(require('../package').version, '-v, --version') // 版本
.usage('<command> [options]'); // 使用信息

// 3. init 初始化项目模板
program
.command('init <template-name> <project-name>')
.description('init a new project from a template')
.action(function(templateName, projectName, cmd) {
// 输入参数校验
validateArgsLen(process.argv.length, 5);
require('../lib/commands/cmd-init')(lowercase(templateName), projectName);
});

// 4. add 添加一个项目模板
program
.command('add <template-name> <git-repo-address>')
.description('add a project template')
.action(function(templateName, gitRepoAddress, cmd) {
validateArgsLen(process.argv.length, 5);
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);
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);
require('../lib/cmd-remove')(templateName);
});
// 7. 处理非法命令
program.arguments('<command>').action(function(cmd) {
// 不退出输出帮助信息
console.log();
console.log(chalk.red('Unknown command ' + chalk.yellow(cmd) + '.'));
console.log();
program.outputHelp();
suggestCommands(cmd);
});
// 8. 重写commander某些事件
enhanceErrorMessages('missingArgument', function(argsName) {
return 'Missing required argument ' + chalk.yellow('<' + argsName + '>');
});
program.parse(process.argv); // 把命令行参数传给commander解析
// 9. 输入gravi显示帮助信息
if (!process.argv.slice(2).length) {
program.outputHelp();
}
}
18 changes: 18 additions & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* 配置commit规则
build:主要目的是修改项目构建系统(例如 glup,webpack,rollup 的配置等)的提交
ci:主要目的是修改项目继续集成流程(例如 Travis,Jenkins,GitLab CI,Circle等)的提交
docs:文档更新
feat:新增功能
merge:分支合并 Merge branch ? of ?
fix:bug 修复
perf:性能, 体验优化
refactor:重构代码(既没有新增功能,也没有修复 bug)
style:不影响程序逻辑的代码修改(修改空白字符,格式缩进,补全缺失的分号等,没有改变代码逻辑)
test:新增测试用例或是更新现有测试
revert:回滚某个更早之前的提交
chore:不属于以上类型的其他类型
*/
module.exports = {
extends: ['@commitlint/config-conventional']
};

0 comments on commit 37e6477

Please sign in to comment.