From 32b8db9a573814ce07ad25ae1db96ead56d0c700 Mon Sep 17 00:00:00 2001 From: abetomo Date: Fri, 9 Jun 2017 19:18:05 +0900 Subject: [PATCH 1/4] Replace async with Promise --- lib/main.js | 33 ++++++++++++++-------- test/main.js | 78 ++++++++++++++++++++++++++++++---------------------- 2 files changed, 66 insertions(+), 45 deletions(-) diff --git a/lib/main.js b/lib/main.js index 45716deb..c1b9e41e 100644 --- a/lib/main.js +++ b/lib/main.js @@ -668,29 +668,38 @@ Lambda.prototype._updateEventSources = function (lambda, functionName, existingE } } - return async.map(updateEventSourceList, function (updateEventSource, _cb) { + return Promise.all(updateEventSourceList.map((updateEventSource) => { switch (updateEventSource['type']) { case 'create': delete updateEventSource['type'] - lambda.createEventSourceMapping(updateEventSource, function (err, data) { - return _cb(err, data) + return new Promise((resolve, reject) => { + lambda.createEventSourceMapping(updateEventSource, (err, data) => { + if (err) return reject(err) + resolve(data) + }) }) - break case 'update': delete updateEventSource['type'] - lambda.updateEventSourceMapping(updateEventSource, function (err, data) { - return _cb(err, data) + return new Promise((resolve, reject) => { + lambda.updateEventSourceMapping(updateEventSource, (err, data) => { + if (err) return reject(err) + resolve(data) + }) }) - break case 'delete': delete updateEventSource['type'] - lambda.deleteEventSourceMapping(updateEventSource, function (err, data) { - return _cb(err, data) + return new Promise((resolve, reject) => { + lambda.deleteEventSourceMapping(updateEventSource, (err, data) => { + if (err) return reject(err) + resolve(data) + }) }) - break } - }, function (err, results) { - return cb(err, results) + return Promise.resolve() + })).then((data) => { + cb(null, data) + }).catch((err) => { + cb(err) }) } diff --git a/test/main.js b/test/main.js index a34a235f..54283bee 100644 --- a/test/main.js +++ b/test/main.js @@ -874,50 +874,62 @@ describe('lib/main', function () { ) }) - it('simple test with mock (In case of new addition)', (done) => { + it('simple test with mock (In case of new addition)', () => { program.eventSourceFile = 'event_sources.json' const eventSourceList = lambda._eventSourceList(program) - lambda._updateEventSources( - awsLambda, - 'functionName', - [], - eventSourceList.EventSourceMappings, - (err, results) => { - assert.isUndefined(err) - assert.deepEqual(results, [lambdaMockSettings.createEventSourceMapping]) - done() + return new Promise((resolve) => { + lambda._updateEventSources( + awsLambda, + 'functionName', + [], + eventSourceList.EventSourceMappings, + (err, results) => resolve({ err: err, results: results }) + ) + }).then((actual) => { + const expected = { + err: null, + results: [lambdaMockSettings.createEventSourceMapping] } - ) + assert.deepEqual(actual, expected) + }) }) - it('simple test with mock (In case of deletion)', (done) => { - lambda._updateEventSources( - awsLambda, - 'functionName', - lambdaMockSettings.listEventSourceMappings.EventSourceMappings, - {}, - (err, results) => { - assert.isUndefined(err) - assert.deepEqual(results, [lambdaMockSettings.deleteEventSourceMapping]) - done() + it('simple test with mock (In case of deletion)', () => { + return new Promise((resolve) => { + lambda._updateEventSources( + awsLambda, + 'functionName', + lambdaMockSettings.listEventSourceMappings.EventSourceMappings, + {}, + (err, results) => resolve({ err: err, results: results }) + ) + }).then((actual) => { + const expected = { + err: null, + results: [lambdaMockSettings.deleteEventSourceMapping] } - ) + assert.deepEqual(actual, expected) + }) }) - it('simple test with mock (In case of update)', (done) => { + it('simple test with mock (In case of update)', () => { program.eventSourceFile = 'event_sources.json' const eventSourceList = lambda._eventSourceList(program) - lambda._updateEventSources( - awsLambda, - 'functionName', - lambdaMockSettings.listEventSourceMappings.EventSourceMappings, - eventSourceList.EventSourceMappings, - (err, results) => { - assert.isUndefined(err) - assert.deepEqual(results, [lambdaMockSettings.updateEventSourceMapping]) - done() + return new Promise((resolve) => { + lambda._updateEventSources( + awsLambda, + 'functionName', + lambdaMockSettings.listEventSourceMappings.EventSourceMappings, + eventSourceList.EventSourceMappings, + (err, results) => resolve({ err: err, results: results }) + ) + }).then((actual) => { + const expected = { + err: null, + results: [lambdaMockSettings.updateEventSourceMapping] } - ) + assert.deepEqual(actual, expected) + }) }) }) From 99641807bb6bd12a0c1c2fb0e0577ac2bb43dc02 Mon Sep 17 00:00:00 2001 From: abetomo Date: Fri, 9 Jun 2017 19:19:20 +0900 Subject: [PATCH 2/4] Modify to arrow function --- lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/main.js b/lib/main.js index c1b9e41e..0a82310c 100644 --- a/lib/main.js +++ b/lib/main.js @@ -614,7 +614,7 @@ Lambda.prototype._listEventSourceMappings = function (lambda, params, cb) { }) } -Lambda.prototype._updateEventSources = function (lambda, functionName, existingEventSourceList, eventSourceList, cb) { +Lambda.prototype._updateEventSources = (lambda, functionName, existingEventSourceList, eventSourceList, cb) => { if (eventSourceList == null) { return cb(null, []) } From bdacf9766aca94fd42cb6e24126dc48eef4bf2be Mon Sep 17 00:00:00 2001 From: abetomo Date: Fri, 9 Jun 2017 19:19:34 +0900 Subject: [PATCH 3/4] Modify from var to const --- lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/main.js b/lib/main.js index 0a82310c..808b7ea5 100644 --- a/lib/main.js +++ b/lib/main.js @@ -618,7 +618,7 @@ Lambda.prototype._updateEventSources = (lambda, functionName, existingEventSourc if (eventSourceList == null) { return cb(null, []) } - var updateEventSourceList = [] + const updateEventSourceList = [] // Checking new and update event sources for (let i in eventSourceList) { let isExisting = false From 9971004c8cee5ac09784f31631400b7b6a7c2d0b Mon Sep 17 00:00:00 2001 From: abetomo Date: Mon, 19 Jun 2017 10:22:01 +0900 Subject: [PATCH 4/4] Improve deploy process with Promise Reduced nesting in the Promise chain --- lib/main.js | 56 ++++++++++++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/lib/main.js b/lib/main.js index f51e341f..d2f2dbeb 100644 --- a/lib/main.js +++ b/lib/main.js @@ -805,35 +805,7 @@ Lambda.prototype._deployToRegion = function (program, params, region) { // Checking function return lambda.getFunction({ 'FunctionName': params.FunctionName - }, (err) => { - if (err) { - // Function does not exist - return _this._uploadNew(lambda, params).then((results) => { - console.log('=> Zip file(s) done uploading. Results follow: ') - console.log(results) - - return Promise.all([ - _this._updateEventSources( - lambda, - params.FunctionName, - [], - eventSourceList.EventSourceMappings - ), - _this._updateScheduleEvents( - scheduleEvents, - results.FunctionArn, - eventSourceList.ScheduleEvents - ) - ]).then((results) => { - resolve(results) - }).catch((err) => { - reject(err) - }) - }).catch((err) => { - reject(err) - }) - } - + }).promise().then(() => { // Function exists _this._listEventSourceMappings(lambda, { 'FunctionName': params.FunctionName @@ -862,6 +834,32 @@ Lambda.prototype._deployToRegion = function (program, params, region) { reject(err) }) }) + }).catch(() => { + // Function does not exist + return _this._uploadNew(lambda, params).then((results) => { + console.log('=> Zip file(s) done uploading. Results follow: ') + console.log(results) + + return Promise.all([ + _this._updateEventSources( + lambda, + params.FunctionName, + [], + eventSourceList.EventSourceMappings + ), + _this._updateScheduleEvents( + scheduleEvents, + results.FunctionArn, + eventSourceList.ScheduleEvents + ) + ]).then((results) => { + resolve(results) + }).catch((err) => { + reject(err) + }) + }).catch((err) => { + reject(err) + }) }) }) }