Last active
February 24, 2017 09:11
-
-
Save vsashyn/b0a8f0130b1944446ddf51beccafb6a5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
fs.readFile('eslintlog.json', 'utf8', (err, data) => { | |
if (err) throw err; | |
let parsed = JSON.parse(data); | |
grabWarnings(parsed) | |
.forEach(generateCmd); | |
}); | |
/** | |
* Grab warnings from eslint json results | |
* @param eslintResults | |
*/ | |
function grabWarnings(eslintResults) { | |
return eslintResults.filter(result => { | |
return result.warningCount !== 0 | |
}).map(result => { | |
const convertedResult = { | |
filePath: result.filePath, | |
lines: {} | |
}; | |
result.messages.forEach(msg => { | |
if (!convertedResult.lines[msg.line]) { | |
convertedResult.lines[msg.line] = []; | |
} | |
convertedResult.lines[msg.line].push(msg.ruleId); | |
}); | |
return convertedResult; | |
}) | |
} | |
/** | |
* Generate bash cmd from grabbed results | |
* @param result | |
*/ | |
function generateCmd(result) { | |
let changeCounter = 0; | |
for (let line in result.lines) { | |
let insertedLine = parseInt(line) + changeCounter; | |
let str = `// eslint-disable-next-line ${removeDuplicatesFromArray(result.lines[line]).join(", ")}`; | |
// GENERATE sed COMMAND | |
console.log(`sed -i '${insertedLine} i ${str} \' ${result.filePath}`); | |
changeCounter += 1; | |
} | |
} | |
/** | |
* Utils. | |
* @param arra1 | |
* @returns {Array} | |
*/ | |
function removeDuplicatesFromArray(arra1) { | |
let i, | |
len = arra1.length, | |
result = [], | |
obj = {}; | |
for (i = 0; i < len; i++) { | |
obj[arra1[i]] = 0; | |
} | |
for (i in obj) { | |
result.push(i); | |
} | |
return result; | |
} | |
/* result | |
sed -i '23 i // eslint-disable-next-line prefer-template ' C:\..tricks.js | |
sed -i '36 i // eslint-disable-next-line prefer-template ' C:\..tricks.js | |
sed -i '79 i // eslint-disable-next-line no-useless-escape no-useless-escape ' C:\..validators.js | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment