This repository has been archived by the owner on Oct 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (63 loc) · 2.5 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
/*jslint node:true*/
module.exports = function (deployDir, amazonBucket, amazonKey, amazonSecret) {
'use strict';
amazonKey = amazonKey || process.env.AWS_ACCESS_KEY_ID;
amazonSecret = amazonSecret || process.env.AWS_SECRET_ACCESS_KEY;
if (!deployDir) { return console.error('Missing source directory'); }
if (!amazonBucket) { return console.error('Missing Amazon Bucket name'); }
if (!amazonKey) { return console.error('Missing Amazon Key'); }
if (!amazonSecret) { return console.error('Missing Amazon Secret'); }
/*jslint stupid:true*/
var fs = require('fs'),
path = require('path'),
exec = require('child_process').execSync,
s3 = require('s3'),
gzipList = /\.(html|xml|svg|js|json|css|txt|md)$/,
tmpDir,
sizeMap = {},
client = s3.createClient({
s3Options: {
accessKeyId: amazonKey,
secretAccessKey: amazonSecret,
region: 'eu-west-1'
}
}),
params = {
localDir: deployDir || 'build',
deleteRemoved: true,
s3Params: { Bucket: amazonBucket },
getS3Params: function (localFile, stat, callback) {
/*jslint unparam:true*/
console.log('>', path.relative(tmpDir, localFile));
var p = {};
if (localFile.match(gzipList)) {
p.ContentEncoding = 'gzip';
p.Metadata = { 'raw-content-length': sizeMap[localFile] };
}
if (localFile.match(/\.(html)/)) {
p.CacheControl = 'no-cache';
} else {
p.CacheControl = 'max-age=31536000';
}
callback(null, p);
}
},
uploader;
tmpDir = exec('mktemp -dt XXXXXX').toString().replace(/\n/, '');
exec('cp -r ' + deployDir + '* ' + tmpDir);
fs.readdirSync(tmpDir).forEach(function (f) {
if (!f.match(gzipList)) { return; }
f = path.resolve(tmpDir, f);
var size = exec('wc -c <"' + '/' + f + '"').toString();
sizeMap[f] = size.replace(/\s/g, '');
exec('gzip -9 ' + f);
exec('mv ' + f + '.gz' + ' ' + f);
});
params.localDir = tmpDir;
uploader = client.uploadDir(params);
uploader.on('error', function (error) {
console.error(error);
});
//on end being triggered before upload finishes
//uploader.on('end', function() { d.resolve('Done Uploading'); });
};