Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:

Expand Down
35 changes: 32 additions & 3 deletions lib/grunt-text-replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
});
},
Expand Down Expand Up @@ -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};
})()
};