Created
January 19, 2017 17:43
-
-
Save Gpx/a2c8c47ebad6aea7448da326ac5d6de1 to your computer and use it in GitHub Desktop.
This file contains 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') | |
const getLog = () => { | |
return new Promise((done, reject) => { | |
fs.readFile('./semi.log', { encoding: 'utf-8' }, (err, data) => { | |
err | |
? reject(err) | |
: done(data) | |
}) | |
}) | |
} | |
const parseLog = log => { | |
const blocks = getBlocks(log) | |
return blocks | |
} | |
const getBlocks = log => log.split(/^$/m).map(parseBlock) | |
const parseBlock = block => { | |
const [file, ...errors] = block.split('\n').filter(line => !!line) | |
return { file, errors } | |
} | |
const fixSemicolons = block => { | |
const semicolonErrors = block.errors | |
.filter(error => error.includes('Extra semicolon semi')) | |
return readFile(block.file) | |
.then(fileContent => { | |
return semicolonErrors.reduce( | |
(newFileContent, error) => ( | |
removeSemicolon(error, newFileContent) | |
), fileContent) | |
}) | |
.then(newFileContent => { | |
writeFile(block.file, newFileContent) | |
}) | |
} | |
const readFile = file => { | |
return new Promise((done, reject) => { | |
fs.readFile(file, { encoding: 'utf-8' }, (err, data) => { | |
err ? reject(err) : done(data) | |
}) | |
}) | |
} | |
const writeFile = (file, content) => { | |
return new Promise((done, reject) => { | |
fs.writeFile(file, content, { encoding: 'utf-8' }, err => { | |
err ? reject(err) : done() | |
}) | |
}) | |
} | |
const removeSemicolon = (error, fileContent) => { | |
const [, row, column] = error.match(/(\d+):(\d+)/) | |
const rows = fileContent.split('\n') | |
rows[row - 1] = rows[row - 1].slice(0, column - 1) + rows[row - 1].slice(column) | |
return rows.join('\n') | |
} | |
getLog() | |
.then(parseLog) | |
.then(blocks => blocks.forEach(fixSemicolons)) | |
.then(console.log) | |
.catch(console.err) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment