-
Notifications
You must be signed in to change notification settings - Fork 2
/
deploy.coffee
65 lines (53 loc) · 1.75 KB
/
deploy.coffee
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
## Basic Deploy: Upload everything in /dist straight to S3
# Configuration Constants
AWS_PROFILE = "docs-deployer" # references ~/.aws/credentials
TARGET_BUCKET = 'docs.paperize.io' # S3 bucket name
CLOUDFRONT_DISTRIBUTION_ID = "E1G7A2XX5SSZTS"
DIRECTORY_TO_DEPLOY = "dist" # local directory containing static files
# Utils
_ = require("lodash")
# Setup AWS
process.env.AWS_PROFILE = AWS_PROFILE
AWS = require("aws-sdk")
s3 = new AWS.S3()
cloudfront = new AWS.CloudFront()
# Filesystem stuff
mimeTypes = require("mime-types")
Promise = require("bluebird")
fs = Promise.promisifyAll(require("fs"))
recursiveReaddir = require("recursive-readdir")
# For all files in deploy dir
recursiveReaddir("./#{DIRECTORY_TO_DEPLOY}").then (files) ->
itemCount = files.length
# Strip the deploy dir from the path
files = files.map (file) -> file.replace("#{DIRECTORY_TO_DEPLOY}/", "")
# Start uploading in parallel
console.log("Uploading #{itemCount} files...")
Promise.map files, uploadFileToS3, concurrency: 10
.then ->
console.log("Invalidating CloudFront cache...")
cloudfront.createInvalidation
DistributionId: CLOUDFRONT_DISTRIBUTION_ID
InvalidationBatch:
CallerReference: String(Date.now())
Paths:
Quantity: 1
Items: [ '/*' ]
.promise()
.then ->
console.log("Done.")
uploadFileToS3 = (file) ->
mimeType = mimeTypes.lookup(file) || 'application/octet-stream'
s3.putObject({
Bucket: TARGET_BUCKET
Key: file
Body: fs.createReadStream("./#{DIRECTORY_TO_DEPLOY}/#{file}")
ACL: 'public-read'
ContentDisposition: 'inline'
ContentType: mimeType
}).promise()
.then ->
console.log "Uploaded #{file}, #{mimeType}"
.catch (error) ->
console.log(error)
throw error