forked from austintgriffith/eth.build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploySandbox.js
72 lines (63 loc) · 1.8 KB
/
deploySandbox.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
const { exec } = require('child_process')
const fs = require("fs")
const awsCreds = JSON.parse(fs.readFileSync("aws.json").toString().trim())
const s3 = require('s3');
const winston = require('winston')
const AWS = require('aws-sdk')
let site = "sandbox.eth.build"
let cloudfrontDistro = "ELRHIIIOM3P69"
const localDir = "build"
//Setup
var client = s3.createClient({
s3Options: awsCreds,
});
uploadParams = buildS3Params(site)
//Prep index
//let index = fs.readFileSync("build/index.html").toString();
//index = index.split("\"\/").join("\"");
//winston.debug(index);
//fs.writeFileSync("build/index.html",index);
//Upload
fs.readdir( 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: cloudfrontDistro,
InvalidationBatch: {
CallerReference: ''+(new Date()),
Paths: {
Quantity: 1,
Items: ["/*"]
}
}
};
cloudfront.createInvalidation(cfparams, function(err, data) {
if (err) reject(err, err.stack); // an error occurred
else console.log(data); // successful response
});
});
})
function buildS3Params(site) {
site = site.toLowerCase()
var uploadParams = {
localDir: localDir,
s3Params: {
Bucket: site,
Prefix: "",
ACL: "public-read"
}
}
return uploadParams
}