先丢出最终版的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” } });}步骤一、创建工具项目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-dev3.配置需要在package.json的script中添加如下// my-ci 是自己定义的写成什么都可以"my-ci": “./node_modules/.bin/my-commit"4.配置之后 执行了git add .之后 执行npm run my-ci 将会出现选填补充信息的选项如果觉得git add.之后再执行 npm run my-ci 有点麻烦,可以直接改成// my-ci 是自己定义的写成什么都可以"my-ci”: “git add. && ./node_modules/.bin/my-commit"5 因为以上命令存在不同系统路径不兼容问题 需要加入 cross-envnpm 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即可7 提示信息加上可爱的表情需要在index.js文件中添加 cz-emoji 如下// 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包之后再安装到自己的项目下,执行提交的时候8 为了增强校验功能,加入eslint对文件进行这个有点复杂,需要通过lint-staged来判断所以先安装以下依赖npm i husky –save-devnpm i lint-stage –save-dev配置package.json// 增加属性 “husky”: { “hooks”: { “pre-commit”: “lint-staged” } }, “lint-staged”: { “src/.js”: [ “eslint –fix” ] },// 其中src的具体路径是可以自己配置// 校验规则是基于当前目录的.eslintrc.js 文件,如果有些规则不想要,就配置这个文件 这个时候我们提交代码的时候再输入基本的信息之后会执行一个eslint的代码规则 总结以上配置文件 我们需要 安装的库有npm i my-commit –save-devnpm i cross –save-devnpm i husky –save-devnpm 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-commitnpx my-commit init提交代码的时候直接执行 npm run my-ci 即可11 更智能(摸索中)