From 3a7d77b4ffa6e8d37894b3336845918d1c592d4a Mon Sep 17 00:00:00 2001 From: Philipp Muens Date: Mon, 9 Sep 2019 13:31:51 +0200 Subject: [PATCH] Use hooks to log Lambda@Edge removal reminder --- .../compile/events/cloudFront/index.js | 34 +++++++++---------- .../compile/events/cloudFront/index.test.js | 7 ++-- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/lib/plugins/aws/package/compile/events/cloudFront/index.js b/lib/plugins/aws/package/compile/events/cloudFront/index.js index 4043e830f..3097f1a39 100644 --- a/lib/plugins/aws/package/compile/events/cloudFront/index.js +++ b/lib/plugins/aws/package/compile/events/cloudFront/index.js @@ -14,27 +14,27 @@ class AwsCompileCloudFrontEvents { 'package:initialize': this.validate.bind(this), 'before:package:compileFunctions': this.prepareFunctions.bind(this), 'package:compileEvents': this.compileCloudFrontEvents.bind(this), + 'before:remove:remove': this.logRemoveReminder.bind(this), }; - if (this.serverless.processedInput.commands[0] === 'remove') { - this.logRemoveReminder.call(this); - } } logRemoveReminder() { - let isEventUsed = false; - const funcKeys = this.serverless.service.getAllFunctions(); - if (funcKeys.length) { - isEventUsed = funcKeys.some(funcKey => { - const func = this.serverless.service.getFunction(funcKey); - return func.events && func.events.find(e => Object.keys(e)[0] === 'cloudFront'); - }); - } - if (isEventUsed) { - const message = [ - "Don't forget to manually remove your Lambda@Edge functions ", - 'once the CloudFront distribution removal is successfully propagated!', - ].join(''); - this.serverless.cli.log(message, 'Serverless', { color: 'orange' }); + if (this.serverless.processedInput.commands[0] === 'remove') { + let isEventUsed = false; + const funcKeys = this.serverless.service.getAllFunctions(); + if (funcKeys.length) { + isEventUsed = funcKeys.some(funcKey => { + const func = this.serverless.service.getFunction(funcKey); + return func.events && func.events.find(e => Object.keys(e)[0] === 'cloudFront'); + }); + } + if (isEventUsed) { + const message = [ + "Don't forget to manually remove your Lambda@Edge functions ", + 'once the CloudFront distribution removal is successfully propagated!', + ].join(''); + this.serverless.cli.log(message, 'Serverless', { color: 'orange' }); + } } } diff --git a/lib/plugins/aws/package/compile/events/cloudFront/index.test.js b/lib/plugins/aws/package/compile/events/cloudFront/index.test.js index 034a66a51..b80360c5e 100644 --- a/lib/plugins/aws/package/compile/events/cloudFront/index.test.js +++ b/lib/plugins/aws/package/compile/events/cloudFront/index.test.js @@ -82,15 +82,15 @@ describe('AwsCompileCloudFrontEvents', () => { it('should set the provider variable to an instance of AwsProvider', () => expect(awsCompileCloudFrontEvents.provider).to.be.instanceof(AwsProvider)); - it('should log an info message if the users wants to remove the stack with a cloudFront event', () => { + it('should use "before:remove:remove" hook to log a message before removing the service', () => { serverless.processedInput.commands = ['remove']; serverless.service.functions = { first: { events: [ { cloudFront: { - eventType: 'viewer-response', - origin: 's3://some-bucket.s3.amazonaws.com/files', + eventType: 'viewer-request', + origin: 's3://bucketname.s3.amazonaws.com/files', }, }, ], @@ -98,6 +98,7 @@ describe('AwsCompileCloudFrontEvents', () => { }; const awsCompileCloudFrontEventsRemoval = new AwsCompileCloudFrontEvents(serverless, options); + awsCompileCloudFrontEventsRemoval.hooks['before:remove:remove'](); expect(awsCompileCloudFrontEventsRemoval.serverless.cli.log).to.have.been.calledOnce; expect(awsCompileCloudFrontEventsRemoval.serverless.cli.log.args[0][0]).to.include( 'remove your Lambda@Edge functions'