From a84060217d124c7061ceb11b398f1280b6b31e6e Mon Sep 17 00:00:00 2001 From: old9 Date: Fri, 8 Nov 2013 13:48:03 +0800 Subject: [PATCH 1/2] Adds options to control overwiting behaviors. forceCopy, defaults to true. forceOverwrite, defaults to false. --- README.md | 48 ++++++++++++++++++++++++++++++++++++--- lib/grunt-text-replace.js | 35 +++++++++++++++++++++++++--- 2 files changed, 77 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 6b3efcf..10b6478 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: { + foceCopy_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: { + foceCopy_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}; + })() +}; From 362ed8b5c600a6a54db09a937b8b962bc5886121 Mon Sep 17 00:00:00 2001 From: old9 Date: Fri, 17 Jan 2014 09:14:53 +0800 Subject: [PATCH 2/2] fix typo --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 10b6478..2ca027d 100644 --- a/README.md +++ b/README.md @@ -194,7 +194,7 @@ file even if the result(after replacing) is identical with the source(before rep ```javascript replace: { - foceCopy_example: { + forceCopy_example: { src: ['text/*.txt'], overwrite: true, options: { @@ -215,7 +215,7 @@ result(after replacing) is identical with the source(before replacing). ```javascript replace: { - foceCopy_example: { + forceCopy_example: { src: ['text/*.txt'], dest: 'build/text/', options: {