0.2.4 • Published 7 months ago

esc-file-replace-substring v0.2.4

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

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();

image

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

Notes

Have fun!

Say thanks

Star the repo https://github.com/softwarecreations/esc-file-replace-substring

PR's or issues

Welcome

License

MIT