adamreisnz / replace-in-file

A simple utility to quickly replace contents in one or more files
580 stars 65 forks source link

[Feature Request] Multiple replacements #162

Closed thdoan closed 3 months ago

thdoan commented 1 year ago

It would be nice to be able to do this:

const options = [
  {
    files: 'path/to/file',
    from: /foo/g,
    to: 'bar',
  },
  {
    files: 'path/to/file2',
    from: 'baz',
    to: 'bax',
  }
];
kabilansakthivelu commented 6 months ago

Hi @adamreisnz,

I have raised a PR #190 for this enhancement. Kindly review and please let me know for any improvements.

Thanks

adamreisnz commented 3 months ago

Thanks @kabilansakthivelu @thdoan , I have updated the readme to clarify how you can do this in your scripts easily, without the need for this package to support this natively:

const replacements = [
  {
    files: 'path/to/file1',
    from: /foo/g,
    to: 'bar',
  },
  {
    files: 'path/to/file2',
    from: /bar/g,
    to: 'foo',
  }
]

await Promise.all(
  replacements.map(options => replaceInFile(options))
)