adamreisnz / replace-in-file

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

relative path issue in `files` options #201

Open xkeshav opened 1 month ago

xkeshav commented 1 month ago

First, Thanks for amazing and useful package.

using it in building npm package and when test local it works fine but when do npm pack and when run it it gives error

here is my folder structure

.
├── bin
│   └── ask.js
├── CHANGELOG.md
├── docs
│   └── README.md
├── package.json
├── src
│   ├── helpers
│         ├── replace.js
│   └── index.js
├── __templates
│   ├── CODE_OF_CONDUCT.md

what I am doing is that check __templates folder all files and do some replacement and later move this folder on project root and this code is written in helpers/replace.js file as below

const TEMPLATE_FOLDER = "__templates";

export const replaceDetailsInTemplateFiles = async (info) => {
  const {  first_name, last_name } = info;
  const author_name = `${first_name} ${last_name}`;
  const options = {
    files: [`${TEMPLATE_FOLDER}/**`],
    from: [/<author_name>/g, /<first_name>/g, /<last_name>/g, /<repo_name>/g],
    to: [author_name, first_name, last_name],
  };
  try {
    const results = await replaceInFile(options);
    console.log("Replacement results:", results);
  } catch (error) {
    console.error("Error occurred:", error);
  }
};

this works fine in local but when I built as package uisng npm pack and now testing on one of project but it gives error

below is folder struture inside node_modules

.
├── bin
│   └── ask.js
├── LICENSE
├── package.json
├── README.md
├── src
│   ├── helpers
│        └── replace.js
│   └── index.js
└── __templates
    ├── CODE_OF_CONDUCT.md

7 directories, 12 files

and now it throw error asbelow

Error occurred: Error: No files match the pattern: ./__templates/**
    at file:///home/recursive/dev/angular-work/node_modules/replace-in-file/src/helpers/paths.js:17:13
    at async Promise.all (index 0)
    at async pathsAsync (file:///home/recursive/dev/angular-work/node_modules/replace-in-file/src/helpers/paths.js:77:17)
    at async replaceInFile (file:///home/recursive/dev/angular-work/node_modules/replace-in-file/src/replace-in-file.js:25:17)
    at async replaceDetailsInTemplateFiles (file:///home/recursive/dev/angular-work/node_modules/@xkeshav/package/src/helpers/replace.js:41:21)

I know there is issue of path but when try to change options-->files value with relative but it throw error

so how does we solve this; how do we tell a file which is in src/helpers/ folder to check files which is above src folder ?

adamreisnz commented 1 month ago

Hi, have you tried specifying an absolute path for TEMPLATE_FOLDER, or otherwise resolving it to an absolute path first?

As you mentioned it works fine locally, I assume it's an environmental issue and not an issue with the package.

You can also consider passing the cwd option to the glob settings, like here: https://github.com/adamreisnz/replace-in-file?tab=readme-ov-file#specify-glob-configuration