Skip to content

Commit

Permalink
Add expandMapping .extDot option. Closes gruntjsgh-979.
Browse files Browse the repository at this point in the history
* Can be 'first' or 'last' but defaults to 'first'.
  • Loading branch information
cowboy authored and jamesplease committed Mar 12, 2014
1 parent e92b242 commit cf7acca
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/grunt/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,17 @@ file.expand = function() {

var pathSeparatorRe = /[\/\\]/g;

// The "ext" option refers to either everything after the first dot (default)
// or everything after the last dot.
var extDotRe = {
first: /(\.[^\/]*)?$/,
last: /(\.[^\/\.]*)?$/,
};

// Build a multi task "files" object dynamically.
file.expandMapping = function(patterns, destBase, options) {
options = grunt.util._.defaults({}, options, {
extDot: 'first',
rename: function(destBase, destPath) {
return path.join(destBase || '', destPath);
}
Expand All @@ -151,7 +159,7 @@ file.expandMapping = function(patterns, destBase, options) {
}
// Change the extension?
if (options.ext) {
destPath = destPath.replace(/(\.[^\/]*)?$/, options.ext);
destPath = destPath.replace(extDotRe[options.extDot], options.ext);
}
// Generate destination filename.
var dest = options.rename(destBase, destPath, options);
Expand Down
22 changes: 22 additions & 0 deletions test/grunt/file_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,28 @@ exports['file.expandMapping'] = {
test.deepEqual(actual, expected, 'specified extension should be added');
test.done();
},
'extDot': function(test) {
test.expect(2);
var actual, expected;

actual = grunt.file.expandMapping(['expand-mapping-ext/**/file*'], 'dest', {ext: '.foo', extDot: 'first'});
expected = [
{dest: 'dest/expand-mapping-ext/dir.ectory/file-no-extension.foo', src: ['expand-mapping-ext/dir.ectory/file-no-extension']},
{dest: 'dest/expand-mapping-ext/dir.ectory/sub.dir.ectory/file.foo', src: ['expand-mapping-ext/dir.ectory/sub.dir.ectory/file.ext.ension']},
{dest: 'dest/expand-mapping-ext/file.foo', src: ['expand-mapping-ext/file.ext.ension']},
];
test.deepEqual(actual, expected, 'extDot of "first" should replace everything after the first dot in the filename.');

actual = grunt.file.expandMapping(['expand-mapping-ext/**/file*'], 'dest', {ext: '.foo', extDot: 'last'});
expected = [
{dest: 'dest/expand-mapping-ext/dir.ectory/file-no-extension.foo', src: ['expand-mapping-ext/dir.ectory/file-no-extension']},
{dest: 'dest/expand-mapping-ext/dir.ectory/sub.dir.ectory/file.ext.foo', src: ['expand-mapping-ext/dir.ectory/sub.dir.ectory/file.ext.ension']},
{dest: 'dest/expand-mapping-ext/file.ext.foo', src: ['expand-mapping-ext/file.ext.ension']},
];
test.deepEqual(actual, expected, 'extDot of "last" should replace everything after the last dot in the filename.');

test.done();
},
'cwd': function(test) {
test.expect(1);
var actual = grunt.file.expandMapping(['**/*.txt'], 'dest', {cwd: 'expand'});
Expand Down

0 comments on commit cf7acca

Please sign in to comment.