-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
226 lines (202 loc) · 5.56 KB
/
index.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
"use strict";
const readline = require('readline');
const Async = require('async');
const AWS = require('aws-sdk');
const Crypto = require('crypto');
const chalk = require('chalk');
const Fs = require('fs');
const Glob = require('glob');
const Mime = require('mime');
const Path = require('path');
require('colors');
let config, s3, cloudfront;
let status = {
total: 0,
uploaded: 0,
skipped: 0
};
module.exports.deploy = (options, callback) => {
return setup(options).then(startDeploy).then(() => {
console.log(chalk.green.bold(`Deployed ${config.publicRoot} to ${config.bucket} on S3!`));
if (callback) {
return callback(null, '');
}
return Promise.resolve('');
}).catch(error => {
console.error(('error: '+error).red);
if (callback) {
return callback(error);
}
return Promise.reject(error);
});
};
function setup(options) {
config = options;
if (config.region) {
AWS.config.region = config.region;
} else {
AWS.config.region = 'us-east-1';
}
if (!config.publicRoot) {
return Promise.reject('Must specify publicRoot');
}
if (!config.bucket) {
return Promise.reject('Must specify bucket');
}
if (!config.acl) {
config.acl = 'public-read';
}
config.concurrentRequests = config.concurrentRequests || 10;
s3 = new AWS.S3();
cloudfront = new AWS.CloudFront();
return Promise.resolve();
}
function startDeploy() {
return getFiles().then(uploadFiles).then(createInvalidation);
}
function getFiles() {
return new Promise((resolve, reject) => {
new Glob('**/*.*', { cwd: config.publicRoot }, (err, files) => {
if (err) {
return reject(err);
}
const addHeaders = Array.isArray(config.putObjectParams) && config.putObjectParams.length > 0;
const addMetadata = Array.isArray(config.metadata) && config.metadata.length > 0;
files = files.filter(f => !Fs.lstatSync(Path.join(config.publicRoot, f)).isDirectory());
resolve(files.map(f => {
const extraHeaders = {};
if (addHeaders) {
config.putObjectParams.forEach(h => {
try {
if (h.match.test(f)) {
Object.assign(extraHeaders, h.tags);
}
} catch (e) {
console.error('Error with additional putObject parameters', e);
}
});
}
const extraMetadata = {};
if (addMetadata) {
config.metadata.forEach(m => {
try {
if (m.match.test(f)) {
Object.assign(extraMetadata, m.tags);
}
} catch (e) {
console.error('Error with metadata', e);
}
});
}
const body = Fs.readFileSync(Path.join(config.publicRoot, f));
return {
body: body,
type: Mime.getType(f),
md5: Crypto.createHash('md5').update(body).digest('hex'),
path: Path.parse(f),
extraHeaders,
extraMetadata
};
}));
});
});
}
function checkIfUploadRequired(file, callback) {
const key = Path.join(file.path.dir, file.path.base).replace(/\\/g,'/');
s3.headObject({
Bucket: config.bucket,
Key: key
}, (err, data) => {
if (err && err.code === 'NotFound') {
return callback(null, true);
} else if (err) {
return callback(err);
}
if (data.Metadata['content-md5'] === file.md5) {
return callback(null, false);
}
callback(null, true);
});
}
function uploadFile(file, callback) {
const key = Path.join(file.path.dir, file.path.base).replace(/\\/g,'/');
const params = {
...file.extraHeaders,
Bucket: config.bucket,
Key: key,
ACL: config.acl,
Body: file.body,
ContentType: file.type,
Metadata: {
...file.extraMetadata,
'Content-MD5': file.md5
}
};
s3.putObject(params, (err) => {
if (err) {
return callback(err);
}
status.uploaded++;
printProgress('Uploaded', file.path.dir + '/' + file.path.base);
callback(null);
});
}
function uploadFiles(files) {
status.total = files.length;
const processFile = (file, callback) => {
checkIfUploadRequired(file, (err, required) => {
if (err) {
return callback(err);
}
if (required) {
return uploadFile(file, callback);
}
status.skipped++;
printProgress('Skipped', file.path.dir + '/' + file.path.base);
return callback();
});
};
return new Promise((resolve, reject) => {
Async.eachLimit(files, config.concurrentRequests, processFile, err => {
if (err) {
return reject(err);
}
console.log('\n');
resolve();
});
});
}
function printProgress(action, file) {
readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0);
process.stdout.write('\r' +
status.uploaded + ' uploaded / ' +
status.skipped + ' skipped / ' +
status.total + ' total --- ' +
(((status.uploaded + status.skipped) / status.total) * 100).toFixed(2) +'% complete' +
' --- ' + action + ' ' + file);
}
function createInvalidation() {
return new Promise((resolve, reject) => {
if (!config.cloudFrontId) {
return resolve();
}
console.log('\nCreating CloudFront invalidation...');
var params = {
DistributionId: config.cloudFrontId,
InvalidationBatch: {
CallerReference: (new Date()).toISOString(),
Paths: {
Quantity: 1,
Items: ['/*']
}
}
};
cloudfront.createInvalidation(params, function(err) {
if (err) {
return reject(err);
}
resolve();
});
});
}