-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.ts
83 lines (72 loc) · 1.92 KB
/
deploy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import {
S3Client,
DeleteObjectsCommand,
ListObjectsV2Command,
PutObjectCommand
} from '@aws-sdk/client-s3'
import mime from 'mime-types'
import {
CloudFrontClient,
CreateInvalidationCommand
} from '@aws-sdk/client-cloudfront'
import { readFileSync } from 'fs'
const BUCKET = 'cmon.rsvp'
const SOURCE_DIR = 'client/dist/'
const s3Client = new S3Client({
region: Bun.env.AWS_REGION,
credentials: {
accessKeyId: Bun.env.AWS_ACCESS_KEY_ID,
secretAccessKey: Bun.env.AWS_SECRET_ACCESS_KEY
}
})
console.log('Removing all files on bucket')
const listObjects = await s3Client.send(
new ListObjectsV2Command({ Bucket: BUCKET })
)
if (listObjects.Contents)
await s3Client.send(
new DeleteObjectsCommand({
Bucket: BUCKET,
Delete: {
Objects: listObjects.Contents!.map(obj => ({ Key: obj.Key! }))
}
})
)
console.log('Attempting to upload site...')
console.log(`Command: aws s3 sync ${SOURCE_DIR} s3://${BUCKET}/`)
const glob = new Bun.Glob(`${SOURCE_DIR}**/*`)
for await (const file of glob.scan('.')) {
console.log(file)
const fileContent = readFileSync(file)
const contentType = mime.lookup(file) || 'application/octet-stream'
await s3Client.send(
new PutObjectCommand({
Bucket: BUCKET,
Key: file.replace(SOURCE_DIR, ''),
ContentType: contentType,
Body: fileContent
})
)
}
console.log('S3 Upload complete')
console.log('Invalidating CloudFront distribution to get fresh cache')
const cloudfrontClient = new CloudFrontClient({
region: Bun.env.AWS_REGION,
credentials: {
accessKeyId: Bun.env.AWS_ACCESS_KEY_ID,
secretAccessKey: Bun.env.AWS_SECRET_ACCESS_KEY
}
})
await cloudfrontClient.send(
new CreateInvalidationCommand({
DistributionId: 'E12X42D43CT3M5',
InvalidationBatch: {
Paths: {
Quantity: 1,
Items: ['/*']
},
CallerReference: Date.now().toString()
}
})
)
console.log('Deployment complete')