From 6b95757ece99e1dc3656e2e3abb182379ecbeb67 Mon Sep 17 00:00:00 2001 From: Gerkin Date: Sat, 22 Apr 2017 10:36:48 +0200 Subject: [PATCH 1/3] Added logs about replacements Just to display something.... Because I spent some time thinking that the gruntfile did not matched any file or pattern --- lib/grunt-text-replace.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/grunt-text-replace.js b/lib/grunt-text-replace.js index 6220456..3babd2d 100644 --- a/lib/grunt-text-replace.js +++ b/lib/grunt-text-replace.js @@ -1,6 +1,8 @@ var grunt = require('grunt'); var path = require('path'); var gruntTextReplace = {}; +var filesReplaced = 0; +var strsReplaced = 0; exports.replace = function (settings) { @@ -46,6 +48,7 @@ gruntTextReplace = { if (isReplacementRequired) { grunt.file.copy(pathToSourceFile, pathToDestinationFile, { process: function (text) { + filesReplaced++; return gruntTextReplace.replaceTextMultiple(text, replacements); } }); @@ -66,6 +69,7 @@ gruntTextReplace = { var text = settings.text; var from = this.convertPatternToRegex(settings.from); var to = this.expandReplacement(settings.to); + strsReplaced += (text.match(from) || []).length return text.replace(from, to); }, @@ -95,6 +99,7 @@ gruntTextReplace = { dest: dest, replacements: replacements }); + grunt.log.oklns("Replaced " + strsReplaced + " occurences in " + filesReplaced + " files."); } }, From 8fe85ec4aed5c13a44d70a7bc1d6963b1f15c211 Mon Sep 17 00:00:00 2001 From: Gerkin Date: Sat, 22 Apr 2017 11:24:06 +0200 Subject: [PATCH 2/3] Allow srcs to be provided the standard way Srcs can be retrieved from either config.src or config.files[..].src. This allow grunt-text-replace to support the [Files Array Format](https://gruntjs.com/configuring-tasks#files-array-format) and the [Compact Format](https://gruntjs.com/configuring-tasks#compact-format). The Files Array Format is used by https://www.npmjs.com/package/grunt-changed, so text-replace wasn't able to handle files outputed by grunt-changed --- tasks/text-replace.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tasks/text-replace.js b/tasks/text-replace.js index 84daa07..add27fb 100644 --- a/tasks/text-replace.js +++ b/tasks/text-replace.js @@ -19,8 +19,13 @@ module.exports = function(grunt) { 'General purpose text replacement for grunt. Allows you to replace ' + 'text in files using strings, regexs or functions.', function () { + var srcs = this.data.src; + if (!srcs && this.data.files) + srcs = Array.prototype.concat.apply([], this.data.files.map(function(filesHash){ + return filesHash.src + })); gruntTextReplace.replace({ - src: this.data.src, + src: srcs, dest: this.data.dest, overwrite: this.data.overwrite, replacements: this.data.replacements From 0d607e2b2ca6d590353e7984ad890c545c132168 Mon Sep 17 00:00:00 2001 From: Gerkin Date: Sun, 23 Apr 2017 01:50:11 +0200 Subject: [PATCH 3/3] Reset vars between each call --- lib/grunt-text-replace.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/grunt-text-replace.js b/lib/grunt-text-replace.js index 3babd2d..6f77a94 100644 --- a/lib/grunt-text-replace.js +++ b/lib/grunt-text-replace.js @@ -1,12 +1,14 @@ var grunt = require('grunt'); var path = require('path'); var gruntTextReplace = {}; -var filesReplaced = 0; -var strsReplaced = 0; +var filesReplaced; +var strsReplaced; exports.replace = function (settings) { gruntTextReplace.replace(settings); + filesReplaced = 0; + strsReplaced = 0; } exports.replaceText = function (settings) {