Last active
December 4, 2019 22:26
-
-
Save iamnewton/b28bf10822bf7fef8dee83bd568e534b to your computer and use it in GitHub Desktop.
Read a file line-by-line and update a line of it
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'); | |
const readline = require('readline'); | |
const readFile = () => { | |
// create readline interface and output to a new file; can't figure out how to write to same file | |
const readInterface = readline.createInterface({ | |
input: fs.createReadStream(findup('sonar-project.properties')), | |
output: fs.createWriteStream('sonar-project.properties.temp'), | |
console: false | |
}); | |
// read line by line and modify a line and rewrite it back | |
readInterface.on('line', function(line) { | |
if (line.includes('projectVersion')) { | |
this.output.write('sonar.projectVersion=2020.1.0\n'); | |
} else { | |
this.output.write(`${line}\n`); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment