-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-commit
140 lines (133 loc) · 4.28 KB
/
pre-commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env node
var gulp = require('gulp');
var path = require('path');
var gutil = require('gulp-util');
var PluginError = require('gulp-util').PluginError;
var exec = require('child_process').exec;
var gutil = require('gulp-util');
var colors = require('colors');
var eslint = require('gulp-eslint');
var Promise = require('bluebird');
var process = require('process');
var Config = path.join(process.cwd(), './.eslintrc');
// var eslintrc = require(Config);
if(!Config){
console.log("PLEASE CREATE .eslintrc file FITRT".red);
throw new PluginError("error", {message: '没有找到eslint文件'})
}
function mltLint (options) {
this.options = options;
this.flag = 0;
}
mltLint.prototype = {
constructor : mltLint,
test : function (results, arrNum) {
var me = this;
if(results.messages.length != 0){
var messages = results.messages;
console.log(results.filePath.green);
for(var i = 0; i < messages.length; i++){
if(me.numInBox(messages[i].line, arrNum)){
me.flag++;
console.log('errorline and errcolum '.red + messages[i].line + '行' + messages[i].column + '列' +
'\nerrorcode '.red + messages[i].source.blue + '\nerrormessage ' + messages[i].message.blue + '\n');
}
}
}
},
getEsLintResult : function (errorlineNb) {
var me = this;
return new Promise(function (resolve, reject) {
var filePath = errorlineNb.filePath, arr = errorlineNb.errLine;
gulp.src(filePath) //
.pipe(eslint()) // js代码检查
.pipe(eslint.result(function (results) {
var arrNum = arr;
me.test(results, arrNum);
resolve("checkout");
}));
})
},
numInBox : function (num, arr) {
var flag = false;
for(var i = 0; i < arr.length; i++){
var arrList = arr[i];
if(num > arrList[0] && num < arrList[1]){
flag = true;
}
}
return flag;
},
lintChain : function () {
var me = this;
return new Promise(function (resolve, reject) {
var args = "git diff HEAD";
var shell = exec(args, function (error, stdout, stderr) {
resolve(stdout);
});
}).then(function (stdout) {
return new Promise (function (resolve, reject) {
var errLineNb = [];
//将文件和行数从返回值中取出来的正则
var regx = /\+\s[b][\/][a-z/]*[\.][j][s]|[@][@]\s-[0-9]+\,[0-9]+\s\+[0-9]+\,[0-9]+/ig
//将文件取出来的正则
var errFileBox = stdout.match(regx);
if(errFileBox == null){return;}
var errLog = {
filePath : '',
errLine : []
}, errLogLine = [];
for(var i = 0; i<errFileBox.length; i++) {
if(errFileBox[i].match(/^\+/)){
if(errLog.filePath != ''){
errLineNb.push(errLog);
};
errLog = {
filePath : errFileBox[i].slice(4),
errLine : []
};
}else {
var a = errFileBox[i].split('+');
var b = a[1].split(',');
errLogLine.push(parseInt(b[0]));
errLogLine.push(parseInt(b[0]) + parseInt(b[1]));
errLog.errLine.push(errLogLine);
errLogLine = [];
if(i == errFileBox.length-1){
errLineNb.push(errLog);
}
}
};
resolve(errLineNb);
})
}).then(function (errLineNb) {
return new Promise(function(resolve, reject){
if(errLineNb.length){
var promiseItr = [];
for(var i = 0; i < errLineNb.length;i++) {
promiseItr.push(me.getEsLintResult(errLineNb[i]));
}
}
Promise.all(promiseItr).then(function(){
resolve(me.flag);
})
})
}).then(function (flag) {
if(flag){
var error = {
name: 'ESLintError',
message: '你还有未解决的ESLint错误还不能提交代码'
}
throw error;
}else{
console.log("验证通过");
}
}).catch(function (e) {
console.log("代码检测完毕,并且您有代码规范错误😂");
process.kill(process.pid, 'SIGTERM');
})
}
};
console.log("代码检测中......");
var mlt = new mltLint();
mlt.lintChain()