-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.cjs
More file actions
150 lines (131 loc) · 4.26 KB
/
deploy.cjs
File metadata and controls
150 lines (131 loc) · 4.26 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const AWS = require('aws-sdk'),
path = require('path'),
fs = require('fs'),
util = require('util'),
mime = require('mime-types');
const { glob } = require('glob');
let dotenv = require('dotenv').config();
projectRoot = path.join(__dirname);
var awsconfig = {
awsAccessKeyId: process.env.awsAccessKeyId,
secretAccessKey: process.env.secretAccessKey,
awsRegion: process.env.awsRegion,
};
var buckets = {
qa: 'demo.apeon.it',
staging: 'preprod.apeon.it',
prod: 'dapp.apeon.it',
};
var distributions = {
qa: 'EQD4LZSQM07QY',
staging: 'E2KSERHMR1BSW6',
prod: 'E1W0AY81RR2BRI',
};
const buildFolder = 'dist';
var asyncForEach = async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
};
var readFile = util.promisify(fs.readFile);
var deleteFile = async function (f) {
return new Promise((resolve, reject) => {
fs.unlink(f, function (err) {
if (err) resolve(false);
resolve(true);
});
});
};
const uploadTos3 = async function (buildType = 'dev') {
if (!buildType) return 0;
var s3 = new AWS.S3({
accessKeyId: awsconfig.awsAccessKeyId,
secretAccessKey: awsconfig.secretAccessKey,
});
const fileList = await getDirectyFiles(buildFolder);
let folders = [];
fileList.forEach((file) => {
let parts = file.split('/');
let folderPath = parts.slice(0, -1).join('/').trim();
if (folderPath && !folders.includes(folderPath)) {
folders.push(folderPath);
}
});
if (!fileList.length) console.log(buildFolder + ' folder empty..');
else {
console.log('Deployment (' + buildType + ') Started...');
var params = { Bucket: buckets[buildType], Delimiter: '/' };
await s3
.listObjectsV2(params)
.promise()
.then(async ({ Contents }) => {
if (Contents.length)
await s3
.deleteObjects({
Bucket: buckets[buildType],
Delete: { Objects: Contents.map((item) => ({ Key: item.Key })) },
})
.promise();
});
for (let folder of folders) {
await s3
.listObjectsV2({ Bucket: buckets[buildType], Delimiter: '/' + folder })
.promise()
.then(async ({ Contents }) => {
if (Contents.length)
await s3
.deleteObjects({
Bucket: buckets[buildType],
Delete: { Objects: Contents.map((item) => ({ Key: item.Key })) },
})
.promise();
});
}
console.log('cleaned up bucket');
console.log('Upload Started...');
await asyncForEach(fileList, async function (item, index) {
var fileContent = await readFile(projectRoot + `/${buildFolder}/` + item).catch((err) => {
console.log('error on read (' + index + ')' + item);
});
if (fileContent) {
var mimeType = mime.lookup(projectRoot + '/dist/' + item);
var params = {
Bucket: buckets[buildType],
Key: item,
Body: fileContent,
ContentType: mimeType,
};
var s = await s3.putObject(params).promise();
console.log('completed ' + index + ' of ' + fileList.length);
}
});
console.log('Upload complete...');
console.log('Deployed ' + buildType);
console.log('Running Invalidations');
var items = ['/*'];
var cloudfront = new AWS.CloudFront({
accessKeyId: awsconfig.awsAccessKeyId,
secretAccessKey: awsconfig.secretAccessKey,
});
var params = {
DistributionId: distributions[buildType],
InvalidationBatch: {
CallerReference: new Date().getTime().toString() /* required */,
Paths: { Quantity: 1, Items: items },
},
};
var s = await cloudfront.createInvalidation(params).promise();
console.log('Invalidations complete...');
}
};
var getDirectyFiles = async function (src) {
return new Promise(async (resolve, reject) => {
var res = await glob(src + '/**/*');
res.forEach((el, i) => {
res[i] = res[i].replace(/\\/g, '/');
res[i] = res[i].replace(src + '/', '');
});
resolve(res);
});
};
uploadTos3(process.argv[2]);