-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.js
144 lines (111 loc) · 2.65 KB
/
deploy.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
var fs = require('fs');
var path = require('path');
var zlib = require('zlib');
var AWS = require('aws-sdk');
var async = require('async');
var config = require(__dirname + '/config')();
var opts = {
s3: {
user: config.services.s3.user,
region: config.services.s3.region,
key: config.services.s3.key,
secret: config.services.s3.secret,
bucket: config.services.s3.bucket,
}
}
AWS.config.region = opts.s3.region;
AWS.config.update({
accessKeyId: opts.s3.key,
secretAccessKey:opts.s3.secret
});
var root = __dirname + '/htdocs'
function fileMime(f) {
var mime = 'application/octet-stream';
var fn = f.toLowerCase();
if (fn.indexOf('.html') >= 0) mime = 'text/html';
else if (fn.indexOf('.css') >= 0) mime = 'text/css';
else if (fn.indexOf('.json') >= 0) mime = 'application/json';
else if (fn.indexOf('.js') >= 0) mime = 'application/javascript';
else if (fn.indexOf('.png') >= 0) mime = 'image/png';
else if (fn.indexOf('.jpg') >= 0) mime = 'image/jpg';
else if (fn.indexOf('.mp3') >= 0) mime = 'audio/mpeg';
else if (fn.indexOf('.txt') >= 0) mime = 'text/plain';
return mime;
}
var putFile = function(f, done){
if(!done){
done = () => {}
}
var src = root + '/' + f;
var target = f;
var mime = fileMime(f);
console.log('>', mime, f);
var s3obj = new AWS.S3({
params: {
Bucket: opts.s3.bucket,
Key: target,
ContentType: mime,
ACL:'public-read',
CacheControl: 'max-age=300000'
}
});
var body = fs.createReadStream(src);
s3obj.upload({
Body: body
}).send(function(err, data) {
if(err){
console.log('error:', err);
}
done();
});
}
var putFileIfNew = function(f, done){
if(!done){
done = () => {}
}
var src = root + '/' + f;
var target = f;
var mime = fileMime(f);
console.log(mime, f);
var s3 = new AWS.S3();
s3.headObject({
Bucket: opts.s3.bucket,
Key: target
}, function (err, metadata) {
// file already on s3
if(!err){
return done();
}
putFile(f, done);
});
}
// always put index
putFile('index.html', function(){})
var addAll = function (f, files) {
if(!f){
f = ''
}
if(!files){
files = [];
}
if (f.substr(0, 1) === '/') {
f = f.substr(1);
}
var src = root + '/' + f;
if (fs.lstatSync(src).isDirectory()) {
fs.readdirSync(src).forEach(function (file) {
if (file.substr(0, 1) === '.') {
return;
}
if (file.substr(0, 1) === '#') {
return;
}
files = files.concat(addAll(f + '/' + file));
});
} else {
files.push(f);
}
return files;
};
var files = addAll();
async.eachSeries(files, putFileIfNew )