Skip to content

Instantly share code, notes, and snippets.

@lucnap
Created August 25, 2020 16:13
Show Gist options
  • Save lucnap/a4c755b779e6c40b70115a593c4c2167 to your computer and use it in GitHub Desktop.
Save lucnap/a4c755b779e6c40b70115a593c4c2167 to your computer and use it in GitHub Desktop.
Node.Js search and replace in files in directory recursively
const fs = require('fs');
const path = require('path');
const walkSync = (dir, filelist = []) => {
fs.readdirSync(dir).forEach(file => {
filelist = fs.statSync(path.join(dir, file)).isDirectory()
? walkSync(path.join(dir, file), filelist)
: filelist.concat(path.join(dir, file));
});
return filelist;
}
var elenco = walkSync("./public");
elenco.map(function(v, i, a) {
fs.readFile(v, 'utf8', function (err, data) {
console.log(v);
var formatted = data.replace(/da sostituire/g, "con questo");
fs.writeFile(v, formatted, 'utf8', function (err) {
if (err) return console.log(err);
});
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment