Gitter migration: Setup redirects (rollout pt. 3)
[gitter.git] / build-scripts / eslint-filter.js
blob926a9ddea9d45ec004f5a57bb7a4a47e8e64e804
1 'use strict';
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);
20 var diffLine = 0;
21 changedLines[absPath] = {};
22 file.chunks.forEach(function(chunk) {
23 chunk.changes.forEach(function(change) {
24 diffLine++;
25 if (change.add) {
26 changedLines[absPath][change.ln] = diffLine;
28 });
29 });
30 });
32 return resolve(changedLines);
33 });
34 });
36 return es.map(function(data, callback) {
37 changedPromise
38 .then(function(changedLines) {
39 if (!changedLines[data.path]) return;
40 if (data.content) {
41 data.eslintFileChanges = changedLines[data.path];
42 return data;
45 return vinylFile.read(data.path).then(function(data) {
46 data.eslintFileChanges = changedLines[data.path];
47 return data;
48 });
50 .then(function(result) {
51 return callback(null, result);
52 }, callback);
53 });
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];
69 });
71 eslintData.messages = messages;
72 var counts = messages.reduce(
73 function(memo, message) {
74 switch (message.severity) {
75 case 1:
76 memo.warningCount++;
77 break;
79 case 2:
80 memo.errorCount++;
81 break;
83 return memo;
86 errorCount: 0,
87 warningCount: 0
91 eslintData.errorCount = counts.errorCount;
92 eslintData.warningCount = counts.warningCount;
95 callback(null, data);
96 });
99 module.exports = {
100 filterFiles: filterFiles,
101 filterMessages: filterMessages