3 var path
= require('path');
4 var simpleGit
= require('simple-git')();
5 var parse
= require('parse-diff');
6 var es
= require('event-stream');
7 var vinylFile
= require('vinyl-file');
8 var Promise
= require('bluebird');
10 function filterFiles(baseBranch
) {
11 var changedPromise
= new Promise(function(resolve
, reject
) {
12 simpleGit
.diff([baseBranch
], function(err
, diff
) {
13 if (err
) return reject(err
);
15 var result
= parse(diff
);
17 var changedLines
= {};
18 result
.forEach(function(file
) {
19 var absPath
= path
.resolve(file
.to
);
21 changedLines
[absPath
] = {};
22 file
.chunks
.forEach(function(chunk
) {
23 chunk
.changes
.forEach(function(change
) {
26 changedLines
[absPath
][change
.ln
] = diffLine
;
32 return resolve(changedLines
);
36 return es
.map(function(data
, callback
) {
38 .then(function(changedLines
) {
39 if (!changedLines
[data
.path
]) return;
41 data
.eslintFileChanges
= changedLines
[data
.path
];
45 return vinylFile
.read(data
.path
).then(function(data
) {
46 data
.eslintFileChanges
= changedLines
[data
.path
];
50 .then(function(result
) {
51 return callback(null, result
);
56 function filterMessages() {
57 return es
.map(function(data
, callback
) {
58 var eslintData
= data
.eslint
;
59 var eslintFileChanges
= data
.eslintFileChanges
;
60 var messages
= eslintData
&& eslintData
.messages
;
62 if (!eslintFileChanges
) {
63 return callback(null, data
);
66 if (messages
&& messages
.length
) {
67 messages
= messages
.filter(function(message
) {
68 return eslintFileChanges
[message
.line
];
71 eslintData
.messages
= messages
;
72 var counts
= messages
.reduce(
73 function(memo
, message
) {
74 switch (message
.severity
) {
91 eslintData
.errorCount
= counts
.errorCount
;
92 eslintData
.warningCount
= counts
.warningCount
;
100 filterFiles
: filterFiles
,
101 filterMessages
: filterMessages