forked from austintgriffith/eth.build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploySandbox.js
93 lines (77 loc) · 2.38 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// eslint-disable-next-line import/no-extraneous-dependencies
const s3FolderUpload = require("s3-folder-upload");
const fs = require("fs");
const directoryName = "build";
const BUCKETNAME = "sandbox.eth.build"; // <<---- SET YOUR BUCKET NAME AND CREATE aws.json ** see below vvvvvvvvvv
const invalidation = {
awsDistributionId: "ELRHIIIOM3P69",
awsInvalidationPath: "/*"
}
if (!BUCKETNAME) {
console.log("☢️ Enter a bucket name in packages/react-app/scripts/s3.js ");
process.exit(1);
}
let credentials = {};
try {
credentials = JSON.parse(fs.readFileSync("aws.json"));
} catch (e) {
console.log(e);
console.log(
'☢️ Create an aws.json credentials file in packages/react-app/ like { "accessKeyId": "xxx", "secretAccessKey": "xxx", "region": "xxx" } ',
);
process.exit(1);
}
credentials.bucket = BUCKETNAME;
// optional options to be passed as parameter to the method
const options = {
useFoldersForFileTypes: false,
useIAMRoleCredentials: false,
};
/// //////////
/// ////////// First, let's automatically create the bucket if it doesn't exist...
/// //////////
// eslint-disable-next-line import/no-extraneous-dependencies
const AWS = require("aws-sdk");
// Load credentials and set Region from JSON file
AWS.config.loadFromPath("./aws.json");
// Create S3 service object
s3 = new AWS.S3({ apiVersion: "2006-03-01" });
// Create params JSON for S3.createBucket
const bucketParams = {
Bucket: BUCKETNAME,
ACL: "public-read",
};
// Create params JSON for S3.setBucketWebsite
const staticHostParams = {
Bucket: BUCKETNAME,
WebsiteConfiguration: {
ErrorDocument: {
Key: "index.html",
},
IndexDocument: {
Suffix: "index.html",
},
},
};
// Call S3 to create the bucket
s3.createBucket(bucketParams, function (err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Bucket URL is ", data.Location);
// Set the new policy on the newly created bucket
s3.putBucketWebsite(staticHostParams, function (err, data) {
if (err) {
// Display error message
console.log("Error", err);
} else {
// Update the displayed policy for the selected bucket
console.log("Success... UPLOADING!", data);
///
/// After the bucket is created, we upload to it:
///
s3FolderUpload(directoryName, credentials, options , invalidation );
}
});
}
});