adamreisnz / replace-in-file

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

[BUG] Paths containing Windows DOS separator characters are not recognized #165

Closed northword closed 1 year ago

northword commented 1 year ago

Description

If the path is separated by \\ in ReplaceInFileConfig.files, the path cannot be replaced, when the path separator is replaced by /, it works fine.

(version 6.3.5 works fine)

Environment

Node.js: v16.16.0 replace-in-file: 7.0.1 OS: Windows 11 22H2 22621.1778

Example

build.js

  const optionsAddon = {
    files: [
      'builds/update.json',
      "builds/**/*.json",
      path.join(buildDir, "**/*.rdf"),
      path.join(buildDir, "**/*.dtd"),
      path.join(buildDir, "**/*.xul"),
      path.join(buildDir, "**/*.xhtml"),
      path.join(buildDir, "**/*.json"),
      path.join(buildDir, "addon/prefs.js"),
      path.join(buildDir, "addon/chrome.manifest"),
      path.join(buildDir, "addon/manifest.json"),
      path.join(buildDir, "addon/bootstrap.js"),
      path.join(buildDir, "update.json"),
    ],
    from: replaceFrom,
    to: replaceTo,
    countMatches: true,
  };
  console.log(optionsAddon.files)

  _ = replace.sync(optionsAddon);
  console.log(
    "[Build] Run replace in ",
    _.filter((f) => f.hasChanged).map(
      (f) => `${f.file} : ${f.numReplacements} / ${f.numMatches}`
    )
  );

console:

[
  'builds/update.json',
  'builds/**/*.json',
  'builds\\**\\*.rdf',
  'builds\\**\\*.dtd',
  'builds\\**\\*.xul',
  'builds\\**\\*.xhtml',
  'builds\\**\\*.json',
  'builds\\addon\\prefs.js',
  'builds\\addon\\chrome.manifest',
  'builds\\addon\\manifest.json',
  'builds\\addon\\bootstrap.js',
  'builds\\update.json'
]
[Build] Run replace in  [ 'builds/update.json : 5 / 5', 'builds/addon/manifest.json : 6 / 6' ]

return

[
  {
    file: 'builds/update.json',
    numMatches: 5,
    numReplacements: 5,
    hasChanged: true
  },
  {
    file: 'builds/addon/manifest.json',
    numMatches: 6,
    numReplacements: 6,
    hasChanged: true
  },
  {
    file: 'builds/update.json',
    numMatches: 0,
    numReplacements: 0,
    hasChanged: false
  }
]

builds\\**\\*.rdf has not been changed (confirm that the builds\\addon\\install.rdf file exists and that it contains the eligible replacement text)

windingwind commented 1 year ago

Hi @adamreisnz, any thoughts? Many projects are using this lib, and some of them have relatively high influence in community with more than 1k stars. This bug prevents us from updating to replace-in-file to the latest.

This is an amazing tool and thank all contributors for your hard work.

adamreisnz commented 1 year ago

Hi, I am currently on holiday for a few weeks, and also don't have a windows machine to test this with. I'll happily accept a PR for a proposed fix.

Does this only affect the latest version?

On Thu, Jun 22, 2023, 06:43 windingwind @.***> wrote:

Hi @adamreisnz https://github.com/adamreisnz, any thoughts? Many projects are using this lib, and some of them have relatively high influence in community with more than 1k stars. This bug prevents us from updating to replace-in-file to the latest.

This is an amazing tool and thank all contributors for your hard work.

— Reply to this email directly, view it on GitHub https://github.com/adamreisnz/replace-in-file/issues/165#issuecomment-1602332379, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADXYQRU2UIJ6IDNPP63A2LXMQHT7ANCNFSM6AAAAAAY5QX6DU . You are receiving this because you were mentioned.Message ID: @.***>

northword commented 1 year ago

Does this only affect the latest version?

yes.


It seems to be an other mistake.

We replaced RplaceInFileOption.files with the correct glob pattern and it works fine, however in 6.3.5, \\**\*.js also works fine, which is confusing.

relate: https://github.com/windingwind/zotero-plugin-template/pull/63

hakimio commented 1 year ago

PR: #169