|
| 1 | +const { S3Client, PutObjectCommand } = require("@aws-sdk/client-s3"); |
| 2 | +const { CloudFrontClient, CreateInvalidationCommand } = require("@aws-sdk/client-cloudfront"); |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +// Configure AWS SDK |
| 7 | +const s3Client = new S3Client({ region: process.env.AWS_REGION }); |
| 8 | +const cloudFrontClient = new CloudFrontClient({ region: process.env.AWS_REGION }); |
| 9 | + |
| 10 | +async function uploadToS3(bucketName, sourceDir, destDir = '') { |
| 11 | + const files = fs.readdirSync(sourceDir); |
| 12 | + |
| 13 | + for (const file of files) { |
| 14 | + const filePath = path.join(sourceDir, file); |
| 15 | + const key = path.join(destDir, file); |
| 16 | + |
| 17 | + if (fs.statSync(filePath).isDirectory()) { |
| 18 | + await uploadToS3(bucketName, filePath, key); |
| 19 | + } else { |
| 20 | + const fileContent = fs.readFileSync(filePath); |
| 21 | + const command = new PutObjectCommand({ |
| 22 | + Bucket: bucketName, |
| 23 | + Key: key, |
| 24 | + Body: fileContent, |
| 25 | + ContentType: getContentType(file), |
| 26 | + }); |
| 27 | + await s3Client.send(command); |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +function getContentType(filename) { |
| 33 | + const ext = path.extname(filename).toLowerCase(); |
| 34 | + switch (ext) { |
| 35 | + case '.html': return 'text/html'; |
| 36 | + case '.css': return 'text/css'; |
| 37 | + case '.js': return 'application/javascript'; |
| 38 | + case '.json': return 'application/json'; |
| 39 | + case '.png': return 'image/png'; |
| 40 | + case '.jpg': case '.jpeg': return 'image/jpeg'; |
| 41 | + default: return 'application/octet-stream'; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +async function invalidateCloudFront(distributionId, paths) { |
| 46 | + const command = new CreateInvalidationCommand({ |
| 47 | + DistributionId: distributionId, |
| 48 | + InvalidationBatch: { |
| 49 | + CallerReference: Date.now().toString(), |
| 50 | + Paths: { |
| 51 | + Quantity: paths.length, |
| 52 | + Items: paths, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }); |
| 56 | + await cloudFrontClient.send(command); |
| 57 | +} |
| 58 | + |
| 59 | +async function publishToSite(site, sourceDir, targetDir = '') { |
| 60 | + const bucketName = process.env.AWS_S3_BUCKET_DOCS; |
| 61 | + let distributionId, siteDir; |
| 62 | + |
| 63 | + if (site === 'dnsdist.org') { |
| 64 | + distributionId = process.env.AWS_CLOUDFRONT_DISTRIBUTION_ID_DNSDIST; |
| 65 | + siteDir = 'dnsdist.org'; |
| 66 | + } else if (site === 'docs.powerdns.com') { |
| 67 | + distributionId = process.env.AWS_CLOUDFRONT_DISTRIBUTION_ID_DOCS; |
| 68 | + siteDir = 'docs.powerdns.com'; |
| 69 | + } else { |
| 70 | + throw new Error('Invalid site specified'); |
| 71 | + } |
| 72 | + |
| 73 | + const fullTargetDir = path.join(siteDir, targetDir); |
| 74 | + await uploadToS3(bucketName, sourceDir, fullTargetDir); |
| 75 | + |
| 76 | + // Invalidate CloudFront cache |
| 77 | + await invalidateCloudFront(distributionId, ['/*']); |
| 78 | + |
| 79 | + console.log(`Published from ${sourceDir} to ${site}${targetDir ? '/' + targetDir : ''}`); |
| 80 | +} |
| 81 | + |
| 82 | +async function main() { |
| 83 | + const args = process.argv.slice(2); |
| 84 | + if (args[0] === 'publish') { |
| 85 | + if (args.length < 3 || args.length > 4) { |
| 86 | + console.log('Usage: node publish.js publish <SITE> <SOURCE_DIR> [TARGET_DIR]'); |
| 87 | + return; |
| 88 | + } |
| 89 | + const [, site, sourceDir, targetDir] = args; |
| 90 | + await publishToSite(site, sourceDir, targetDir); |
| 91 | + } else { |
| 92 | + console.log('Usage: node publish.js publish <SITE> <SOURCE_DIR> [TARGET_DIR]'); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +main().catch(console.error); |
0 commit comments