Search through file system and replace after callback.
npm install search-act-replace.
This modules takes a root path, a regex and a callback. It will recursively search the file within the root path and call the callback for each match. It also provides another callback, which you can feed with the text that the match should be replaced with (or false for no replacement).
Let the example talk for itself.
var sar = require'search-act-replace';sar'./test' /www\.\w+\.com/ghttpget'http://' + match0if resstatusCode !== 200replacematch0 + ' (link broken)';elsereplacefalse;;;
The script will automatically ignore
bower_components folders (have a look at the
.sarignore file) as well
as files specified in the roots
.gitignore file as well as a custom
.sarignore file in the root folder.
SAR is an Event Emitter. It supports the following events:
Emitted whenever sar stumbles across a file.
on'file'// file is the filename
Emitted whenever sar stumbles across a directory.
on'directory'// dir is the filename
Whenever SAR ignores to match within a file or ignores a directory.
on'ignore'// file is the filename or foldername
When everything is done. Meaning that all files are traversed, all callbacks finished and all the writing/replacing has been done.
on'end'// file is the filename