-
Notifications
You must be signed in to change notification settings - Fork 5
/
deploy.js
executable file
·75 lines (63 loc) · 1.93 KB
/
deploy.js
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
const fs = require('fs');
const awsCreds = JSON.parse(fs.readFileSync("aws.json").toString().trim())
const s3 = require('s3');
const AWS = require('aws-sdk')
function uploadToS3(site,target) {
//Setup
var client = s3.createClient({
s3Options: awsCreds,
});
uploadParams = buildS3Params(site)
//Prep index
let index = fs.readFileSync("build/index.html").toString();
index = index.split("\"\/").join("\"");
fs.writeFileSync("build/index.html",index);
//Upload
fs.readdir( uploadParams.localDir , function( err, files ) {
if( err ) {
console.error( "Could not list the directory.", err );
process.exit( 1 );
}
var uploader = client.uploadDir(uploadParams);
uploader.on('error', function(err) {
console.error("unable to sync:", err.stack);
});
uploader.on('progress', function() {
console.log("progress", uploader.progressAmount, uploader.progressTotal);
});
uploader.on('end', function() {
console.log("done uploading "+site);
var cloudfront = new AWS.CloudFront(new AWS.Config(awsCreds));
var cfparams = {
DistributionId: target,
InvalidationBatch: {
CallerReference: ''+(new Date()),
Paths: {
Quantity: 1,
Items: ["/*"]
}
}
};
cloudfront.createInvalidation(cfparams, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
});
})
}
function buildS3Params(site) {
site = site.toLowerCase()
if(site.indexOf(".") <0){
site = site + ".io"
}
var uploadParams = {
localDir: "build",
s3Params: {
Bucket: site,
Prefix: "",
ACL: "public-read"
}
}
return uploadParams
}
uploadToS3("signator.io","EQ7GCXD60HYON")