GenderDysphoria.fyi/gulp/cloudfront.js

60 lines
1.3 KiB
JavaScript
Raw Normal View History

2020-02-20 14:38:25 -08:00
const log = require('fancy-log');
const aws = require('aws-sdk');
var Promise = require('bluebird');
2021-08-25 09:22:39 -07:00
var credentials;
try {
credentials = require('../aws.json');
} catch (e) {
credentials = null;
}
2020-02-20 14:38:25 -08:00
async function invalidate (wait) {
2021-08-25 09:22:39 -07:00
if (!credentials) {
console.error('Cannot access cloudfront without AWS credentials present.'); // eslint-disable-line
return false;
}
2020-02-20 14:38:25 -08:00
var cloudfront = new aws.CloudFront();
cloudfront.config.update({ credentials });
var poll = async function (id) {
const res = await cloudfront.getInvalidation({
DistributionId: credentials.distribution,
Id: id,
}).promise();
if (res.Invalidation.Status === 'Completed') {
return;
}
return Promise.delay(5000).then(() => poll(id));
};
const { Invalidation } = await cloudfront.createInvalidation({
DistributionId: credentials.distribution,
InvalidationBatch: {
CallerReference: Date.now().toString(),
Paths: {
Quantity: 1,
Items: [ '/*' ],
},
},
}).promise();
const id = Invalidation.Id;
log('Invalidation created, waiting for it to complete.', id);
if (wait) await poll(id);
}
module.exports = exports = function invalidateCloudfrontAndWait () {
return invalidate(true);
};
exports.prod = function invalidateCloudfront () {
return invalidate(false);
};