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

Only modify files that match the replace expression #70

Open
bassmanpaul opened this issue Jul 27, 2016 · 9 comments
Open

Only modify files that match the replace expression #70

bassmanpaul opened this issue Jul 27, 2016 · 9 comments
Labels

Comments

@bassmanpaul
Copy link

From what I can tell, the gulp-replace modifies all files regardless of whether they match the replace condition or not.

My scenario is that gulp-replace correctly updates my files but also my fonts, images and text files that are Unicode format. The skipBinary option successfully skips the fonts & images but my .txt files that have unicode characters in them are still left broken.

Some other skip options would be nice, but surely it would be better to skip modifying files unless they actually have a match?

Thoughts?

P.S. Thanks for providing a very useful module :)

@lazd
Copy link
Owner

lazd commented Aug 11, 2016

Interesting... This definitely sounds like a bug. I'd accept a PR to fix it.

@bassmanpaul
Copy link
Author

Sure thing.

How about something like the following before the skipBinary check?

options = options || {};

if(options.testFirst === null || options.testFirst)
{
  if(!String(file.contents).match(search))
  {
    return callback(null, file);
  }
}

This way users can opt out of the extra regular expression check if they are more concerned with performance than stream modification.

@lazd
Copy link
Owner

lazd commented Aug 19, 2016

Hmm, I'd rather do it without introducing a new option... Perhaps we can perform the replacement as normal, test for equality, and if they're equal, then don't modify the file?

@bassmanpaul
Copy link
Author

Okay, fair enough. How about placing the check (without options) at the start of the doReplace(); function?

function doReplace()
{
  if(!String(file.contents).match(search))
  {
    return callback(null, file);
  }
...

To do it before this point would probably require the check in two places (the skipBinary check and after the skipBinary check). Doing the check before the skipBinary would mean unnecessary checks for skipBinary files?

@lazd lazd added the bug label Mar 27, 2017
@akaleeroy
Copy link

Is there a problem preventing this fix?

@lazd
Copy link
Owner

lazd commented Mar 26, 2018

@akaleeroy the problem is there is no PR with the fix and tests :) Feel free to send one along and I'll review it!

@akaleeroy
Copy link

@lazd Haha, yeah, I figured it out as soon as I ran the tests

function doReplace() {
   if(!String(file.contents).match(search)) {
     return callback(null, null);
   }
  // ...

Swallowing the file by passing null will work to not overwrite unnecessarily, but it makes the tests fail.
We can't do that, right? I don't understand enough to figure out what to do instead.

@akaleeroy
Copy link

I worked around this issue using gulp-ignore, here is a gist:
Gulp replace only relevant files

Maybe there should be an option, passthrough all or replaced, I don't know.

@bassmanpaul
Copy link
Author

@akaleeroy Nice gist.

It's a shame a fix can't be directly incorporated into gulp-replace as I haven't got much time at the mo to try a PR :s

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

No branches or pull requests

3 participants