Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented tracking of changes to s3 assets #93

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ module.exports = function (grunt) {
port: 1337,
secure: false,
access: 'public-read',
style: 'path'
style: 'path',
trackChanges: true
},
test: {
options: {}
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion tasks/lib/s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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.
*
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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));
}
});
Expand Down
14 changes: 13 additions & 1 deletion test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}

9 changes: 4 additions & 5 deletions test/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
6 changes: 2 additions & 4 deletions test/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
2 changes: 1 addition & 1 deletion test/s3Task.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion test/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions test/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down