From 32b8db9a573814ce07ad25ae1db96ead56d0c700 Mon Sep 17 00:00:00 2001 From: abetomo Date: Fri, 9 Jun 2017 19:18:05 +0900 Subject: [PATCH 01/12] 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 02/12] 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 03/12] 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 4be72088cf8138d0b4a05bc6a131f25009960187 Mon Sep 17 00:00:00 2001 From: abetomo Date: Wed, 14 Jun 2017 21:10:22 +0900 Subject: [PATCH 04/12] Modify _uploadExisting returns Promise --- lib/main.js | 63 ++++++++++++++++++++++++++-------------------------- test/main.js | 6 ++--- 2 files changed, 33 insertions(+), 36 deletions(-) diff --git a/lib/main.js b/lib/main.js index 4d7b6014..6b0c00e4 100644 --- a/lib/main.js +++ b/lib/main.js @@ -465,38 +465,37 @@ Lambda.prototype._setRunTimeEnvironmentVars = function (program) { } Lambda.prototype._uploadExisting = (lambda, params, cb) => { - const request = lambda.updateFunctionCode({ - 'FunctionName': params.FunctionName, - 'ZipFile': params.Code.ZipFile, - 'Publish': params.Publish - }, (err, data) => { - if (err) { - return cb(err, data) - } - - return lambda.updateFunctionConfiguration({ + return new Promise((resolve, reject) => { + const request = lambda.updateFunctionCode({ 'FunctionName': params.FunctionName, - 'Description': params.Description, - 'Handler': params.Handler, - 'MemorySize': params.MemorySize, - 'Role': params.Role, - 'Timeout': params.Timeout, - 'Runtime': params.Runtime, - 'VpcConfig': params.VpcConfig, - 'Environment': params.Environment, - 'DeadLetterConfig': params.DeadLetterConfig, - 'TracingConfig': params.TracingConfig - }, (err, data) => { - return cb(err, data) + 'ZipFile': params.Code.ZipFile, + 'Publish': params.Publish + }, (err) => { + if (err) return reject(err) + + lambda.updateFunctionConfiguration({ + 'FunctionName': params.FunctionName, + 'Description': params.Description, + 'Handler': params.Handler, + 'MemorySize': params.MemorySize, + 'Role': params.Role, + 'Timeout': params.Timeout, + 'Runtime': params.Runtime, + 'VpcConfig': params.VpcConfig, + 'Environment': params.Environment, + 'DeadLetterConfig': params.DeadLetterConfig, + 'TracingConfig': params.TracingConfig + }, (err, data) => { + if (err) return reject(err) + resolve(data) + }) }) - }) - request.on('retry', (response) => { - console.log(response.error.message) - console.log('=> Retrying') + request.on('retry', (response) => { + console.log(response.error.message) + console.log('=> Retrying') + }) }) - - return request } Lambda.prototype._uploadNew = (lambda, params, cb) => { @@ -868,10 +867,7 @@ Lambda.prototype.deploy = function (program) { // 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 - } + _this._uploadExisting(lambda, params).then((results) => { console.log('=> Zip file(s) done uploading. Results follow: ') console.log(results) _this._updateScheduleEvents( @@ -883,8 +879,11 @@ Lambda.prototype.deploy = function (program) { resolve(results) } ) + }).catch((err) => { + reject(err) }) }), + new Promise((resolve, reject) => { _this._updateEventSources( lambda, diff --git a/test/main.js b/test/main.js index 5cc0518a..5347ab41 100644 --- a/test/main.js +++ b/test/main.js @@ -1007,12 +1007,10 @@ describe('lib/main', function () { }) describe('_uploadExisting', () => { - it('simple test with mock', (done) => { + it('simple test with mock', () => { const params = lambda._params(program, null) - lambda._uploadExisting(awsLambda, params, (err, results) => { - assert.isNull(err) + return lambda._uploadExisting(awsLambda, params).then((results) => { assert.deepEqual(results, lambdaMockSettings.updateFunctionConfiguration) - done() }) }) }) From f2c20c8ad639488ff2e523370b0995b8dd339822 Mon Sep 17 00:00:00 2001 From: abetomo Date: Wed, 14 Jun 2017 21:21:34 +0900 Subject: [PATCH 05/12] Modify _uploadNew returns Promise --- lib/main.js | 24 +++++++++++++----------- test/main.js | 6 ++---- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/main.js b/lib/main.js index 6b0c00e4..7e97f0e5 100644 --- a/lib/main.js +++ b/lib/main.js @@ -498,14 +498,17 @@ Lambda.prototype._uploadExisting = (lambda, params, cb) => { }) } -Lambda.prototype._uploadNew = (lambda, params, cb) => { - const request = lambda.createFunction(params, (err, data) => cb(err, data)) - request.on('retry', (response) => { - console.log(response.error.message) - console.log('=> Retrying') +Lambda.prototype._uploadNew = (lambda, params) => { + return new Promise((resolve, reject) => { + const request = lambda.createFunction(params, (err, data) => { + if (err) return reject(err) + resolve(data) + }) + request.on('retry', (response) => { + console.log(response.error.message) + console.log('=> Retrying') + }) }) - - return request } Lambda.prototype._readArchive = function (program, archiveCallback) { @@ -814,10 +817,7 @@ Lambda.prototype.deploy = function (program) { }, (err) => { if (err) { // Function does not exist - return _this._uploadNew(lambda, params, function (err, results) { - if (err) { - throw err - } + return _this._uploadNew(lambda, params).then((results) => { console.log('=> Zip file(s) done uploading. Results follow: ') console.log(results) @@ -852,6 +852,8 @@ Lambda.prototype.deploy = function (program) { }).catch((err) => { cb(err) }) + }).catch((err) => { + throw(err) }) } diff --git a/test/main.js b/test/main.js index 5347ab41..bde43e58 100644 --- a/test/main.js +++ b/test/main.js @@ -996,12 +996,10 @@ describe('lib/main', function () { }) describe('_uploadNew', () => { - it('simple test with mock', (done) => { + it('simple test with mock', () => { const params = lambda._params(program, null) - lambda._uploadNew(awsLambda, params, (err, results) => { - assert.isNull(err) + return lambda._uploadNew(awsLambda, params, (results) => { assert.deepEqual(results, lambdaMockSettings.createFunction) - done() }) }) }) From 83d6fc04424676b220a2f32b1250337c3b295130 Mon Sep 17 00:00:00 2001 From: abetomo Date: Wed, 14 Jun 2017 21:22:47 +0900 Subject: [PATCH 06/12] Space after "throw" --- lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/main.js b/lib/main.js index 7e97f0e5..2f2e0dc9 100644 --- a/lib/main.js +++ b/lib/main.js @@ -853,7 +853,7 @@ Lambda.prototype.deploy = function (program) { cb(err) }) }).catch((err) => { - throw(err) + throw err }) } From 9bf097e05f146137e3fdd2af179127701d99c9b6 Mon Sep 17 00:00:00 2001 From: abetomo Date: Thu, 15 Jun 2017 11:03:07 +0900 Subject: [PATCH 07/12] Modify _updateScheduleEvents returns Promise --- lib/main.js | 52 ++++++++++++++++++++-------------------------------- test/main.js | 41 ++++++++++++++++------------------------- 2 files changed, 36 insertions(+), 57 deletions(-) diff --git a/lib/main.js b/lib/main.js index 2f2e0dc9..2d6368ff 100644 --- a/lib/main.js +++ b/lib/main.js @@ -705,9 +705,9 @@ Lambda.prototype._updateEventSources = (lambda, functionName, existingEventSourc }) } -Lambda.prototype._updateScheduleEvents = (scheduleEvents, functionArn, scheduleList, cb) => { +Lambda.prototype._updateScheduleEvents = (scheduleEvents, functionArn, scheduleList) => { if (scheduleList == null) { - return new Promise(resolve => cb(null, [])) + return Promise.resolve([]) } const paramsList = scheduleList.map((schedule) => @@ -722,9 +722,9 @@ Lambda.prototype._updateScheduleEvents = (scheduleEvents, functionArn, scheduleL // Since `scheduleEvents.add(params)` returns only `{}` if it succeeds // it is not very meaningful. // Therefore, return the params used for execution - cb(null, paramsList) + return Promise.resolve(paramsList) }).catch((err) => { - cb(err) + return Promise.reject(err) }) } @@ -836,24 +836,18 @@ Lambda.prototype.deploy = function (program) { } ) }), - new Promise((resolve, reject) => { - _this._updateScheduleEvents( - scheduleEvents, - results.FunctionArn, - eventSourceList.ScheduleEvents, - (err, results) => { - if (err) return reject(err) - resolve(results) - } - ) - }) + _this._updateScheduleEvents( + scheduleEvents, + results.FunctionArn, + eventSourceList.ScheduleEvents + ) ]).then((results) => { cb(null, results) }).catch((err) => { cb(err) }) }).catch((err) => { - throw err + return Promise.reject(err) }) } @@ -868,22 +862,16 @@ Lambda.prototype.deploy = function (program) { // 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).then((results) => { - console.log('=> Zip file(s) done uploading. Results follow: ') - console.log(results) - _this._updateScheduleEvents( - scheduleEvents, - results.FunctionArn, - eventSourceList.ScheduleEvents, - (err, results) => { - if (err) return reject(err) - resolve(results) - } - ) - }).catch((err) => { - reject(err) - }) + _this._uploadExisting(lambda, params).then((results) => { + console.log('=> Zip file(s) done uploading. Results follow: ') + console.log(results) + return _this._updateScheduleEvents( + scheduleEvents, + results.FunctionArn, + eventSourceList.ScheduleEvents + ) + }).catch((err) => { + return Promise.reject(err) }), new Promise((resolve, reject) => { diff --git a/test/main.js b/test/main.js index bde43e58..6bbe2f3b 100644 --- a/test/main.js +++ b/test/main.js @@ -956,41 +956,32 @@ describe('lib/main', function () { after(() => fs.unlinkSync('event_sources.json')) - it('program.eventSourceFile is empty value', (done) => { + it('program.eventSourceFile is empty value', () => { program.eventSourceFile = '' const eventSourceList = lambda._eventSourceList(program) - lambda._updateScheduleEvents( + return lambda._updateScheduleEvents( schedule, '', - eventSourceList.ScheduleEvents, - (err, results) => { - assert.isNull(err) - assert.deepEqual(results, []) - done() - } - ) + eventSourceList.ScheduleEvents + ).then((results) => { + assert.deepEqual(results, []) + }) }) it('simple test with mock', () => { program.eventSourceFile = 'event_sources.json' const eventSourceList = lambda._eventSourceList(program) const functionArn = 'arn:aws:lambda:us-west-2:XXX:function:node-lambda-test-function' - return new Promise((resolve) => { - lambda._updateScheduleEvents( - schedule, - functionArn, - eventSourceList.ScheduleEvents, - (err, results) => resolve({ err: err, results: results }) - ) - }).then((actual) => { - const expected = { - err: null, - results: [Object.assign( - eventSourcesJsonValue.ScheduleEvents[0], - { FunctionArn: functionArn } - )] - } - assert.deepEqual(actual, expected) + return lambda._updateScheduleEvents( + schedule, + functionArn, + eventSourceList.ScheduleEvents + ).then((results) => { + const expected = [Object.assign( + eventSourcesJsonValue.ScheduleEvents[0], + { FunctionArn: functionArn } + )] + assert.deepEqual(results, expected) }) }) }) From ace14396c3ef36c27a1321854617e3f1d23419b7 Mon Sep 17 00:00:00 2001 From: abetomo Date: Thu, 15 Jun 2017 11:19:14 +0900 Subject: [PATCH 08/12] Modify _updateEventSources returns Promise --- lib/main.js | 45 +++++++++++------------------- test/main.js | 78 ++++++++++++++++++---------------------------------- 2 files changed, 43 insertions(+), 80 deletions(-) diff --git a/lib/main.js b/lib/main.js index 2d6368ff..ca15e694 100644 --- a/lib/main.js +++ b/lib/main.js @@ -616,9 +616,9 @@ Lambda.prototype._listEventSourceMappings = function (lambda, params, cb) { }) } -Lambda.prototype._updateEventSources = (lambda, functionName, existingEventSourceList, eventSourceList, cb) => { +Lambda.prototype._updateEventSources = (lambda, functionName, existingEventSourceList, eventSourceList) => { if (eventSourceList == null) { - return new Promise(resolve => cb(null, [])) + return Promise.resolve([]) } const updateEventSourceList = [] // Checking new and update event sources @@ -699,9 +699,9 @@ Lambda.prototype._updateEventSources = (lambda, functionName, existingEventSourc } return Promise.resolve() })).then((data) => { - cb(null, data) + return Promise.resolve(data) }).catch((err) => { - cb(err) + return Promise.reject(err) }) } @@ -824,18 +824,12 @@ Lambda.prototype.deploy = function (program) { // 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) - } - ) - }), + _this._updateEventSources( + lambda, + params.FunctionName, + [], + eventSourceList.EventSourceMappings + ), _this._updateScheduleEvents( scheduleEvents, results.FunctionArn, @@ -873,19 +867,12 @@ Lambda.prototype.deploy = function (program) { }).catch((err) => { return Promise.reject(err) }), - - new Promise((resolve, reject) => { - _this._updateEventSources( - lambda, - params.FunctionName, - existingEventSourceList, - eventSourceList.EventSourceMappings, - (err, results) => { - if (err) return reject(err) - resolve(results) - } - ) - }) + _this._updateEventSources( + lambda, + params.FunctionName, + existingEventSourceList, + eventSourceList.EventSourceMappings + ) ]).then((results) => { cb(null, results) }).catch((err) => { diff --git a/test/main.js b/test/main.js index 6bbe2f3b..a6700e7d 100644 --- a/test/main.js +++ b/test/main.js @@ -858,77 +858,53 @@ describe('lib/main', function () { after(() => fs.unlinkSync('event_sources.json')) - it('program.eventSourceFile is empty value', (done) => { + it('program.eventSourceFile is empty value', () => { program.eventSourceFile = '' const eventSourceList = lambda._eventSourceList(program) - lambda._updateEventSources( + return lambda._updateEventSources( awsLambda, '', [], - eventSourceList.EventSourceMappings, - (err, results) => { - assert.isNull(err) - assert.deepEqual(results, []) - done() - } - ) + eventSourceList.EventSourceMappings + ).then((results) => { + assert.deepEqual(results, []) + }) }) it('simple test with mock (In case of new addition)', () => { program.eventSourceFile = 'event_sources.json' const eventSourceList = lambda._eventSourceList(program) - 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) + return lambda._updateEventSources( + awsLambda, + 'functionName', + [], + eventSourceList.EventSourceMappings + ).then((results) => { + assert.deepEqual(results, [lambdaMockSettings.createEventSourceMapping]) }) }) 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) + return lambda._updateEventSources( + awsLambda, + 'functionName', + lambdaMockSettings.listEventSourceMappings.EventSourceMappings, + {} + ).then((results) => { + assert.deepEqual(results, [lambdaMockSettings.deleteEventSourceMapping]) }) }) it('simple test with mock (In case of update)', () => { program.eventSourceFile = 'event_sources.json' const eventSourceList = lambda._eventSourceList(program) - 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) + return lambda._updateEventSources( + awsLambda, + 'functionName', + lambdaMockSettings.listEventSourceMappings.EventSourceMappings, + eventSourceList.EventSourceMappings + ).then((results) => { + assert.deepEqual(results, [lambdaMockSettings.updateEventSourceMapping]) }) }) }) From 950d207ca85f4ac1370079b03d459fa46dcad234 Mon Sep 17 00:00:00 2001 From: abetomo Date: Thu, 15 Jun 2017 11:29:18 +0900 Subject: [PATCH 09/12] Remove unnecessary arguments --- lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/main.js b/lib/main.js index ca15e694..7e5bc6e8 100644 --- a/lib/main.js +++ b/lib/main.js @@ -464,7 +464,7 @@ Lambda.prototype._setRunTimeEnvironmentVars = function (program) { } } -Lambda.prototype._uploadExisting = (lambda, params, cb) => { +Lambda.prototype._uploadExisting = (lambda, params) => { return new Promise((resolve, reject) => { const request = lambda.updateFunctionCode({ 'FunctionName': params.FunctionName, From 9d2fbe6aad144ea8eba4a51485d4dde4cf00a998 Mon Sep 17 00:00:00 2001 From: abetomo Date: Fri, 16 Jun 2017 10:47:40 +0900 Subject: [PATCH 10/12] Modify main process of deploy to another function --- lib/main.js | 194 +++++++++++++++++++++++++--------------------------- 1 file changed, 93 insertions(+), 101 deletions(-) diff --git a/lib/main.js b/lib/main.js index 7e5bc6e8..081c915a 100644 --- a/lib/main.js +++ b/lib/main.js @@ -765,127 +765,117 @@ Lambda.prototype.package = function (program) { }) } -Lambda.prototype.deploy = function (program) { - var _this = this - var regions = program.region.split(',') - _this._archive(program, function (err, buffer) { - if (err) { - throw err - } - - console.log('=> Reading zip file to memory') - var params = _this._params(program, buffer) - - console.log('=> Reading event source file to memory') - var eventSourceList = _this._eventSourceList(program) +Lambda.prototype._deployToRegion = function (program, params, region) { + const _this = this + console.log('=> Reading event source file to memory') + const eventSourceList = _this._eventSourceList(program) - async.map(regions, function (region, cb) { - console.log('=> Uploading zip file to AWS Lambda ' + region + ' with parameters:') - console.log(params) - - var awsSecurity = { - region: region - } - - if (program.profile) { - aws.config.credentials = new aws.SharedIniFileCredentials({ - profile: program.profile - }) - } else { - awsSecurity.accessKeyId = program.accessKey - awsSecurity.secretAccessKey = program.secretKey - } + return new Promise((resolve, reject) => { + console.log('=> Uploading zip file to AWS Lambda ' + region + ' with parameters:') + console.log(params) - if (program.sessionToken) { - awsSecurity.sessionToken = program.sessionToken - } + const awsSecurity = { region: region } - if (program.deployTimeout) { - aws.config.httpOptions.timeout = parseInt(program.deployTimeout) - } + if (program.profile) { + aws.config.credentials = new aws.SharedIniFileCredentials({ + profile: program.profile + }) + } else { + awsSecurity.accessKeyId = program.accessKey + awsSecurity.secretAccessKey = program.secretKey + } - aws.config.update(awsSecurity) + if (program.sessionToken) { + awsSecurity.sessionToken = program.sessionToken + } - var lambda = new aws.Lambda({ - apiVersion: '2015-03-31' - }) - var scheduleEvents = new ScheduleEvents(aws) + if (program.deployTimeout) { + aws.config.httpOptions.timeout = parseInt(program.deployTimeout) + } - // 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) + aws.config.update(awsSecurity) - // This code is on its way to Promise. - // From now on, callback will not be used. - return Promise.all([ - _this._updateEventSources( - lambda, - params.FunctionName, - [], - eventSourceList.EventSourceMappings - ), - _this._updateScheduleEvents( - scheduleEvents, - results.FunctionArn, - eventSourceList.ScheduleEvents - ) - ]).then((results) => { - cb(null, results) - }).catch((err) => { - cb(err) - }) - }).catch((err) => { - return Promise.reject(err) - }) - } + const lambda = new aws.Lambda({ apiVersion: '2015-03-31' }) + const scheduleEvents = new ScheduleEvents(aws) - // Function exists - _this._listEventSourceMappings(lambda, { - 'FunctionName': params.FunctionName - }, (err, existingEventSourceList) => { - if (err) { - throw err - } + // 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) - // This code is on its way to Promise. - // From now on, callback will not be used. return Promise.all([ - _this._uploadExisting(lambda, params).then((results) => { - console.log('=> Zip file(s) done uploading. Results follow: ') - console.log(results) - return _this._updateScheduleEvents( - scheduleEvents, - results.FunctionArn, - eventSourceList.ScheduleEvents - ) - }).catch((err) => { - return Promise.reject(err) - }), _this._updateEventSources( lambda, params.FunctionName, - existingEventSourceList, + [], eventSourceList.EventSourceMappings + ), + _this._updateScheduleEvents( + scheduleEvents, + results.FunctionArn, + eventSourceList.ScheduleEvents ) ]).then((results) => { - cb(null, results) + resolve(results) }).catch((err) => { - cb(err) + reject(err) }) + }).catch((err) => { + reject(err) }) - }) - }, function (err, results) { - if (err) { - throw err } - const resultsIsEmpty = results.filter(function (result) { - return result.filter(function (res) { + + // Function exists + _this._listEventSourceMappings(lambda, { + 'FunctionName': params.FunctionName + }, (err, existingEventSourceList) => { + if (err) return reject(err) + + return Promise.all([ + _this._uploadExisting(lambda, params).then((results) => { + console.log('=> Zip file(s) done uploading. Results follow: ') + console.log(results) + return _this._updateScheduleEvents( + scheduleEvents, + results.FunctionArn, + eventSourceList.ScheduleEvents + ) + }), + _this._updateEventSources( + lambda, + params.FunctionName, + existingEventSourceList, + eventSourceList.EventSourceMappings + ) + ]).then((results) => { + resolve(results) + }).catch((err) => { + reject(err) + }) + }) + }) + }) +} + +Lambda.prototype.deploy = function (program) { + const _this = this + const regions = program.region.split(',') + _this._archive(program, (err, buffer) => { + if (err) throw err + + console.log('=> Reading zip file to memory') + const params = _this._params(program, buffer) + + Promise.all(regions.map((region) => { + return _this._deployToRegion(program, params, region) + })).then((results) => { + const resultsIsEmpty = results.filter((result) => { + return result.filter((res) => { return res.length > 0 }).length > 0 }).length === 0 @@ -893,6 +883,8 @@ Lambda.prototype.deploy = function (program) { console.log('=> All tasks done. Results follow: ') console.log(JSON.stringify(results, null, ' ')) } + }).catch((err) => { + console.log(err) }) }) } From f917d6d317f28bd7dea44b89d4e5111d4693e9bc Mon Sep 17 00:00:00 2001 From: abetomo Date: Fri, 16 Jun 2017 10:57:24 +0900 Subject: [PATCH 11/12] Remove `async` --- lib/main.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/main.js b/lib/main.js index 081c915a..ff47593f 100644 --- a/lib/main.js +++ b/lib/main.js @@ -8,7 +8,6 @@ const execFile = require('child_process').execFile const fs = require('fs-extra') const packageJson = require(path.join(__dirname, '..', 'package.json')) const minimatch = require('minimatch') -const async = require('async') const zip = new (require('node-zip'))() const dotenv = require('dotenv') const ScheduleEvents = require(path.join(__dirname, 'schedule_events')) From 7fe63ab992da09a2ea4294a26b14fe7b1bb0f728 Mon Sep 17 00:00:00 2001 From: abetomo Date: Fri, 16 Jun 2017 10:57:39 +0900 Subject: [PATCH 12/12] Add reason for pending PR was opened https://github.com/dwyl/aws-sdk-mock/pull/94 --- test/main.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/main.js b/test/main.js index a6700e7d..0f22cbb2 100644 --- a/test/main.js +++ b/test/main.js @@ -1022,7 +1022,11 @@ describe('lib/main', function () { }) }) + describe('Lambda.prototype._deployToRegion()', () => { + it('Since `aws-mock` does not correspond to `request.on`, it is impossible to test with Mock') + }) + describe('Lambda.prototype.deploy()', () => { - it('TODO: Add test. Since the current deploy function is hard to test, skip') + it('Since `aws-mock` does not correspond to `request.on`, it is impossible to test with Mock') }) })