diff --git a/Gruntfile.js b/Gruntfile.js index 1a56b96..9b593ad 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -17,7 +17,8 @@ module.exports = function (grunt) { port: 1337, secure: false, access: 'public-read', - style: 'path' + style: 'path', + trackChanges: true }, test: { options: {} diff --git a/README.md b/README.md index 1c956a6..3fb7008 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,7 @@ and a `dest`. Any of the above values may also be overriden. * **del** - (*array*) An array of objects, each object containing a `src` to delete from s3. Any of the above values may also be overriden. * **sync** - (*array*) An array of ojects, each oject containing a `src` and `dest`. Default behavior is to only upload new files (that don't exist). Set a key called `verify` with the value `true` on this object's options property (i.e. `options: {verify: true}`) to upload existing files if and only if they are newer than the versions of those same files on the server. This is implemented via an MD5 hash and by checking the modified times of the files. +* **trackChanges** - (*boolean*) Default `false`. If true, an array of changed assets can be retrieved from `grunt.config('s3.changed')` * **debug** - (*boolean*) If true, no transfers with S3 will occur, will print all actions for review by user * **logSuccess** - (*boolean*) If false, output for successful transfers will be ignored. Default: true * **logErrors** - (*boolean*) If false, output for failed transfers will be ignored. Default: true diff --git a/tasks/lib/s3.js b/tasks/lib/s3.js index 945978e..78fb4a8 100644 --- a/tasks/lib/s3.js +++ b/tasks/lib/s3.js @@ -79,7 +79,7 @@ exports.init = function (grunt) { /** * Create an s3 client. Returns an Knox instance. * - * @param {Object} Format. + * @param {Object} options. * @returns {Object} */ var makeClient = exports.makeClient = function(options) { @@ -88,6 +88,20 @@ exports.init = function (grunt) { ])); }; + /** + * Track a change to an asset on s3 + * + * @param {String} path of changed asset. + * @returns {Object} + */ + var trackChanges = function(path) { + var changes = grunt.config.get('s3.changed') || []; + if (changes.indexOf(path) < 0 ) { + changes.push(path); + } + grunt.config.set('s3.changed', changes); + }; + /** * Publishes the local file at src to the s3 dest. * @@ -151,6 +165,9 @@ exports.init = function (grunt) { if (remoteHash === localHash) { var msg = util.format(MSG_UPLOAD_SUCCESS, prettySrc, client.bucket, dest, localHash); + if (options.trackChanges) { + trackChanges(dest); + } cb(null, msg); } else { @@ -364,6 +381,9 @@ exports.init = function (grunt) { dfd.reject(makeError(MSG_ERR_DELETE, src, err || res.statusCode)); } else { + if (options.trackChanges) { + trackChanges(src); + } dfd.resolve(util.format(MSG_DELETE_SUCCESS, src)); } }); diff --git a/test/common.js b/test/common.js index c52c332..330ccb5 100644 --- a/test/common.js +++ b/test/common.js @@ -11,5 +11,17 @@ var common = module.exports = { clean: function(cb) { rimraf(__dirname + '/../s3', cb); + }, + + setup: function(cb) { + grunt.config.set('s3.changed', []); + common.clean(cb); + }, + + upload: function(file) { + return function(cb) { + s3.upload(__dirname + '/files/a.txt', 'a.txt', common.config).done(cb); + } } -} \ No newline at end of file +} + diff --git a/test/delete.js b/test/delete.js index ca95975..46ba5a5 100644 --- a/test/delete.js +++ b/test/delete.js @@ -13,17 +13,15 @@ var hashFile = require('../tasks/lib/common').hashFile module.exports = { setUp: function(cb) { async.series([ - common.clean, - function(done) { - s3.upload(__dirname + '/files/a.txt', 'a.txt', common.config).done(done); - } + common.setup, + common.upload('a.txt') ], function() { cb(); }); }, testDelete: function(test) { - test.expect(4); + test.expect(5); var dest = 'a.txt'; var client = s3.makeClient(config); @@ -45,6 +43,7 @@ module.exports = { client.getFile(dest, function (err, res) { test.ifError(err); test.equal(res.statusCode, 404, 'File does not exist.'); + test.deepEqual(grunt.config('s3.changed'), ['a.txt']); next(); }, 500); } diff --git a/test/download.js b/test/download.js index 7422268..a4b49d5 100644 --- a/test/download.js +++ b/test/download.js @@ -13,10 +13,8 @@ var s3Config = grunt.config("s3") module.exports = { setUp: function(cb) { async.series([ - common.clean, - function(done) { - s3.upload(__dirname + '/files/a.txt', 'a.txt', common.config).done(done); - } + common.setup, + common.upload('a.txt') ], function() { cb(); }); diff --git a/test/s3Task.js b/test/s3Task.js index 16813fe..44839bf 100644 --- a/test/s3Task.js +++ b/test/s3Task.js @@ -36,7 +36,7 @@ var makeMockTask = function (taskDef) { }; module.exports = { - setUp: common.clean, + setUp: common.setup, run: function (test) { var taskDef = new _.Deferred(); var asyncCalls = 0; diff --git a/test/sync.js b/test/sync.js index 2a21fee..73421ba 100644 --- a/test/sync.js +++ b/test/sync.js @@ -12,7 +12,7 @@ var s3Config = grunt.config("s3") , config = common.config; module.exports = { - setUp: common.clean, + setUp: common.setup, testSync: function (test) { test.expect(1); diff --git a/test/upload.js b/test/upload.js index 40addaa..5461b21 100644 --- a/test/upload.js +++ b/test/upload.js @@ -12,10 +12,10 @@ var s3Config = grunt.config("s3") , config = common.config; module.exports = { - setUp: common.clean, + setUp: common.setup, testUpload : function (test) { - test.expect(2); + test.expect(3); async.series([ function (cb) { @@ -25,6 +25,7 @@ module.exports = { s3.upload(src, 'a.txt', config) .done(function () { test.ok(hashFile(src) === hashFile(dest), 'File uploaded successfully.'); + test.deepEqual(grunt.config('s3.changed'), ['a.txt']); }) .always(function () { cb(null);