-
Notifications
You must be signed in to change notification settings - Fork 4
/
deploy.js
180 lines (145 loc) · 4.07 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
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
'use strict';
const S3Bucket = 'docs.smartloop.ai';
const CloudfrontDistributionId = 'ER92RU0J2HCS0';
//const S3Bucket = 'stage-console.recime.io';
//const CloudfrontDistributionId = 'EYLJ8B8R99MTE';
const ShortCacheDuration = 60 * 60; // 1 hour
const LongCacheDuration = 60 * 60 * 24 * 365; // 365 Days
const path = require('path');
const spawn = require('child_process').spawn;
const aws = require('aws-sdk');
const fs = require('fs-extra');
const mime = require('mime-types');
aws.config.update({
region: 'us-west-2',
maxRetries: 10,
retryDelayOptions: {
base: 300
}
});
const distDirectory = path.join(__dirname, '.vuepress', 'dist');
const s3 = new aws.S3();
const cloudfront = new aws.CloudFront();
function build() {
return new Promise((resolve, reject) => {
const ps = spawn('npm', ['run', 'docs:build'], {
cwd: __dirname,
stdio: 'ignore'
});
ps.on(
'close',
code => (code ? reject(new Error(`Error building documentation!`)) : resolve())
);
});
}
function getFiles() {
function load(location) {
return new Promise((resolve, reject) => {
fs.readdir(location, (error, entries) => {
if (error) {
return reject(error);
}
const directories = [];
const files = [];
entries.forEach(entry => {
const fullLocation = path.join(location, entry);
const stat = fs.statSync(fullLocation);
if (stat.isFile()) {
files.push(fullLocation);
} else if (stat.isDirectory()) {
directories.push(fullLocation);
}
});
const tasks = directories.map(load);
return Promise.all(tasks)
.then(results => {
results.forEach(result => files.push(...result));
return resolve(files);
})
.catch(reject);
});
});
}
return load(distDirectory);
}
function upload(files) {
function put(file) {
return new Promise((resolve, reject) => {
const fileName = file.substring(distDirectory.length + 1);
const mimeType = mime.lookup(fileName);
const cacheControl =
path.basename(fileName) === 'index.html'
? `private,max-age=${ShortCacheDuration}`
: `public,max-age=${LongCacheDuration}`;
const params = {
Bucket: S3Bucket,
CacheControl: cacheControl,
Key: fileName,
Body: fs.createReadStream(file)
};
if (mimeType) {
params.ContentType = mimeType;
}
s3.upload(params, error => (error ? reject(error) : resolve()));
});
}
const tasks = files.map(put);
return Promise.all(tasks);
}
function invalidate(files) {
function poll(id, resolve, reject) {
setTimeout(() => {
const params = {
DistributionId: CloudfrontDistributionId,
Id: id
};
cloudfront.getInvalidation(params, (error, result) => {
if (error) {
return reject(error);
}
if (result.Invalidation.Status === 'Completed') {
return resolve();
}
poll(id, resolve, reject);
});
}, 3000);
}
return new Promise((resolve, reject) => {
const params = {
DistributionId: CloudfrontDistributionId,
InvalidationBatch: {
CallerReference: Date.now().toString(),
Paths: {
Quantity: files.length,
Items: files
}
}
};
cloudfront.createInvalidation(params, (error, result) => {
if (error) {
return reject(error);
}
return poll(result.Invalidation.Id, resolve, reject);
});
});
}
function remove(location) {
return new Promise((resolve, reject) => {
fs.remove(location, error => {
return error ? reject(error) : resolve();
});
});
}
function deploy() {
return build()
.then(() => getFiles())
.then(files => upload(files).then(()=> files))
.then((files) => remove(distDirectory).then(()=> files))
.then((files) => {
const f = files.map((file)=>{
return "/" + path.parse(file).base;
});
invalidate(f);
});
}
module.exports = deploy;