From 98cabd2256ecb42b476bf25b1461d91c34222e78 Mon Sep 17 00:00:00 2001 From: Beni Date: Thu, 29 Oct 2015 15:27:22 +0100 Subject: [PATCH] add callback options for change, nochange and new after file got uploaded to S3 --- Readme.md | 21 +++++++++++++++++++++ index.js | 17 +++++++++++++---- src/helper.js | 3 +++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/Readme.md b/Readme.md index b18f1bd..e4e171e 100644 --- a/Readme.md +++ b/Readme.md @@ -142,6 +142,27 @@ Use this to transform your file names before they're uploaded to your S3 bucket. Type: `object` + `function` + +#### onChange + +Type: `function` + +This function gets called with the S3 keyname as the first parameter if the uploaded file resulted in a change. + + +#### onNoChange + +Type: `function` + +This function gets called with the S3 keyname as the first parameter if the uploaded file did not result in a change. + + +#### onNew + +Type: `function` + +This function gets called with the S3 keyname as the first parameter if the uploaded file is a new file in the bucket. + **NEW IN 1.3** Upon reviewing an issue with `metadataMap` and `manualContentEncoding`, a standard method for mapping each `s3.putObject` param was created. For now, `metadataMap` and `manualContentEncoding` are still available, but they will be depricated in the next major version (2.0). diff --git a/index.js b/index.js index a35d5d9..36dcc7c 100644 --- a/index.js +++ b/index.js @@ -108,7 +108,7 @@ gulpPrefixer = function (AWS) { // === manualContentEncoding =========================== // Similar to metadataMap to put global / individual - // headers on each file object (only if + // headers on each file object (only if // options.ContentEncoding) is undefined. (1.2) // ** WILL DEPRICATE IN 2.0.0 ** @@ -118,8 +118,8 @@ gulpPrefixer = function (AWS) { options.ContentEncoding = options.manualContentEncoding; } - // Check the file that's up in the bucket already - + // Check the file that's up in the bucket already + _s3.headObject({ 'Bucket': the_bucket, 'Key': keyname @@ -159,7 +159,7 @@ gulpPrefixer = function (AWS) { objOpts.Bucket = the_bucket; objOpts.Key = keyname; objOpts.Body = file.contents; - + if(mimetype.length) { // A check in case of map ContentType objOpts.ContentType = mimetype; @@ -207,12 +207,21 @@ gulpPrefixer = function (AWS) { if (head_data) { if (head_data.ETag !== data.ETag) { gutil.log(gutil.colors.yellow("Updated ....... "), keyname); + if (options.onChange && typeof options.onChange === 'function') { + options.onChange.call(this, keyname); + } } else { gutil.log(gutil.colors.gray("No Change ..... "), keyname); + if (options.onNoChange && typeof options.onNoChange === 'function') { + options.onNoChange.call(this, keyname); + } } } else { // Doesn't exist in bucket; the object is new to the bucket gutil.log(gutil.colors.green("Uploaded! ..... "), keyname); + if (options.onNew && typeof options.onNew === 'function') { + options.onNew.call(this, keyname); + } } callback(null); diff --git a/src/helper.js b/src/helper.js index 6b77c5e..e8a53c8 100644 --- a/src/helper.js +++ b/src/helper.js @@ -24,6 +24,9 @@ module.exports = { 'maps', 'mimeTypeLookup', 'nameTransform', + 'onChange', + 'onNoChange', + 'onNew', 'uploadNewFilesOnly', 'verbose' ];