diff --git a/lib/main.js b/lib/main.js index 7e5bc6e8..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')) @@ -765,127 +764,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 +882,8 @@ Lambda.prototype.deploy = function (program) { console.log('=> All tasks done. Results follow: ') console.log(JSON.stringify(results, null, ' ')) } + }).catch((err) => { + console.log(err) }) }) } 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') }) })