forked from ittech17/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.js
139 lines (124 loc) · 3.59 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
var AWS = require('aws-sdk');
var fs = require('fs');
var crypto = require('crypto');
var mime = require('mime-types');
var s3 = new AWS.S3();
function upload(bucket, key) {
console.log('Upload file: ' + key);
return new Promise((resolve, reject) => {
fs.readFile(key, function (err, data) {
if (err) {
console.error('Cannot read file: ' + key);
reject(err);
} else {
var contentType = mime.lookup(key);
var s3File = {
Bucket: bucket,
Key: key,
ACL: 'public-read',
Body: data
};
if (contentType) {
s3File.ContentType = contentType;
}
console.log('Content-Type: ' + contentType);
s3.putObject(s3File, function (err, data) {
if (err) {
console.error('Cannot upload: ' + key);
reject(err);
} else {
console.log('Uploaded: ' + key);
resolve(data);
}
});
}
});
})
}
function getFileHash(filename) {
var data = fs.readFileSync(filename);
return crypto.createHash('md5').update(data).digest('hex');
}
function processDirectory(newTrackFile, oldTrackFile, toBeUploaded, dir) {
console.log('Process directory: ' + dir);
return new Promise((resolve, reject) => {
fs.readdir(dir, (err, children) => {
if (err) {
console.error('Cannot process directory: ', dir);
reject(err);
} else {
var promises = [];
for (var child of children) {
var path;
if (dir === '.') {
path = child;
} else {
path = dir + '/' + child;
}
if (fs.lstatSync(path).isDirectory()) {
promises.push(processDirectory(newTrackFile, oldTrackFile, toBeUploaded, path));
} else {
processFile(newTrackFile, oldTrackFile, toBeUploaded, path);
}
}
Promise.all(promises).then(resolve);
}
});
});
}
function processFile(newTrackFile, oldTrackFile, toBeUploaded, path) {
var hash = getFileHash(path);
// keep hash
newTrackFile[path] = hash;
// check modified
if (oldTrackFile[path] != hash) {
toBeUploaded.push(path);
}
}
function uploadTrackFile(bucket, trackFile, content) {
return new Promise((resolve, reject) => {
fs.writeFile(trackFile, JSON.stringify(content), (err) => {
if (err) {
console.error('Cannot write track file');
reject(err);
} else {
upload(bucket, trackFile).then(() => {
resolve();
});
}
});
});
}
function run() {
var bucket = process.argv[2];
var trackFile = process.argv[3] + '.json'; // branch name or PR ID
var s3TrackFile = {
Bucket: bucket,
Key: trackFile
};
s3.getObject(s3TrackFile, function (err, data) {
var oldTrackFile;
if (err) {
console.error('Cannot get file: ' + trackFile, err);
oldTrackFile = {};
} else {
console.log('Retrieved file: ' + trackFile);
oldTrackFile = JSON.parse(data.Body.toString());
}
// delete the old track file in case this process is interrupted,
// the next one will start over
return uploadTrackFile(bucket, trackFile, {}).then(() => {
var newTrackFile = {};
var toBeUploaded = [];
return processDirectory(newTrackFile, oldTrackFile, toBeUploaded, '.').then(() => {
var promises = toBeUploaded.map((file) => {
return upload(bucket, file);
});
return Promise.all(promises).then(() => {
return uploadTrackFile(bucket, trackFile, newTrackFile)
});
});
});
});
}
run();