grunt-text-replace icon indicating copy to clipboard operation
grunt-text-replace copied to clipboard

It would be nice to have the source and destination filename available to the "to" function

Open stuartf opened this issue 12 years ago • 10 comments

I'd like to do something like:

replacements: [{
  from: /foo/,
  to: function(matchedWord, index, fullText, regexMatches, sourceFile) {
    grunt.log.writeln('Found "foo" in ' + sourceFile + ' replacing with "bar".');
    return 'bar';
  }
}]

Maybe I'm missing something and this is available through something like grunt.task, but the closest I've found is grunt.task.current.file.src which gives me the unexpanded path (something like ['**/*.js']).

stuartf avatar Jan 17 '13 17:01 stuartf

Hi Stuart, thanks for your suggestion. It's a good idea, I'll try to get this for the next update. I don't believe there's a way to achieve this at present, without a refactor to the plugin itself.

One factor in the potential solution is that the number of parameters in the callback is rather high: matchedWord, index, fullText, regexMatches, pathToSource, pathToDestination, it might be best to have a single object returned, with each of these properties hanging off it. It would also be more extensible in future. It's just not backwards compatible. Will let you know when it lands. Cheers, Jonathan

yoniholmes avatar Jan 22 '13 11:01 yoniholmes

Hi Jonathan,

is there any update on this issue? Accessing the src and dst in the "to" function would be great for my current issue. I understand that this will be a breaking change, maybe an script to upgrade the gruntfile would reduce the pain.

spyro1st avatar May 24 '13 12:05 spyro1st

Based on the fork from #7 I added awareness for the "src" and "dest" via https://github.com/snostorm/grunt-text-replace/commit/d49925bea5b3be68d14fbb305af962346a0d3ddf

I wasn't confident to post it yet as a pull request as the tests weren't passing for me on the base #7 commits I started with.

snostorm avatar May 24 '13 22:05 snostorm

Hi guys, thanks for the comments. I can see how this would be helpful. I had a go at implementing this after my previous comment. Unfortunately, it required such a fundamental rewrite that it would have forced a different approach, and a rewrite of the test suite. I'm still open to the idea of getting this into a future realise, but I haven't immediate plans to add this new feature. I hope you might be able to find a work around in the meantime. If someone however is able to implement the new functionality, and pass the current test suite, then I will of course consider accepting a pull request. Many thanks, Jonathan

yoniholmes avatar May 27 '13 21:05 yoniholmes

yoniholmes, any news about approving the pull request?

e1024kb avatar Aug 08 '13 13:08 e1024kb

Hi @ebaranov. Based on your recent request, I've had a go a merging the pull request but found the merge request didn't pass the unit tests. If someone can have a go a updating the pull request so that it passes all the tests, then I'll be happy to merge this in. I expect I won't look at this until someone sends the request though, as unfortunately this isn't a feature I need myself. Many thanks & sorry for the delay in getting back to you. Cheers, Jonathan

yoniholmes avatar Aug 26 '13 08:08 yoniholmes

Thanks @yoniholmes, I think I will have a time to check it this week.

e1024kb avatar Aug 26 '13 08:08 e1024kb

So, what's news?

pfrankov avatar Dec 17 '14 06:12 pfrankov

+1, I need this feature to solve my current issue where I have to apply different operations based on the source file matched by the regex. I'll use seanzarrin's "passSourceFile" branch for now. Thanks!

running-coder avatar Aug 17 '15 20:08 running-coder

Same problem here. This thread is more than 2 years old, and this is still no implemented.

Skwal avatar Aug 18 '15 14:08 Skwal