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

Added support for destination to be a function #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ It supports [minimatch][minimatch] paths.

- file: `'path/output.txt'`
- directory: `'path/'`
- function: ```function (input) {
return input;
}```

grunt-text-replace will throw an error if multiple source files are mapped to
a single file.
Expand Down
6 changes: 5 additions & 1 deletion lib/grunt-text-replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ gruntTextReplace = {
var dest = settings.dest;
var overwrite = settings.overwrite;
var replacements = settings.replacements;
var isDestinationDirectory = (/\/$/).test(dest);
var isDestinationDirectory = (/\/$/).test(dest) || typeof dest === 'function';
var initialWarnCount = grunt.fail.warncount;

if (typeof dest === 'undefined' &&
Expand Down Expand Up @@ -116,6 +116,10 @@ gruntTextReplace = {
},

getPathToDestination: function (pathToSource, pathToDestinationFile) {
if (typeof pathToDestinationFile === 'function') {
pathToDestinationFile = pathToDestinationFile(pathToSource);
}

var isDestinationDirectory = (/\/$/).test(pathToDestinationFile);
var fileName = path.basename(pathToSource);
var newPathToDestination;
Expand Down
14 changes: 14 additions & 0 deletions test/text-replace-unit-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,20 @@ exports.textReplace = {
test.equal(replacedTextA, 'Hello planet');
test.equal(replacedTextB, 'Hello planet');
test.done();
},

'Test function as destination': function (test) {
var originalText, replacedTextA, replacedTextB;
originalText = grunt.file.read('test/temp/testA.txt');
replaceFileMultiple(['test/temp/test*.txt'], function (input) {
return input + '.foo';
}, [{from: 'world', to: 'planet'}]);
replacedTextA = grunt.file.read('test/temp/testA.txt.foo');
replacedTextB = grunt.file.read('test/temp/testB.txt.foo');
test.equal(originalText, 'Hello world');
test.equal(replacedTextA, 'Hello planet');
test.equal(replacedTextB, 'Hello planet');
test.done();
}

}
Expand Down