Skip to content

Commit a412f99

Browse files
abetomoDeviaVir
authored andcommitted
Modify main process of deploy to another function (#323)
* Replace async with Promise * Modify to arrow function * Modify from var to const * Modify _uploadExisting returns Promise * Modify _uploadNew returns Promise * Space after "throw" * Modify _updateScheduleEvents returns Promise * Modify _updateEventSources returns Promise * Remove unnecessary arguments * Modify main process of deploy to another function * Remove `async` * Add reason for pending PR was opened dwyl/aws-sdk-mock#94
1 parent 4e8285a commit a412f99

File tree

2 files changed

+98
-103
lines changed

2 files changed

+98
-103
lines changed

lib/main.js

Lines changed: 93 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const execFile = require('child_process').execFile
88
const fs = require('fs-extra')
99
const packageJson = require(path.join(__dirname, '..', 'package.json'))
1010
const minimatch = require('minimatch')
11-
const async = require('async')
1211
const zip = new (require('node-zip'))()
1312
const dotenv = require('dotenv')
1413
const ScheduleEvents = require(path.join(__dirname, 'schedule_events'))
@@ -765,134 +764,126 @@ Lambda.prototype.package = function (program) {
765764
})
766765
}
767766

768-
Lambda.prototype.deploy = function (program) {
769-
var _this = this
770-
var regions = program.region.split(',')
771-
_this._archive(program, function (err, buffer) {
772-
if (err) {
773-
throw err
774-
}
775-
776-
console.log('=> Reading zip file to memory')
777-
var params = _this._params(program, buffer)
778-
779-
console.log('=> Reading event source file to memory')
780-
var eventSourceList = _this._eventSourceList(program)
767+
Lambda.prototype._deployToRegion = function (program, params, region) {
768+
const _this = this
769+
console.log('=> Reading event source file to memory')
770+
const eventSourceList = _this._eventSourceList(program)
781771

782-
async.map(regions, function (region, cb) {
783-
console.log('=> Uploading zip file to AWS Lambda ' + region + ' with parameters:')
784-
console.log(params)
785-
786-
var awsSecurity = {
787-
region: region
788-
}
789-
790-
if (program.profile) {
791-
aws.config.credentials = new aws.SharedIniFileCredentials({
792-
profile: program.profile
793-
})
794-
} else {
795-
awsSecurity.accessKeyId = program.accessKey
796-
awsSecurity.secretAccessKey = program.secretKey
797-
}
772+
return new Promise((resolve, reject) => {
773+
console.log('=> Uploading zip file to AWS Lambda ' + region + ' with parameters:')
774+
console.log(params)
798775

799-
if (program.sessionToken) {
800-
awsSecurity.sessionToken = program.sessionToken
801-
}
776+
const awsSecurity = { region: region }
802777

803-
if (program.deployTimeout) {
804-
aws.config.httpOptions.timeout = parseInt(program.deployTimeout)
805-
}
778+
if (program.profile) {
779+
aws.config.credentials = new aws.SharedIniFileCredentials({
780+
profile: program.profile
781+
})
782+
} else {
783+
awsSecurity.accessKeyId = program.accessKey
784+
awsSecurity.secretAccessKey = program.secretKey
785+
}
806786

807-
aws.config.update(awsSecurity)
787+
if (program.sessionToken) {
788+
awsSecurity.sessionToken = program.sessionToken
789+
}
808790

809-
var lambda = new aws.Lambda({
810-
apiVersion: '2015-03-31'
811-
})
812-
var scheduleEvents = new ScheduleEvents(aws)
791+
if (program.deployTimeout) {
792+
aws.config.httpOptions.timeout = parseInt(program.deployTimeout)
793+
}
813794

814-
// Checking function
815-
return lambda.getFunction({
816-
'FunctionName': params.FunctionName
817-
}, (err) => {
818-
if (err) {
819-
// Function does not exist
820-
return _this._uploadNew(lambda, params).then((results) => {
821-
console.log('=> Zip file(s) done uploading. Results follow: ')
822-
console.log(results)
795+
aws.config.update(awsSecurity)
823796

824-
// This code is on its way to Promise.
825-
// From now on, callback will not be used.
826-
return Promise.all([
827-
_this._updateEventSources(
828-
lambda,
829-
params.FunctionName,
830-
[],
831-
eventSourceList.EventSourceMappings
832-
),
833-
_this._updateScheduleEvents(
834-
scheduleEvents,
835-
results.FunctionArn,
836-
eventSourceList.ScheduleEvents
837-
)
838-
]).then((results) => {
839-
cb(null, results)
840-
}).catch((err) => {
841-
cb(err)
842-
})
843-
}).catch((err) => {
844-
return Promise.reject(err)
845-
})
846-
}
797+
const lambda = new aws.Lambda({ apiVersion: '2015-03-31' })
798+
const scheduleEvents = new ScheduleEvents(aws)
847799

848-
// Function exists
849-
_this._listEventSourceMappings(lambda, {
850-
'FunctionName': params.FunctionName
851-
}, (err, existingEventSourceList) => {
852-
if (err) {
853-
throw err
854-
}
800+
// Checking function
801+
return lambda.getFunction({
802+
'FunctionName': params.FunctionName
803+
}, (err) => {
804+
if (err) {
805+
// Function does not exist
806+
return _this._uploadNew(lambda, params).then((results) => {
807+
console.log('=> Zip file(s) done uploading. Results follow: ')
808+
console.log(results)
855809

856-
// This code is on its way to Promise.
857-
// From now on, callback will not be used.
858810
return Promise.all([
859-
_this._uploadExisting(lambda, params).then((results) => {
860-
console.log('=> Zip file(s) done uploading. Results follow: ')
861-
console.log(results)
862-
return _this._updateScheduleEvents(
863-
scheduleEvents,
864-
results.FunctionArn,
865-
eventSourceList.ScheduleEvents
866-
)
867-
}).catch((err) => {
868-
return Promise.reject(err)
869-
}),
870811
_this._updateEventSources(
871812
lambda,
872813
params.FunctionName,
873-
existingEventSourceList,
814+
[],
874815
eventSourceList.EventSourceMappings
816+
),
817+
_this._updateScheduleEvents(
818+
scheduleEvents,
819+
results.FunctionArn,
820+
eventSourceList.ScheduleEvents
875821
)
876822
]).then((results) => {
877-
cb(null, results)
823+
resolve(results)
878824
}).catch((err) => {
879-
cb(err)
825+
reject(err)
880826
})
827+
}).catch((err) => {
828+
reject(err)
881829
})
882-
})
883-
}, function (err, results) {
884-
if (err) {
885-
throw err
886830
}
887-
const resultsIsEmpty = results.filter(function (result) {
888-
return result.filter(function (res) {
831+
832+
// Function exists
833+
_this._listEventSourceMappings(lambda, {
834+
'FunctionName': params.FunctionName
835+
}, (err, existingEventSourceList) => {
836+
if (err) return reject(err)
837+
838+
return Promise.all([
839+
_this._uploadExisting(lambda, params).then((results) => {
840+
console.log('=> Zip file(s) done uploading. Results follow: ')
841+
console.log(results)
842+
return _this._updateScheduleEvents(
843+
scheduleEvents,
844+
results.FunctionArn,
845+
eventSourceList.ScheduleEvents
846+
)
847+
}),
848+
_this._updateEventSources(
849+
lambda,
850+
params.FunctionName,
851+
existingEventSourceList,
852+
eventSourceList.EventSourceMappings
853+
)
854+
]).then((results) => {
855+
resolve(results)
856+
}).catch((err) => {
857+
reject(err)
858+
})
859+
})
860+
})
861+
})
862+
}
863+
864+
Lambda.prototype.deploy = function (program) {
865+
const _this = this
866+
const regions = program.region.split(',')
867+
_this._archive(program, (err, buffer) => {
868+
if (err) throw err
869+
870+
console.log('=> Reading zip file to memory')
871+
const params = _this._params(program, buffer)
872+
873+
Promise.all(regions.map((region) => {
874+
return _this._deployToRegion(program, params, region)
875+
})).then((results) => {
876+
const resultsIsEmpty = results.filter((result) => {
877+
return result.filter((res) => {
889878
return res.length > 0
890879
}).length > 0
891880
}).length === 0
892881
if (!resultsIsEmpty) {
893882
console.log('=> All tasks done. Results follow: ')
894883
console.log(JSON.stringify(results, null, ' '))
895884
}
885+
}).catch((err) => {
886+
console.log(err)
896887
})
897888
})
898889
}

test/main.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,11 @@ describe('lib/main', function () {
10221022
})
10231023
})
10241024

1025+
describe('Lambda.prototype._deployToRegion()', () => {
1026+
it('Since `aws-mock` does not correspond to `request.on`, it is impossible to test with Mock')
1027+
})
1028+
10251029
describe('Lambda.prototype.deploy()', () => {
1026-
it('TODO: Add test. Since the current deploy function is hard to test, skip')
1030+
it('Since `aws-mock` does not correspond to `request.on`, it is impossible to test with Mock')
10271031
})
10281032
})

0 commit comments

Comments
 (0)