#!/usr/bin/env node /** * Deploy to S3 + CloudFront * Usage: node scripts/deploy.js --env=staging|production */ const { execSync } = require('child_process'); const fs = require('fs'); const path = require('path'); const ENVIRONMENTS = { staging: { bucket: 'quixzoom-asia-staging', region: 'ap-southeast-1', distributionId: process.env.CF_DIST_STAGING, url: 'https://staging.quixzoom.asia', }, production: { bucket: 'quixzoom.asia', region: 'ap-southeast-1', distributionId: 'EHBQ9EIYNM3O0', url: 'https://quixzoom.asia', }, }; const [, , envArg] = process.argv; const env = envArg?.replace('--env=', '') || 'staging'; if (!ENVIRONMENTS[env]) { console.error(`āŒ Unknown environment: ${env}`); console.error('Usage: node scripts/deploy.js --env=staging|production'); process.exit(1); } const config = ENVIRONMENTS[env]; const distDir = path.join(__dirname, '../dist'); const manifestPath = path.join(distDir, 'manifest.json'); if (!fs.existsSync(distDir)) { console.error('āŒ No dist/ directory. Run: npm run build'); process.exit(1); } const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8')); console.log(`šŸš€ Deploying quiXzoom Asia v${manifest.version} to ${env}...`); console.log(` Bucket: ${config.bucket}`); console.log(` Region: ${config.region}`); console.log(` Commit: ${manifest.gitCommit}`); console.log(''); // Sync to S3 const syncCmd = `aws s3 sync ${distDir}/ s3://${config.bucket}/ \ --delete \ --cache-control "max-age=3600" \ --region ${config.region}`; console.log('šŸ“¤ Uploading...'); execSync(syncCmd, { stdio: 'inherit' }); // Set content types console.log('\nšŸ“ Setting content types...'); execSync(`aws s3 cp s3://${config.bucket}/ s3://${config.bucket}/ \ --recursive --exclude "*" --include "*.html" \ --content-type "text/html; charset=utf-8" \ --metadata-directive REPLACE --region ${config.region}`, { stdio: 'inherit' }); // Invalidate CloudFront if (config.distributionId) { console.log('\nšŸ”„ Invalidating CloudFront...'); execSync(`aws cloudfront create-invalidation \ --distribution-id ${config.distributionId} \ --paths "/*" --region ${config.region}`, { stdio: 'inherit' }); } console.log(`\nāœ… Deployed to ${config.url}`); console.log(` Version: ${manifest.version}`); console.log(` Build: ${manifest.buildTime}`);