From 75c634894d716752adf56adbd787aed4d6600573 Mon Sep 17 00:00:00 2001 From: Amir Date: Sat, 24 Apr 2021 15:09:38 -0500 Subject: [PATCH] Fix "autoInvalidate" as it was broken with #19 --- index.js | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index fc6db28..2cc353c 100644 --- a/index.js +++ b/index.js @@ -39,11 +39,8 @@ class CloudfrontInvalidate { this.hooks = { 'cloudfrontInvalidate:invalidate': this.invalidate.bind(this), + 'after:deploy:deploy': this.invalidate.bind(this), }; - - if (serverless.service.custom.cloudfrontInvalidate.autoInvalidate !== false) { - this.hooks['after:deploy:deploy'] = this.invalidate.bind(this) - } } setProxy(proxyURL) { @@ -100,7 +97,9 @@ class CloudfrontInvalidate { return; } - this.serverless.service.custom.cloudfrontInvalidate.forEach(element => { + const invalidationPromises = this.serverless.service.custom.cloudfrontInvalidate.map(element => { + const autoInvalidate = element.autoInvalidate !== false; + let cloudfrontInvalidate = element; let reference = randomstring.generate(16); let distributionId = cloudfrontInvalidate.distributionId; @@ -112,6 +111,12 @@ class CloudfrontInvalidate { if (distributionId) { cli.consoleLog(`DistributionId: ${chalk.yellow(distributionId)}`); + + if (autoInvalidate === false) { + cli.consoleLog(`Skipping invalidation for the distributionId "${distributionId}" as autoInvalidate is set to false.`); + return; + } + return this.createInvalidation(distributionId, reference, cloudfrontInvalidate); } @@ -136,12 +141,20 @@ class CloudfrontInvalidate { }); } }) - .then(() => this.createInvalidation(distributionId, reference, cloudfrontInvalidate)) - .catch(error => { + .then(() => { + if (autoInvalidate === false) { + cli.consoleLog(`Skipping invalidation for the distributionId "${distributionId}" as autoInvalidate is set to false.`); + return; + } + + return this.createInvalidation(distributionId, reference, cloudfrontInvalidate); + }) + .catch(() => { cli.consoleLog('Failed to get DistributionId from stack output. Please check your serverless template.'); - return; }); }); + + return Promise.all(invalidationPromises); } }