From c0382957a0729f9de2defbc76a2dde8e0b204bbb Mon Sep 17 00:00:00 2001 From: abetomo Date: Wed, 14 Jun 2017 11:02:12 +0900 Subject: [PATCH 1/4] Fix to add awsMock.setSDK Because `npm test test/main.js` fails --- test/main.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/main.js b/test/main.js index 985932d3..5cc0518a 100644 --- a/test/main.js +++ b/test/main.js @@ -7,6 +7,7 @@ const lambda = require(path.join(__dirname, '..', 'lib', 'main')) const Zip = require('node-zip') const assert = require('chai').assert const awsMock = require('aws-sdk-mock') +awsMock.setSDK(path.resolve('node_modules/aws-sdk')) const originalProgram = { environment: 'development', From 4d53da1acaf5a0516fc43656da7e9f22a7209843 Mon Sep 17 00:00:00 2001 From: abetomo Date: Wed, 14 Jun 2017 11:04:46 +0900 Subject: [PATCH 2/4] Fix to return Promise --- lib/main.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/main.js b/lib/main.js index 18b41a04..b01a4434 100644 --- a/lib/main.js +++ b/lib/main.js @@ -616,7 +616,7 @@ Lambda.prototype._listEventSourceMappings = function (lambda, params, cb) { Lambda.prototype._updateEventSources = (lambda, functionName, existingEventSourceList, eventSourceList, cb) => { if (eventSourceList == null) { - return cb(null, []) + return new Promise(resolve => cb(null, [])) } const updateEventSourceList = [] // Checking new and update event sources @@ -705,14 +705,14 @@ Lambda.prototype._updateEventSources = (lambda, functionName, existingEventSourc Lambda.prototype._updateScheduleEvents = (scheduleEvents, functionArn, scheduleList, cb) => { if (scheduleList == null) { - return cb(null, []) + return new Promise(resolve => cb(null, [])) } const paramsList = scheduleList.map((schedule) => Object.assign(schedule, { FunctionArn: functionArn })) // series - paramsList.map((params) => { + return paramsList.map((params) => { return scheduleEvents.add(params) }).reduce((a, b) => { return a.then(b) From caac6bcb227b5a584401af9c39eed63f75ff1f7d Mon Sep 17 00:00:00 2001 From: abetomo Date: Wed, 14 Jun 2017 11:23:23 +0900 Subject: [PATCH 3/4] Replace async.parallel with Promise.all --- lib/main.js | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/lib/main.js b/lib/main.js index b01a4434..7d28573a 100644 --- a/lib/main.js +++ b/lib/main.js @@ -812,7 +812,7 @@ Lambda.prototype.deploy = function (program) { // Checking function return lambda.getFunction({ 'FunctionName': params.FunctionName - }, function (err) { + }, (err) => { if (err) { // Function does not exist return _this._uploadNew(lambda, params, function (err, results) { @@ -822,20 +822,36 @@ Lambda.prototype.deploy = function (program) { console.log('=> Zip file(s) done uploading. Results follow: ') console.log(results) - async.parallel([ - function (_callback) { - // Updating event source(s) - _this._updateEventSources(lambda, params.FunctionName, [], eventSourceList.EventSourceMappings, function (err, results) { - _callback(err, results) - }) - }, - function (_callback) { - _this._updateScheduleEvents(scheduleEvents, results.FunctionArn, eventSourceList.ScheduleEvents, function (err, results) { - _callback(err, results) - }) - } - ], function (err, results) { - cb(err, results) + // This code is on its way to Promise. + // From now on, callback will not be used. + return Promise.all([ + new Promise((resolve, reject) => { + _this._updateEventSources( + lambda, + params.FunctionName, + [], + eventSourceList.EventSourceMappings, + (err, results) => { + if (err) return reject(err) + resolve(results) + } + ) + }), + new Promise((resolve, reject) => { + _this._updateScheduleEvents( + scheduleEvents, + results.FunctionArn, + eventSourceList.ScheduleEvents, + (err, results) => { + if (err) return reject(err) + resolve(results) + } + ) + }) + ]).then((results) => { + cb(null, results) + }).catch((err) => { + cb(err) }) }) } From 7bf14dae7a7457c5aac6552147ac594152f80da7 Mon Sep 17 00:00:00 2001 From: abetomo Date: Wed, 14 Jun 2017 11:40:31 +0900 Subject: [PATCH 4/4] Replace async.parallel with Promise.all --- lib/main.js | 48 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/lib/main.js b/lib/main.js index 7d28573a..4d7b6014 100644 --- a/lib/main.js +++ b/lib/main.js @@ -859,30 +859,48 @@ Lambda.prototype.deploy = function (program) { // Function exists _this._listEventSourceMappings(lambda, { 'FunctionName': params.FunctionName - }, function (err, existingEventSourceList) { + }, (err, existingEventSourceList) => { if (err) { throw err } - return async.parallel([ - function (_callback) { - _this._uploadExisting(lambda, params, function (err, results) { + + // This code is on its way to Promise. + // From now on, callback will not be used. + return Promise.all([ + new Promise((resolve, reject) => { + _this._uploadExisting(lambda, params, (err, results) => { if (err) { throw err } console.log('=> Zip file(s) done uploading. Results follow: ') console.log(results) - _this._updateScheduleEvents(scheduleEvents, results.FunctionArn, eventSourceList.ScheduleEvents, function (err, results) { - _callback(err, results) - }) - }) - }, - function (_callback) { - _this._updateEventSources(lambda, params.FunctionName, existingEventSourceList, eventSourceList.EventSourceMappings, function (err, results) { - _callback(err, results) + _this._updateScheduleEvents( + scheduleEvents, + results.FunctionArn, + eventSourceList.ScheduleEvents, + (err, results) => { + if (err) return reject(err) + resolve(results) + } + ) }) - } - ], function (err, results) { - cb(err, results) + }), + new Promise((resolve, reject) => { + _this._updateEventSources( + lambda, + params.FunctionName, + existingEventSourceList, + eventSourceList.EventSourceMappings, + (err, results) => { + if (err) return reject(err) + resolve(results) + } + ) + }) + ]).then((results) => { + cb(null, results) + }).catch((err) => { + cb(err) }) }) })