From d49925bea5b3be68d14fbb305af962346a0d3ddf Mon Sep 17 00:00:00 2001 From: Sean Ouimet Date: Fri, 24 May 2013 16:34:39 -0600 Subject: [PATCH] Passes path information along to the replace function --- lib/grunt-text-replace.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/grunt-text-replace.js b/lib/grunt-text-replace.js index d1c26f5..39c88d5 100644 --- a/lib/grunt-text-replace.js +++ b/lib/grunt-text-replace.js @@ -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); }, @@ -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}); } }); }, @@ -146,15 +147,15 @@ 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]; @@ -162,7 +163,7 @@ gruntTextReplace = { 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; };