esc-file-replace-substring
v0.2.4
Published
Replace (with option to append) string or RegEx in a file, if different vs old contents (SSD/flash/write-IOPS friendly).
Downloads
120
Maintainers
Keywords
Readme
esc-file-replace-substring
Replace (with option to append) string or RegEx in a file, if different vs old contents (SSD/flash memory friendly).
Installation
npm install esc-file-replace-substring
Use
import fileReplaceSubstring from 'esc-file-replace-substring';
fileReplaceSubstring(filePath, 'normal', 'special');
Example
(see test.mjs)
const show = () => console.log('---\n' + fs.readFileSync(filePath) + '---');
fs.writeFileSync(filePath, 'This is a normal config file\nFOO=friend\nBAR=bear\n');
show();
fileReplaceSubstring(filePath, /^\s*FOO=.+/gm, 'FOO=fantastic');
show();
fileReplaceSubstring(filePath, /^\s*ZOO=.+/gm, 'ZOO=zebra', 'append');
show();
fileReplaceSubstring(filePath, /^\s*ZOO=.+/gm, 'ZOO=zzzzz', 'append');
show();
fileReplaceSubstring(filePath, 'normal', 'special');
show();
console.log override-able
Pass in your own logging functions for
- Changed
- Not found (disabled by default)
- Unchanged (disabled by default)
Project goals
- SSD/flash friendly, only writes if the contents of the file will change
- No dependencies
- No nonsense
- Reliable, no radical changes will ever happen.
- Small, simple, easy to audit, yourself
If I have ideas for something different, I'll make a new package rather than make breaking changes.
See also
- https://www.npmjs.com/package/esc-file-replace-substring-between-comments
- https://www.npmjs.com/package/esc-file-replace-contents
- https://www.npmjs.com/package/esc-read-file-without-comments
- https://www.npmjs.com/package/esc-get-data-from-json-file
Notes
Have fun!
Say thanks
Star the repo https://github.com/softwarecreations/esc-file-replace-substring
PR's or issues
Welcome
License
MIT