Skip to content
This repository was archived by the owner on Mar 26, 2018. It is now read-only.

Commit d2d144e

Browse files
committed
Merge pull request #47 from murphybob/filename-template
Add support and test for output filename template.
2 parents 03c31b8 + debee31 commit d2d144e

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

Gruntfile.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ module.exports = function (grunt) {
4545
src: ['*'],
4646
dest: 'test/tmp/expand'
4747
},
48+
withFilenameProcessing: {
49+
options: {
50+
process: function(name, hash, extension) {
51+
return name + '-processed-' + hash + '.' + extension;
52+
}
53+
},
54+
src: ['test/fixtures/another.png'],
55+
dest: 'test/tmp'
56+
},
4857
withSummaryAttributeName: {
4958
options: {
5059
summary: 'foo'

tasks/filerev.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,18 @@ module.exports = function (grunt) {
4646
var hash = crypto.createHash(options.algorithm).update(fs.readFileSync(file)).digest('hex');
4747
var suffix = hash.slice(0, options.length);
4848
var ext = path.extname(file);
49-
var newName = [path.basename(file, ext), suffix, ext.slice(1)].join('.');
49+
var newName;
50+
51+
if (typeof options.process === 'function') {
52+
newName = options.process(path.basename(file, ext), suffix, ext.slice(1));
53+
} else {
54+
if (options.process) {
55+
grunt.log.error('options.process must be a function; ignoring');
56+
}
57+
58+
newName = [path.basename(file, ext), suffix, ext.slice(1)].join('.');
59+
}
60+
5061
var resultPath;
5162

5263
if (move) {

test/test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ var hashes = {
77
'test/fixtures/cfgfile.png' : 'test/tmp/cfgfile.da63.png',
88
'test/fixtures/math.js' : 'test/tmp/withSourceMaps/math.2f56179e.js',
99
'test/fixtures/math.js.map' : 'test/tmp/withSourceMaps/math.2f56179e.js.map',
10-
'test/fixtures/physics.js' : 'test/tmp/withSourceMaps/physics.14a0a482.js'
10+
'test/fixtures/physics.js' : 'test/tmp/withSourceMaps/physics.14a0a482.js',
11+
'test/fixtures/another.png' : 'test/tmp/another-processed-92279d3f.png'
1112
};
1213

1314
it('should revision files based on content', function () {
@@ -52,3 +53,9 @@ it('should revision .js file ok without any .map', function () {
5253
assert(revisioned === original);
5354
});
5455

56+
it('should allow a filename processing function', function () {
57+
var file = 'test/fixtures/another.png';
58+
var original = fs.statSync(file).size;
59+
var revisioned = fs.statSync(hashes[file]).size;
60+
assert(revisioned === original);
61+
});

0 commit comments

Comments
 (0)