From a4ed0fc8768e9c45a03c7c148f5596eb4c3c8a6a Mon Sep 17 00:00:00 2001 From: abetomo Date: Wed, 28 Jun 2017 14:05:26 +0900 Subject: [PATCH 1/2] Refactoring _listEventSourceMappings Modify that _listEventSourceMappings returns Promise --- lib/main.js | 26 +++++++++++++------------- test/main.js | 14 ++++++++++++++ 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/lib/main.js b/lib/main.js index bde0a895..315c684c 100644 --- a/lib/main.js +++ b/lib/main.js @@ -567,13 +567,15 @@ Lambda.prototype._buildAndArchive = function (program, archiveCallback) { }) } -Lambda.prototype._listEventSourceMappings = function (lambda, params, cb) { - return lambda.listEventSourceMappings(params, function (err, data) { - var eventSourceMappings = [] - if (!err && data && data.EventSourceMappings) { - eventSourceMappings = data.EventSourceMappings - } - return cb(err, eventSourceMappings) +Lambda.prototype._listEventSourceMappings = (lambda, params) => { + return new Promise((resolve, reject) => { + lambda.listEventSourceMappings(params, (err, data) => { + if (err) return reject(err) + if (data && data.EventSourceMappings) { + return resolve(data.EventSourceMappings) + } + return resolve([]) + }) }) } @@ -768,11 +770,9 @@ Lambda.prototype._deployToRegion = function (program, params, region) { 'FunctionName': params.FunctionName }).promise().then(() => { // Function exists - _this._listEventSourceMappings(lambda, { + return _this._listEventSourceMappings(lambda, { 'FunctionName': params.FunctionName - }, (err, existingEventSourceList) => { - if (err) return reject(err) - + }).then((existingEventSourceList) => { return Promise.all([ _this._uploadExisting(lambda, params).then((results) => { console.log('=> Zip file(s) done uploading. Results follow: ') @@ -791,9 +791,9 @@ Lambda.prototype._deployToRegion = function (program, params, region) { ) ]).then((results) => { resolve(results) - }).catch((err) => { - reject(err) }) + }).catch((err) => { + reject(err) }) }).catch(() => { // Function does not exist diff --git a/test/main.js b/test/main.js index 4314d992..5c51f4f7 100644 --- a/test/main.js +++ b/test/main.js @@ -800,6 +800,20 @@ describe('lib/main', function () { }) }) + describe('_listEventSourceMappings', () => { + it('simple test with mock', () => { + return lambda._listEventSourceMappings( + awsLambda, + { FunctionName: 'test-func' } + ).then((results) => { + assert.deepEqual( + results, + lambdaMockSettings.listEventSourceMappings.EventSourceMappings + ) + }) + }) + }) + describe('_updateEventSources', () => { const eventSourcesJsonValue = { EventSourceMappings: [{ From 58e7f2c5b7df67603ca37076de44ef648087777d Mon Sep 17 00:00:00 2001 From: abetomo Date: Wed, 28 Jun 2017 14:06:18 +0900 Subject: [PATCH 2/2] Remove unnecessary catch --- lib/main.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/main.js b/lib/main.js index 315c684c..c10751a8 100644 --- a/lib/main.js +++ b/lib/main.js @@ -815,8 +815,6 @@ Lambda.prototype._deployToRegion = function (program, params, region) { ) ]).then((results) => { resolve(results) - }).catch((err) => { - reject(err) }) }).catch((err) => { reject(err)