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

Support for changing CWD + respecting source path #7

Closed
wants to merge 2 commits into from
Closed
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
18 changes: 14 additions & 4 deletions lib/grunt-text-replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,19 @@ exports.replaceFileMultiple = function (settings) {

gruntTextReplace = {
replace: function (settings) {
var src = grunt.file.expand(settings.src || []);
var src = settings.src || '';
var dest = settings.dest;
var cwd = settings.cwd;
Copy link
Owner

Choose a reason for hiding this comment

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

Hi there. Thanks again for submitting your pull request. This review is coming quite late, but was spurred on by a recent request. A minor point, but for legibility reasons I'd opt for 'currentWorkingDirectory' instead of 'cwd'. As we 'previousWorkingDirectory'. Cheers & many thanks, Jonathan

var prevCwd;
var overwrite = settings.overwrite;
var replacements = settings.replacements;
var isDestinationDirectory = (/\/$/).test(dest);
var initialWarnCount = grunt.fail.warncount;

if(cwd) {
prevCwd = process.cwd();
grunt.file.setBase(cwd);
}

if (typeof dest === 'undefined' &&
typeof src === 'undefined' &&
Expand All @@ -53,6 +60,10 @@ gruntTextReplace = {
replacements: replacements
});
}

if(prevCwd) {
grunt.file.setBase(prevCwd);
}
},

errorMessages: {
Expand Down Expand Up @@ -114,12 +125,11 @@ gruntTextReplace = {

getPathToDestination: function (pathToSource, pathToDestinationFile) {
var isDestinationDirectory = (/\/$/).test(pathToDestinationFile);
var fileName = path.basename(pathToSource);
var newPathToDestination;
if (typeof pathToDestinationFile === 'undefined') {
newPathToDestination = pathToSource;
} else {
newPathToDestination = pathToDestinationFile + (isDestinationDirectory ? fileName : '');
newPathToDestination = pathToDestinationFile + (isDestinationDirectory ? pathToSource : '');
}
return newPathToDestination;
},
Expand Down Expand Up @@ -172,4 +182,4 @@ gruntTextReplace = {
return isProcessTemplateTrue ? grunt.template.process(string) : string;
}

}
}
1 change: 1 addition & 0 deletions tasks/text-replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = function(grunt) {
gruntTextReplace.replace({
src: this.data.src,
dest: this.data.dest,
cwd: this.data.cwd,
overwrite: this.data.overwrite,
replacements: this.data.replacements
});
Expand Down