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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ replace: {
}
```

Here's another example using the *expand* option

```javascript
replace: {
another_example: {
src: ['build/**/*.html'],
dest: 'dist/', // filepaths will be exanded fully in this directory
replacements: [{
from: /[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{2,4}/g,
to: "<%= grunt.template.today('dd/mm/yyyy') %>"
}]
}
}
```


## API reference
Expand All @@ -75,6 +89,9 @@ name anything you like.

[multitask]: https://github.com/gruntjs/grunt/wiki/Configuring-tasks#task-configuration-and-targets

### expand

*expand* will fully expand src file paths in the destination directory

### src

Expand Down
38 changes: 30 additions & 8 deletions lib/grunt-text-replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ var grunt = require('grunt');
var path = require('path');
var gruntTextReplace = {};


exports.replace = function (settings) {
gruntTextReplace.replace(settings);
}
Expand All @@ -21,24 +20,28 @@ exports.replaceFileMultiple = function (settings) {
return gruntTextReplace.replaceFileMultiple(settings)
}



gruntTextReplace = {
replaceFileMultiple: function (settings) {

var sourceFiles = grunt.file.expand(settings.src);

sourceFiles.forEach(function (pathToSource) {
gruntTextReplace.replaceFile({
options: settings.options,
src: pathToSource,
origSrc: settings.origSrc,
dest: settings.dest,
replacements: settings.replacements
});
});
},

replaceFile: function (settings) {

var pathToSourceFile = settings.src;
var pathToDestinationFile = this.getPathToDestination(pathToSourceFile, settings.dest);
var pathToDestinationFile = this.getPathToDestination(pathToSourceFile, settings.dest, settings);
var replacements = settings.replacements;

var isThereAGenuineReplacement = replacements.reduce(function (previous, current) {
return previous || (current.from !== current.to)
}, false);
Expand Down Expand Up @@ -70,12 +73,13 @@ gruntTextReplace = {
},

replace: function (settings) {
var src = grunt.file.expand(settings.src || []);
var dest = settings.dest;
var overwrite = settings.overwrite;
var replacements = settings.replacements;
var isDestinationDirectory = (/\/$/).test(dest);
var src = isDestinationDirectory ? grunt.file.expand(settings.src || []) : settings.src;
var initialWarnCount = grunt.fail.warncount;
var options = settings.options;

if (typeof dest === 'undefined' &&
typeof src === 'undefined' &&
Expand All @@ -90,8 +94,11 @@ gruntTextReplace = {
} else if ((isDestinationDirectory === false && src.length > 1) && overwrite !== true) {
grunt.warn(gruntTextReplace.errorMessages.multipleSourceSingleDestination);
} else if (grunt.fail.warncount - initialWarnCount === 0) {

gruntTextReplace.replaceFileMultiple({
options: options,
src: src,
origSrc: settings.src,
dest: dest,
replacements: replacements
});
Expand All @@ -115,15 +122,30 @@ gruntTextReplace = {
"file, make sure there is only one source file"
},

getPathToDestination: function (pathToSource, pathToDestinationFile) {
getPathToDestination: function (pathToSource, pathToDestinationFile, settings) {
var isDestinationDirectory = (/\/$/).test(pathToDestinationFile);
var fileName = path.basename(pathToSource);
var newPathToDestination;
if (typeof pathToDestinationFile === 'undefined') {
var srcPath, omitDirs = '';

if (typeof pathToDestinationFile === 'undefined'){
newPathToDestination = pathToSource;
} else if(settings.options && settings.options.hasOwnProperty('expand') && settings.options.expand === true){

settings.origSrc.forEach(function(src){
srcPath = src.substr(0, pathToSource.lastIndexOf('/'));
srcPath.split('/').forEach(function(dir){
if(!/\*/g.test(dir)){
omitDirs += dir + '/';
}
});
});
return pathToSource.replace(omitDirs, pathToDestinationFile.toString());

} else {
newPathToDestination = pathToDestinationFile + (isDestinationDirectory ? fileName : '');
}

return newPathToDestination;
},

Expand Down Expand Up @@ -184,4 +206,4 @@ gruntTextReplace = {
return isProcessTemplateTrue ? grunt.template.process(string) : string;
}

}
}
5 changes: 2 additions & 3 deletions tasks/text-replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ var gruntTextReplace = require('../lib/grunt-text-replace');

module.exports = function(grunt) {



// Please see the grunt documentation for more information regarding task
// creation: https://github.com/gruntjs/grunt/blob/devel/docs/toc.md

Expand All @@ -23,7 +21,8 @@ module.exports = function(grunt) {
src: this.data.src,
dest: this.data.dest,
overwrite: this.data.overwrite,
replacements: this.data.replacements
replacements: this.data.replacements,
options: this.data.options
});
});
};