Skip to content

Commit

Permalink
Passes path information along to the replace function
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean Ouimet committed May 24, 2013
1 parent 9799f07 commit d49925b
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions lib/grunt-text-replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,17 @@ gruntTextReplace = {
replaceText: function (settings) {
var text = settings.text;
var from = this.convertPatternToRegex(settings.from);
var to = this.expandReplacement(settings.to);
var to = this.expandReplacement(settings.to, settings.paths);
return text.replace(from, to);
},

replaceTextMultiple: function (text, replacements) {
replaceTextMultiple: function (text, replacements, paths) {
return replacements.reduce(function (newText, replacement) {
return gruntTextReplace.replaceText({
text: newText,
from: replacement.from,
to: replacement.to
to: replacement.to,
paths: paths
});
}, text);
},
Expand All @@ -107,7 +108,7 @@ gruntTextReplace = {
var replacements = settings.replacements;
grunt.file.copy(pathToSourceFile, pathToDestinationFile, {
process: function (text) {
return gruntTextReplace.replaceTextMultiple(text, replacements);
return gruntTextReplace.replaceTextMultiple(text, replacements, {src: pathToSourceFile, dest: pathToDestinationFile});
}
});
},
Expand Down Expand Up @@ -146,23 +147,23 @@ gruntTextReplace = {
return pattern;
},

expandReplacement: function (replacement) {
expandReplacement: function (replacement, paths) {
if (typeof replacement === 'function') {
return this.expandFunctionReplacement(replacement);
return this.expandFunctionReplacement(replacement, paths);
} else if (typeof replacement === 'string') {
return this.expandStringReplacement(replacement);
}
},

expandFunctionReplacement: function (replacement) {
expandFunctionReplacement: function (replacement, paths) {
return function () {
var matchedSubstring = arguments[0];
var index = arguments[arguments.length - 2];
var fullText = arguments[arguments.length - 1];
var regexMatches = Array.prototype.slice.call(arguments, 1,
arguments.length - 2);
var returnValue = replacement(matchedSubstring, index, fullText,
regexMatches);
regexMatches, paths);
return (typeof returnValue === 'string') ?
gruntTextReplace.processGruntTemplate(returnValue) : returnValue;
};
Expand Down

1 comment on commit d49925b

@snostorm
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A step in implementing yoniholmes#2 (to fill my own needs for now). Not tested.

Please sign in to comment.