Skip to content

Instantly share code, notes, and snippets.

@linkstrifer
Created August 8, 2017 21:56
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 linkstrifer/4297bb67b9338427687c03cbfef0ffb5 to your computer and use it in GitHub Desktop.
Save linkstrifer/4297bb67b9338427687c03cbfef0ffb5 to your computer and use it in GitHub Desktop.
search for string on files and replace with something else using node
const Finder = require('fs-finder');
const fs = require('fs');
const changeCase = require('change-case');
var files = Finder.from('./components/').findFiles('*.js');
function editFile(filePath) {
fs.readFile(filePath, 'utf-8', function(err, data) {
if (err) throw err;
const oldValues = data.match(/{(this.renderFormGroup)+(.*?)}/g);
let newValues = []
let newData = data
if (oldValues) {
oldValues.forEach((value) => {
const temp = value.split("'");
let newValue = value
let newId;
newId = changeCase.paramCase(temp[1]);
newValue = newValue.replace(temp[1], `id-${newId}`);
newValues.push(newValue)
});
newValues.forEach((value, index) => {
newData = newData.replace(oldValues[index], value);
fs.writeFile(filePath, newData, 'utf-8');
});
}
});
}
function loopFiles() {
files.forEach(editFile)
}
loopFiles();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment