Created
November 1, 2023 17:51
-
-
Save kevinmcmahon/2cfcec0e4d92184cfbd9b14144d927b0 to your computer and use it in GitHub Desktop.
Quick script to deploy a SPA to a S3 bucket. Extracted from aws-samples/amazon-cognito-passwordless-auth and tweaked.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
process.env.AWS_SDK_LOAD_CONFIG = '1'; | |
const {default: s3SpaUpload} = require('s3-spa-upload'); | |
const fs = require('fs'); | |
const path = require('path'); | |
const proc = require('node:child_process'); | |
proc.execFileSync('yarn', ['run', 'build', ...process.argv.slice(2)], { | |
cwd: __dirname, | |
stdio: 'inherit', | |
env: { | |
...process.env, | |
VITE_APP_BUILD_DATE: new Date().toISOString(), | |
}, | |
}); | |
s3SpaUpload(path.join(__dirname, 'dist'), readEnvFile(), { | |
cacheControlMapping: { | |
'index.html': 'public,max-age=10,stale-while-revalidate=31536000', | |
'*.js': 'public,max-age=31536000,immutable', | |
'*.js.map': 'public,max-age=31536000,immutable', | |
'*.css': 'public,max-age=31536000,immutable', | |
'*.svg': 'public,max-age=86400,stale-while-revalidate=31536000', | |
}, | |
delete: true, | |
}); | |
function readEnvFile() { | |
function tryReadEntry(fname) { | |
try { | |
// eslint-disable-next-line security/detect-non-literal-fs-filename | |
return fs | |
.readFileSync(path.join(__dirname, fname), 'utf8') | |
.split('\n') | |
.filter((l) => !!l && l.startsWith('CDK_STACK_SPA_BUCKET_NAME')) | |
.at(0) | |
?.replace('CDK_STACK_SPA_BUCKET_NAME=', ''); | |
} catch { | |
return; | |
} | |
} | |
const spaBucketName = tryReadEntry('.env.local') ?? tryReadEntry('.env'); | |
if (!spaBucketName) { | |
throw new Error('Failed to read CDK_STACK_SPA_BUCKET_NAME config from .env file'); | |
} | |
return spaBucketName; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment