Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds options to control overwiting behaviors. #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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: {
foceCopy_example: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a typo in the "force" word

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:

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};
})()
};