Updating all files in a directory with Node.js
const fs = require('fs');
fs.readdirSync('./path/to/directory', {
recursive: true,
withFileTypes: true
}).filter(item => !item.isDirectory()).map(item => {
const filePath = item.parentPath + '/' + item.name;
try {
const data = fs.readFileSync(filePath, 'utf8');
if (data.indexOf('string to be replaced') >= 0) {
const result = data.replace(/string to be replaced/g, replacement);
fs.writeFileSync(filePath, result);
}
} catch (err) {
console.error(err);
}
});