adamreisnz / replace-in-file

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

replace is not working with path.join #184

Closed indraraj26 closed 8 months ago

indraraj26 commented 8 months ago

index.js

const re = require('replace-in-file');
const path = require('path');
const fs = require('fs');

function replaceFoo() {
  const indexHtmlPath = path.join(__dirname, 'index.html');
  const isIndexHtmlPathExist = fs.existsSync(indexHtmlPath);
  console.log(isIndexHtmlPathExist, 'isIndexHtmlPathExist')
  const option = {
    files: indexHtmlPath, // If i put hardcoded 'index.html' then it is work fine fine
    from: /foo/g,
    to: 'bar',
    countMatches: true,
  };
  console.log(option);
  const output = re.sync(option);
  console.log(output);
}

replaceFoo()

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    foo
</body>
</html>

Node -v

v14.18.1

replace-in-file version

    "replace-in-file": "^7.1.0"

image

indraraj26 commented 8 months ago

I don't know the exact cause but this "replace-in-file": "^6.3.5" is working fine but not with 7x

adamreisnz commented 8 months ago

Hi, not sure, are the files are passed to Glob, could be something with the newer version of Glob that is used in 7.x. I suggest perhaps trying the disableGlobs setting or specifically the windowsPathsNoEscape setting.

Feel free to reopen if those don't help and if you can pin point an error in this library.

indraraj26 commented 7 months ago

Yes indeed it is working fine with disableGlobs: true or

 glob: {

      //Glob settings here (examples given below)
      dot: true, //To include file names starting with a dot
      windowsPathsNoEscape: true, //To fix paths on Windows OS when path.join() is used to create paths
    },