adamreisnz / replace-in-file

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

Can't replace all content in file #125

Closed GeorgeFlorian closed 4 years ago

GeorgeFlorian commented 4 years ago

I am trying to use replace-in-file but it always returns hasChanged: false and it doesn't replace anything in my file.

  const replace = require("replace-in-file");

  const options = {
    files: path.join(uploadedFilesLocation, fileName),
    from: "/\{\{(.*?)\}\}/g",
    to: newFileContent,
  };
  replace(options)
    .then((result) => {
      console.log("Replacement results: ", result);
    })
    .catch((error) => {
      console.error(error);
    });

This returns:

Replacement results:  [
  {
    file: 'server/uploads/mount_dnn.txt',
    hasChanged: false
  }
]

The file is correct, path is corect, newFileContent is different than the old one.

Old Content in file:

line1
line2

New content string:

line1
line2
line3

but the file has the old content inside it.

I have also used from: "/[\s\S]+/g", but with the same negative result.

I don't know what goes wrong.

Any thoughts ?

mattvb91 commented 4 years ago

Same issue here. Did you manage to resolve it?

GeorgeFlorian commented 4 years ago

@mattvb91 I could not use this library as I wanted to so I've stopped trying and went for a simpler approach using https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback