Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to manually invalidate all the elements #25

Merged
merged 1 commit into from
Sep 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;