#!/usr/bin/env node "use strict"; const path = require('path'); const editJsonFile = require("edit-json-file"); const arg = process.argv // 初始化my-commit ,将部分脚本写入到package.json中 if (arg[2] && arg[2] === 'init') { // If the file doesn't exist, the content will be an empty object by default. let file = editJsonFile(`${process.cwd()}/package.json`); // Set a couple of fields file.set("husky", {"hooks": { "pre-commit": "lint-staged" }}); file.set("lint-staged", { "src/*.js": "['eslint --fix']" }); // 询问是否所有使用git add . var List = require('prompt-list'); var list = new List({ name: 'order', message: '是否默认使用git add .', // choices may be defined as an array or a function that returns an array choices: [ 'yes', 'no' ] }) // async list.ask(function(answer) { file.set("scripts", { "my-ci": answer === 'yes' ? 'git add . && cross-env ./node_modules/.bin/my-commit' : 'cross-env ./node_modules/.bin/my-commit' }); // Output the content file.save(); var shell = require('shelljs'); console.log('开始安装依赖'); shell.exec('npm i husky --save-dev', {async: true}) console.log('正在安装 husky---- '); shell.exec('npm i cross-env --save-dev', {async: true}) console.log('正在安装cross-env ---- '); shell.exec('npm i lint-staged --save-dev', {async: true}) }) } else { const bootstrap = require('commitizen/dist/cli/git-cz').bootstrap; bootstrap({ cliPath: path.join(__dirname, '../../node_modules/commitizen'), // this is new config: { "path": "cz-conventional-changelog", "path": "cz-emoji" } }); }
1.使用git/gitlab建立一个空的仓库
2.在空仓库中添加
index.js
内容以下
// index.js #!/usr/bin/env node "use strict"; const bootstrap = require('commitizen/dist/cli/git-cz').bootstrap; bootstrap({ cliPath: path.join(__dirname, '../../node_modules/commitizen'), // this is new config: { "path": "cz-conventional-changelog" } });
my-commit
)1.先发布你的工具项目到npm,至关于建立一个npm包、具体怎么发布 这里不作赘述,网上不少教程
2.安装工具(假设插件名称
my-commit
)
npm install my-commit --save-dev
3.配置
须要在package.json
的script
中添加以下node
// my-ci 是本身定义的写成什么均可以 "my-ci": "./node_modules/.bin/my-commit"
4.配置以后 执行了git add .
以后 执行npm run my-ci
将会出现选填补充信息的选项
若是以为git add.
以后再执行 npm run my-ci
有点麻烦,能够直接改为git
// my-ci 是本身定义的写成什么均可以 "my-ci": "git add. && ./node_modules/.bin/my-commit"
5 由于以上命令存在不一样系统路径不兼容问题 须要加入
cross-env
npm install cross-env --save-dev
6 再次修改
package.json
// my-ci 是本身定义的写成什么均可以 "my-ci": "git add. && cross-env ./node_modules/.bin/my-commit"
当须要提交代码的时候,不用执行git add .
直接执行npm run my-ci
便可shell
7 提示信息加上可爱的表情
须要在index.js
文件中添加 cz-emoji
以下npm
// index.js #!/usr/bin/env node "use strict"; const bootstrap = require('commitizen/dist/cli/git-cz').bootstrap; bootstrap({ cliPath: path.join(__dirname, '../../node_modules/commitizen'), // this is new config: { "path": "cz-conventional-changelog", "path": "cz-emoji" } });
这个时候 从新发npm包以后再安装到本身的项目下,执行提交的时候json
8 为了加强校验功能,加入
eslint
对文件进行
这个有点复杂,须要经过lint-staged
来判断bootstrap
因此先安装如下依赖async
npm i husky --save-dev npm i lint-stage --save-dev
配置package.json
工具
// 增长属性 "husky": { "hooks": { "pre-commit": "lint-staged" } }, "lint-staged": { "src/*.js": [ "eslint --fix" ] }, // 其中src的具体路径是能够本身配置 // 校验规则是基于当前目录的.eslintrc.js 文件,若是有些规则不想要,就配置这个文件
这个时候咱们提交代码的时候再输入基本的信息以后会执行一个eslint的代码规则 gitlab
总结以上配置文件 咱们须要 ui
安装的库有
npm i my-commit --save-dev npm i cross --save-dev npm i husky --save-dev npm i lint-stage --save-dev
须要配置package.json属性有
"script": { ... "my-ci": "git add. && cross-env ./node_modules/.bin/my-commit" }, "husky": { "hooks": { "pre-commit": "lint-staged" } }, "lint-staged": { "src/*.js": [ "eslint --fix" ] },
若是每一个项目都这么玩。其实有点耗时间,因此咱们作了一下自动化
10 初步自动化
修改my-commit中的 index.js
#!/usr/bin/env node "use strict"; const path = require('path'); const editJsonFile = require("edit-json-file"); const arg = process.argv // 初始化my-commit ,将部分脚本写入到package.json中 if (arg[2] && arg[2] === 'init') { // If the file doesn't exist, the content will be an empty object by default. let file = editJsonFile(`${process.cwd()}/package.json`); // Set a couple of fields file.set("husky", {"hooks": { "pre-commit": "lint-staged" }}); file.set("lint-staged", { "src/*.js": "['eslint --fix']" }); // 询问是否所有使用git add . var List = require('prompt-list'); var list = new List({ name: 'order', message: '是否默认使用git add .', // choices may be defined as an array or a function that returns an array choices: [ 'yes', 'no' ] }) // async list.ask(function(answer) { file.set("scripts", { "my-ci": answer === 'yes' ? 'git add . && cross-env ./node_modules/.bin/my-commit' : 'cross-env ./node_modules/.bin/my-commit' }); // Output the content file.save(); var shell = require('shelljs'); console.log('开始安装依赖'); shell.exec('npm i husky --save-dev', {async: true}) console.log('正在安装 husky---- '); shell.exec('npm i cross-env --save-dev', {async: true}) console.log('正在安装cross-env ---- '); shell.exec('npm i lint-staged --save-dev', {async: true}) }) } else { const bootstrap = require('commitizen/dist/cli/git-cz').bootstrap; bootstrap({ cliPath: path.join(__dirname, '../../node_modules/commitizen'), // this is new config: { "path": "cz-conventional-changelog", "path": "cz-emoji" } }); }
清除掉之前配置的package.json
只要两部安装便可
npm i my-commit npx my-commit init
提交代码的时候直接执行 npm run my-ci
便可
11 更智能(摸索中)