Created
August 25, 2020 16:13
-
-
Save lucnap/a4c755b779e6c40b70115a593c4c2167 to your computer and use it in GitHub Desktop.
Node.Js search and replace in files in directory recursively
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 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