Skip to content

Commit

Permalink
Merge pull request #25 from mihaerzen/master
Browse files Browse the repository at this point in the history
Allow to manually invalidate all the elements
  • Loading branch information
aghadiry authored Sep 1, 2021
2 parents 45590f9 + 30353a1 commit ba44c52
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class CloudfrontInvalidate {

this.hooks = {
'cloudfrontInvalidate:invalidate': this.invalidate.bind(this),
'after:deploy:deploy': this.invalidate.bind(this),
'after:deploy:deploy': this.afterDeploy.bind(this),
};
}

Expand Down Expand Up @@ -89,34 +89,27 @@ class CloudfrontInvalidate {
);
}

invalidate() {
invalidateElements(elements) {
const cli = this.serverless.cli;

if (this.options.noDeploy) {
cli.consoleLog('skipping invalidation due to noDeploy option');
return;
}

const invalidationPromises = this.serverless.service.custom.cloudfrontInvalidate.map(element => {
const autoInvalidate = element.autoInvalidate !== false;

const invalidationPromises = elements.map(element => {
let cloudfrontInvalidate = element;
let reference = randomstring.generate(16);
let distributionId = cloudfrontInvalidate.distributionId;
let stage = cloudfrontInvalidate.stage;

if (stage !== undefined && stage != `${this.serverless.service.provider.stage}`) {
if (stage !== undefined && stage !== `${this.serverless.service.provider.stage}`) {
return;
}

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 @@ -141,21 +134,32 @@ class CloudfrontInvalidate {
});
}
})
.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);
})
.then(() => this.createInvalidation(distributionId, reference, cloudfrontInvalidate))
.catch(() => {
cli.consoleLog('Failed to get DistributionId from stack output. Please check your serverless template.');
});
});

return Promise.all(invalidationPromises);
}

afterDeploy() {
const elementsToInvalidate = this.serverless.service.custom.cloudfrontInvalidate
.filter((element) => {
if (element.autoInvalidate !== false) {
return true;
}

this.serverless.cli.consoleLog(`Will skip invalidation for the distributionId "${element.distributionId || element.distributionIdKey}" as autoInvalidate is set to false.`);
return false;
});

return this.invalidateElements(elementsToInvalidate);
}

invalidate() {
return this.invalidateElements(this.serverless.service.custom.cloudfrontInvalidate);
}
}

module.exports = CloudfrontInvalidate;

0 comments on commit ba44c52

Please sign in to comment.