Created
August 8, 2017 21:56
-
-
Save linkstrifer/4297bb67b9338427687c03cbfef0ffb5 to your computer and use it in GitHub Desktop.
search for string on files and replace with something else using node
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 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