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

Using CLI with RegExp and a config file in version 8 #197

Closed
elchininet opened this issue Jul 1, 2024 · 6 comments
Closed

Using CLI with RegExp and a config file in version 8 #197

elchininet opened this issue Jul 1, 2024 · 6 comments

Comments

@elchininet
Copy link

Hi there,

I am trying to update to version 8 one of my repos. I have converted the config file to JSON, double escaped the escaped characters and added the --isRegex flag. But I am receiving the next error:

TypeError: from.replace is not a function
    at main (file:///xxx/node_modules/replace-in-file/bin/cli.js:37:24)
error Command failed with exit code 1.

I have prepared a minimum reproducible repo so maybe you can spot what is the issue there.

  1. Just run yarn replace first (it runs without issues and replace the string in the file)
  2. Then try to run yarn replace-regexp

Could you indicate if I am missing something here?

Regards

@adamreisnz
Copy link
Owner

Thank you, I'll have a look shortly!

@adamreisnz
Copy link
Owner

adamreisnz commented Jul 2, 2024

I think if you are using a regex in the JSON config now, then you don't need to use the --isRegex flag in addition, as this will automatically be detected if you are using. It should only be necessary when passing the from parameter via CLI argument as well. I will update the readme to clarify that and update the code to handle that better.

Try not using the --isRegex flag and let me know if that works.

I have updated the readme accordingly and released a new version 8.0.2 to better handle this.

@elchininet
Copy link
Author

Hi @adamreisnz,

And how is detected that I am writing a regular expression?

The config was a Node.js module before, so it was possible to write the from parameter as a string or as a regular expression, but it is a JSON file now, so it is only possible to write a string. I though that the intention of the --isRegex flag was precissely that, to know that I am not intending to search a string but a regular expression no mattering if it came from the CLI parameters or from the config.

Just check the code in the minimum reproducible repo and remove the --isRegex flag, you will not receive any error but there will not be any changes then because the string is not found in the file:

Replacing '/\/\w{3} \d{3}\/g/' with 'replaced'
No files were changed
✨  Done in 0.15s.

If I try to use only CLI parameters, removing completely the JSON config, it works as expected.

@elchininet
Copy link
Author

I am seeing that you are checking if the from parameter from the config matches a regexp, and convert it to a regexp then. I think that you need to do the same that you are doing here, because the resulting regexp will not be right in that case:

// correct
new RegExp('\\w+') // /\w+/

// incorrect
new RegExp('/\\w+/') // /\/\w+\//

adamreisnz added a commit that referenced this issue Jul 2, 2024
@adamreisnz
Copy link
Owner

Ahh, yes, gotcha, thank you.
Have fixed in 8.0.3

@elchininet
Copy link
Author

Thanks @adamreisnz,
Let me update it 👍

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