From cf7acca66fd02d2d82d044cf50c2b8d68def49d8 Mon Sep 17 00:00:00 2001 From: Ben Alman Date: Thu, 6 Mar 2014 12:34:01 -0500 Subject: [PATCH] Add expandMapping .extDot option. Closes gh-979. * Can be 'first' or 'last' but defaults to 'first'. --- lib/grunt/file.js | 10 +++++++++- test/grunt/file_test.js | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/grunt/file.js b/lib/grunt/file.js index dd486410b..1abbebf89 100644 --- a/lib/grunt/file.js +++ b/lib/grunt/file.js @@ -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); } @@ -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); diff --git a/test/grunt/file_test.js b/test/grunt/file_test.js index b72a62a66..27493191f 100644 --- a/test/grunt/file_test.js +++ b/test/grunt/file_test.js @@ -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'});