Skip to content

Commit

Permalink
Merge pull request #24 from mihaerzen/master
Browse files Browse the repository at this point in the history
Fix "autoInvalidate"
  • Loading branch information
aghadiry authored Sep 1, 2021
2 parents 581b8f8 + 75c6348 commit df793f7
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}

Expand All @@ -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);
}
}

Expand Down

0 comments on commit df793f7

Please sign in to comment.