diff --git a/README.md b/README.md index 6b3efcf..2ca027d 100644 --- a/README.md +++ b/README.md @@ -157,8 +157,7 @@ replacements: [{ ### options -*options* is an object, specific to a target, and the only supported option is -*processTemplates* +*options* is an object, specific to a target, currently supports these settings: #### processTemplates @@ -186,9 +185,52 @@ replace: { } ``` - [grunt.template]: https://github.com/gruntjs/grunt/wiki/grunt.template +#### forceOverwrite + +**forceOverwrite** when set to true (by default it is false) force overwrites the +file even if the result(after replacing) is identical with the source(before replacing). + +```javascript +replace: { + forceCopy_example: { + src: ['text/*.txt'], + overwrite: true, + options: { + forceOverwrite: true + }, + replacements: [{ + from: 'foo', + to: 'bar' + }] + } +} +``` + +#### forceCopy + +**forceCopy** when set to false (by default it is true) prevents from copying when the +result(after replacing) is identical with the source(before replacing). + +```javascript +replace: { + forceCopy_example: { + src: ['text/*.txt'], + dest: 'build/text/', + options: { + forceCopy: false + }, + replacements: [{ + from: 'foo', + to: 'bar' + }] + } +} +``` + + + ## Road map Some changes I'm considering. Happy to receive suggestions for/against: diff --git a/lib/grunt-text-replace.js b/lib/grunt-text-replace.js index 4aaf51d..564f20b 100644 --- a/lib/grunt-text-replace.js +++ b/lib/grunt-text-replace.js @@ -41,7 +41,19 @@ gruntTextReplace = { var replacements = settings.replacements; grunt.file.copy(pathToSourceFile, pathToDestinationFile, { process: function (text) { - return gruntTextReplace.replaceTextMultiple(text, replacements); + var content = gruntTextReplace.replaceTextMultiple(text, replacements); + var identical = content === text; + var overwrite = pathToSourceFile === pathToDestinationFile; + var forceCopy = gruntTextReplace.forceSettings.forceCopy; + var forceOverwrite = gruntTextReplace.forceSettings.forceOverwrite; + if ( + (identical && overwrite && !forceOverwrite) || + (identical && !overwrite && !forceCopy) + ){ + return false; + }else{ + return content; + } } }); }, @@ -171,6 +183,23 @@ gruntTextReplace = { isProcessTemplateTrue = false; } return isProcessTemplateTrue ? grunt.template.process(string) : string; - } + }, -} + forceSettings: (function(){ + var forceCopy = true; + var forceOverwrite = false; + + if (grunt.task.current.data && + grunt.task.current.data.options && + grunt.task.current.data.options.forceCopy === false){ + forceCopy = false; + } + if (grunt.task.current.data && + grunt.task.current.data.options && + grunt.task.current.data.options.forceOverwrite === true){ + forceOverwrite = true; + } + + return {'forceCopy' : forceCopy, 'forceOverwrite': forceOverwrite}; + })() +};