基于git diff进行的eslint代码检测
2016-08-26 14:40
281 查看
缘起
在项目中, 通常都会使用代码检测工具来规范团队的代码风格, 比如eslint。随着代码的不断增加,
eslint进行代码检测的时间也越来越久。每次检测的时候, 需要检测的文件和实际检测的文件极度不对称,所以便基于
git diff写了这样一个小工具。
源代码
2017-02-21更新太久时间没来博客园了, 今天来了, 然后顺便把这个也更新了吧。 最开始写这个的时候, 思路不清晰, 以至于数据处理部分用了bash, node, 然而实际上我只是通过
git来获取到与指定分支相比发生变化的文件而已, 所以这个过程应该从git入手而不是自己另行处理, 庆幸的是, 刚好有那么个选项
git diff origin/<branchname> --name-only可以让我获取到当前分支和指定分支发生变化的文件, 所以现在要达到之前的效果, 只需要执行这个bash脚本即可。
#!/bin/bash INFO='\033[36m'; NOR='\033[0m'; ERR='\033[31m'; br='dev'; echo -e "${INFO}run lint now ... just wait a moment ...${NOR}"; [ $1 ] && br=$1; log=`git diff origin/${br} --name-only | grep ".js$" | egrep "^src/|^tests/"`; if [ -z "${log}" ]; then echo -e "${INFO}No file changed, exit now ${NOR}"; exit 0; fi; node ./node_modules/eslint/bin/eslint.js $log | grep error -C 100 --color=auto;
更新之前的
启动脚本(lint.sh)
#!/bin/bash INFO='\033[36m'; NOR='\033[0m'; ERR='\033[31m'; br='dev'; echo -e "${INFO}run lint now ... just wait a moment ...${NOR}"; if [ $1 ]; then br=$1; fi; # echo "The br is $br"; git diff origin/${br} > diff.log; log=`cat diff.log | grep 'diff --git a/src'`; if [[ -z ${log} ]]; then echo -e "${INFO}没有文件发生变化${NOR}"; else echo ''; node ./lint-by-diff.js > lint.log; cat lint.log | grep error -C 1000 --color=auto; fi; echo -e "${INFO}done ...${NOR}"; rm diff.log change.log lint.log 2> /dev/null read;
检测工具(lint-by-diff.js)
const fs = require('fs'); const shelljs = require('shelljs'); const jsFiles = [], LOG__PATH = './diff.log', FILE = /diff --git [a]([\s\S]*?) /g, data = fs.readFileSync(LOG__PATH).toString(), _files = data.match(FILE), len = _files.length; let i = 0; while (i < len) { const _item = _files[i++].trim(); if (!/.js$/.test(_item)) continue; const item = './' + _item.slice(13); if (!/^\.\/src\//.test(item)) continue; if (!fs.existsSync(item)) continue; jsFiles.push(item); } if (jsFiles.length === 0) { console.log('没有文件发生变化'); console.log(''); process.exit(1); } console.log('------------------------------'); console.log(' 以下文件发生改变: '); console.log(jsFiles.join('\n')); console.log('------------------------------'); shelljs.exec('node ./node_modules/eslint/bin/eslint.js ' + jsFiles.join(' '));
原理
通过git diff origin/dev获取到和dev分支的不同, 从而知道哪些文件需要进行代码检测(dev上的是通过检测的), 然后运行
eslint的时候就指定这部分文件。
使用
在项目根目录下输入./lint.sh或者
bash ./lint.sh, 默认的远程分支是dev, 如果需要和其他分支比较的话, 指定远程分支名,比如
./lint.sh master
不足
使用了bash, 导致这个看起来有点不伦不类, 使用纯js也许会更好, 但是我毕竟半吊子→_←相关文章推荐
- (论文+代码)R-FCN:基于区域的全卷积神经网络进行物体检测
- OpenFace库(Tadas Baltrusaitis)中基于HOG进行正脸人脸检测的测试代码
- OpenFace库(Tadas Baltrusaitis)中基于Haar Cascade Classifiers进行人脸检测的测试代码
- Windows下Git使用记录--03 Git GUI与金山快盘组建私人代码仓库进行项目开发
- Git diff代码比较的高级技巧
- git 进行代码回滚
- passwordStrength 基于jquery的密码强度检测代码使用介绍
- 从x264抽出对cpu进行检测的asm代码并放到vc工程里
- Git进行代码管理
- 使用Purify进行java代码内存泄漏检测与诊断
- 基于Android中dp和px之间进行转换的实现代码
- 使用gitosis和git进行代码权限管理
- 基于测试概念进行代码设计的七条基本原则
- Git diff 代码比较的高级技巧
- 基于svn diff结果的有效代码量统计
- 对基于规则推理机代码进行改动的一点说明 by 大年儿
- 基于密码强度检测代码
- 对代码进行内存泄漏检测的工具——Valgrind
- 一个简单好用的基于git的代码托管网站
- 使用Git进行小项目代码管理