From 20f7efd8447b50436c3704befe93d76252e8c898 Mon Sep 17 00:00:00 2001 From: PhilNehaev Date: Sun, 2 Jun 2013 19:24:39 +0400 Subject: [PATCH] After callback, passing mapping object --- Gruntfile.js | 8 ++++++++ tasks/rev.js | 11 +++++++++-- test/fixtures/mapping.txt | 0 test/rev_test.js | 8 ++++++++ 4 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/mapping.txt diff --git a/Gruntfile.js b/Gruntfile.js index a9e2b22..32bc55c 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -56,6 +56,14 @@ module.exports = function(grunt) { }, src: ['tmp/international.txt'] }, + mapping: { + options: { + after: function (mapping, options) { + grunt.file.write('tmp/mapping.json', JSON.stringify(mapping)); + } + }, + src: ['tmp/mapping.txt', 'tmp/international.txt'] + }, }, // Unit tests. diff --git a/tasks/rev.js b/tasks/rev.js index f6f77c2..81652e0 100644 --- a/tasks/rev.js +++ b/tasks/rev.js @@ -13,6 +13,7 @@ var fs = require('fs'), crypto = require('crypto'); module.exports = function(grunt) { + var _ = grunt.util._; function md5(filepath, algorithm, encoding, fileEncoding) { var hash = crypto.createHash(algorithm); @@ -27,7 +28,8 @@ module.exports = function(grunt) { encoding: 'utf8', algorithm: 'md5', length: 8 - }); + }), + mapping = {}; this.files.forEach(function(filePair) { filePair.src.forEach(function(f) { @@ -35,13 +37,18 @@ module.exports = function(grunt) { var hash = md5(f, options.algorithm, 'hex', options.encoding), prefix = hash.slice(0, options.length), renamed = [prefix, path.basename(f)].join('.'), - outPath = path.resolve(path.dirname(f), renamed); + outPath = path.join(path.dirname(f), renamed); + mapping[f] = outPath; grunt.verbose.ok().ok(hash); fs.renameSync(f, outPath); grunt.log.write(f + ' ').ok(renamed); }); + + if (_.isFunction(options.after)) { + options.after(mapping, options); + } }); }); diff --git a/test/fixtures/mapping.txt b/test/fixtures/mapping.txt new file mode 100644 index 0000000..e69de29 diff --git a/test/rev_test.js b/test/rev_test.js index b876642..ee7c7fc 100644 --- a/test/rev_test.js +++ b/test/rev_test.js @@ -49,6 +49,14 @@ exports.rev = { var exists = grunt.file.exists('tmp/faa07745.international.txt'); test.ok(exists, '8 character MD5 hash prefix for international content'); + test.done(); + }, + mapping: function(test) { + test.expect(1); + + var raw = grunt.file.read('tmp/mapping.json'); + test.equal(raw, '{"tmp/mapping.txt":"tmp/d41d8cd9.mapping.txt"}'); + test.done(); } };