Skip to content

Instantly share code, notes, and snippets.

@iamnewton
Last active December 4, 2019 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamnewton/b28bf10822bf7fef8dee83bd568e534b to your computer and use it in GitHub Desktop.
Save iamnewton/b28bf10822bf7fef8dee83bd568e534b to your computer and use it in GitHub Desktop.
Read a file line-by-line and update a line of it
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