Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

relative path issue in files options #201

Open
xkeshav opened this issue Sep 29, 2024 · 1 comment
Open

relative path issue in files options #201

xkeshav opened this issue Sep 29, 2024 · 1 comment

Comments

@xkeshav
Copy link

xkeshav commented Sep 29, 2024

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants