aws-spa
Version:
A no-brainer script to deploy a single page app on AWS
102 lines (76 loc) • 3.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.deploy = void 0;
var _fs = require("fs");
var _acm = require("./acm");
var _cloudfront = require("./cloudfront");
var _originAccess = require("./cloudfront/origin-access");
var _logger = require("./logger");
var _prompt = require("./prompt");
var _route = require("./route53");
var _s = require("./s3");
const deploy = async (url, folder, wait, cacheInvalidations, cacheBustedPrefix, noPrompt, shouldBlockBucketPublicAccess, noDefaultRootObject, redirect403ToRoot, objectExpirationDays) => {
await (0, _prompt.predeployPrompt)(Boolean(process.env.CI), noPrompt);
const [domainName, s3Folder] = url.split('/');
_logger.logger.info(`✨ Deploying "${folder}" on "${domainName}" with path "${s3Folder || '/'}"...`);
if (!(0, _fs.existsSync)(folder)) {
throw new Error(`folder "${folder}" not found`);
}
if (!(0, _fs.existsSync)(`${folder}/index.html`)) {
throw new Error(`"index.html" not found in "${folder}" folder`);
}
if (await (0, _s.doesS3BucketExists)(domainName)) {
await (0, _s.confirmBucketManagement)(domainName);
} else {
await (0, _s.createBucket)(domainName); // without this timeout `setBucketPolicy` fails with error
// "The specified bucket does not exist"
await new Promise(resolve => setTimeout(resolve, 2000));
}
await (0, _s.tagBucket)(domainName);
if (objectExpirationDays) {
await (0, _s.upsertLifeCycleConfiguration)(domainName, objectExpirationDays);
}
let hostedZone = (await (0, _route.findHostedZone)(domainName)) || (await (0, _route.createHostedZone)(domainName));
let certificateArn = await (0, _acm.getCertificateARN)(domainName);
if (!certificateArn) {
if (!hostedZone.Id) {
throw new Error(`[route53] hostedZone.Id is not defined for "${domainName}"`);
}
certificateArn = await (0, _acm.createCertificate)(domainName, hostedZone.Id);
}
let distribution = await (0, _cloudfront.findDeployedCloudfrontDistribution)(domainName);
if (!distribution) {
distribution = await (0, _cloudfront.createCloudFrontDistribution)(domainName, certificateArn);
}
if (shouldBlockBucketPublicAccess) {
const oac = await (0, _originAccess.upsertOriginAccessControl)(domainName, distribution.Id);
await (0, _cloudfront.updateCloudFrontDistribution)(distribution.Id, domainName, {
shouldBlockBucketPublicAccess: true,
noDefaultRootObject,
oac,
redirect403ToRoot
});
await (0, _s.removeBucketWebsite)(domainName);
await (0, _s.blockBucketPublicAccess)(domainName);
await (0, _s.setBucketPolicyForOAC)(domainName, distribution.Id);
} else {
await (0, _cloudfront.updateCloudFrontDistribution)(distribution.Id, domainName, {
shouldBlockBucketPublicAccess: false,
noDefaultRootObject,
oac: null,
redirect403ToRoot
});
await (0, _s.setBucketWebsite)(domainName);
await (0, _s.allowBucketPublicAccess)(domainName);
await (0, _s.setBucketPolicy)(domainName);
await (0, _originAccess.cleanExistingOriginAccessControl)(domainName, distribution.Id);
}
if (await (0, _route.needsUpdateRecord)(hostedZone.Id, domainName, distribution.DomainName)) {
await (0, _route.updateRecord)(hostedZone.Id, domainName, distribution.DomainName);
}
await (0, _s.syncToS3)(folder, domainName, cacheBustedPrefix, s3Folder);
await (0, _cloudfront.invalidateCloudfrontCacheWithRetry)(distribution.Id, (0, _cloudfront.getCacheInvalidations)(cacheInvalidations, s3Folder), wait);
};
exports.deploy = deploy;