forked from gatsby-uc/gatsby-plugin-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin.js
executable file
·359 lines (358 loc) · 15.3 KB
/
bin.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env node
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.deploy = void 0;
require("@babel/polyfill");
require("fs-posix");
const s3_1 = __importDefault(require("aws-sdk/clients/s3"));
const sts_1 = __importDefault(require("aws-sdk/clients/sts"));
const yargs_1 = __importDefault(require("yargs"));
const constants_1 = require("./constants");
const fs_extra_1 = require("fs-extra");
const klaw_1 = __importDefault(require("klaw"));
const pretty_error_1 = __importDefault(require("pretty-error"));
const stream_to_promise_1 = __importDefault(require("stream-to-promise"));
const ora_1 = __importDefault(require("ora"));
const chalk_1 = __importDefault(require("chalk"));
const path_1 = require("path");
const url_1 = require("url");
const fs_1 = __importDefault(require("fs"));
const util_1 = __importDefault(require("util"));
const minimatch_1 = __importDefault(require("minimatch"));
const mime_1 = __importDefault(require("mime"));
const inquirer_1 = __importDefault(require("inquirer"));
const aws_sdk_1 = require("aws-sdk");
const crypto_1 = require("crypto");
const is_ci_1 = __importDefault(require("is-ci"));
const util_2 = require("./util");
const async_1 = require("async");
const proxy_agent_1 = __importDefault(require("proxy-agent"));
const pe = new pretty_error_1.default();
const OBJECTS_TO_REMOVE_PER_REQUEST = 1000;
const promisifiedParallelLimit = util_1.default.promisify(async_1.parallelLimit);
const guessRegion = (s3, constraint) => { var _a; return (_a = constraint !== null && constraint !== void 0 ? constraint : s3.config.region) !== null && _a !== void 0 ? _a : aws_sdk_1.config.region; };
const getBucketInfo = async (config, s3) => {
try {
const { $response } = await s3.getBucketLocation({ Bucket: config.bucketName }).promise();
const responseData = $response.data; // Fix type to be possibly `null` instead of possibly `void`
const detectedRegion = guessRegion(s3, responseData === null || responseData === void 0 ? void 0 : responseData.LocationConstraint);
return {
exists: true,
region: detectedRegion,
};
}
catch (ex) {
if (ex.code === 'NoSuchBucket') {
return {
exists: false,
region: guessRegion(s3),
};
}
throw ex;
}
};
const getParams = (path, params) => {
let returned = {};
for (const key of Object.keys(params)) {
if (minimatch_1.default(path, key)) {
returned = Object.assign(Object.assign({}, returned), params[key]);
}
}
return returned;
};
const listAllObjects = async (s3, bucketName, bucketPrefix) => {
const list = [];
let token;
do {
const response = await s3
.listObjectsV2({
Bucket: bucketName,
ContinuationToken: token,
Prefix: bucketPrefix,
})
.promise();
if (response.Contents) {
list.push(...response.Contents);
}
token = response.NextContinuationToken;
} while (token);
return list;
};
const createSafeS3Key = (key) => {
if (path_1.sep === '\\') {
return key.replace(/\\/g, '/');
}
return key;
};
exports.deploy = async ({ yes, bucket, userAgent } = {}) => {
var _a;
const spinner = ora_1.default({ text: 'Retrieving bucket info...', color: 'magenta', stream: process.stdout }).start();
let dontPrompt = yes;
const uploadQueue = [];
try {
const config = await fs_extra_1.readJson(constants_1.CACHE_FILES.config);
const params = await fs_extra_1.readJson(constants_1.CACHE_FILES.params);
const routingRules = await fs_extra_1.readJson(constants_1.CACHE_FILES.routingRules);
const redirectObjects = fs_1.default.existsSync(constants_1.CACHE_FILES.redirectObjects)
? await fs_extra_1.readJson(constants_1.CACHE_FILES.redirectObjects)
: [];
// Override the bucket name if it is set via command line
if (bucket) {
config.bucketName = bucket;
}
let httpOptions = {};
if (process.env.HTTP_PROXY) {
httpOptions = {
agent: proxy_agent_1.default(process.env.HTTP_PROXY),
};
}
httpOptions = Object.assign({ agent: process.env.HTTP_PROXY ? proxy_agent_1.default(process.env.HTTP_PROXY) : undefined, timeout: config.timeout, connectTimeout: config.connectTimeout }, httpOptions);
let roleCredentials;
if (process.env.AWS_ROLE_ARN && process.env.AWS_ROLE_SESSION_NAME) {
const sts = new sts_1.default({ apiVersion: '2011-06-15' });
const { Credentials } = await sts
.assumeRole({
RoleArn: process.env.AWS_ROLE_ARN,
RoleSessionName: process.env.AWS_ROLE_SESSION_NAME,
})
.promise();
roleCredentials = Credentials;
}
const s3Config = {
region: config.region,
endpoint: config.customAwsEndpointHostname,
customUserAgent: userAgent !== null && userAgent !== void 0 ? userAgent : '',
httpOptions,
logger: config.verbose ? console : undefined,
retryDelayOptions: {
customBackoff: process.env.fixedRetryDelay ? () => Number(config.fixedRetryDelay) : undefined,
},
};
if (roleCredentials) {
console.log(`Uploading to S3 using role ${process.env.AWS_ROLE_ARN} and session ${process.env.AWS_ROLE_SESSION_NAME}`);
s3Config.credentials = {
accessKeyId: roleCredentials.AccessKeyId,
secretAccessKey: roleCredentials.SecretAccessKey,
sessionToken: roleCredentials.SessionToken,
};
}
const s3 = new s3_1.default(s3Config);
const { exists, region } = await getBucketInfo(config, s3);
if (is_ci_1.default && !dontPrompt) {
dontPrompt = true;
}
if (!dontPrompt) {
spinner.stop();
console.log(chalk_1.default `
{underline Please review the following:} ({dim pass -y next time to skip this})
Deploying to bucket: {cyan.bold ${config.bucketName}}
In region: {yellow.bold ${region !== null && region !== void 0 ? region : 'UNKNOWN!'}}
Gatsby will: ${!exists
? chalk_1.default `{bold.greenBright CREATE}`
: chalk_1.default `{bold.blueBright UPDATE} {dim (any existing website configuration will be overwritten!)}`}
`);
const { confirm } = await inquirer_1.default.prompt([
{
message: 'OK?',
name: 'confirm',
type: 'confirm',
},
]);
if (!confirm) {
throw new Error('User aborted!');
}
spinner.start();
}
spinner.text = 'Configuring bucket...';
spinner.color = 'yellow';
if (!exists) {
const createParams = {
Bucket: config.bucketName,
ACL: config.acl === null ? undefined : (_a = config.acl) !== null && _a !== void 0 ? _a : 'public-read',
};
if (config.region) {
createParams.CreateBucketConfiguration = {
LocationConstraint: config.region,
};
}
await s3.createBucket(createParams).promise();
}
if (config.enableS3StaticWebsiteHosting) {
const websiteConfig = {
Bucket: config.bucketName,
WebsiteConfiguration: {
IndexDocument: {
Suffix: 'index.html',
},
ErrorDocument: {
Key: '404.html',
},
},
};
if (routingRules.length) {
websiteConfig.WebsiteConfiguration.RoutingRules = routingRules;
}
await s3.putBucketWebsite(websiteConfig).promise();
}
spinner.text = 'Listing objects...';
spinner.color = 'green';
const objects = await listAllObjects(s3, config.bucketName, config.bucketPrefix);
const keyToETagMap = objects.reduce((acc, curr) => {
if (curr.Key && curr.ETag) {
acc[curr.Key] = curr.ETag;
}
return acc;
}, {});
spinner.color = 'cyan';
spinner.text = 'Syncing...';
const publicDir = path_1.resolve('./public');
const stream = klaw_1.default(publicDir);
const isKeyInUse = {};
stream.on('data', ({ path, stats }) => {
if (!stats.isFile()) {
return;
}
uploadQueue.push(async_1.asyncify(async () => {
var _a, _b;
let key = createSafeS3Key(path_1.relative(publicDir, path));
if (config.bucketPrefix) {
key = `${config.bucketPrefix}/${key}`;
}
const readStream = fs_1.default.createReadStream(path);
const hashStream = readStream.pipe(crypto_1.createHash('md5').setEncoding('hex'));
const data = await stream_to_promise_1.default(hashStream);
const tag = `"${data}"`;
const objectUnchanged = keyToETagMap[key] === tag;
isKeyInUse[key] = true;
if (!objectUnchanged) {
try {
const upload = new s3_1.default.ManagedUpload({
service: s3,
params: Object.assign({ Bucket: config.bucketName, Key: key, Body: fs_1.default.createReadStream(path), ACL: config.acl === null ? undefined : (_a = config.acl) !== null && _a !== void 0 ? _a : 'public-read', ContentType: (_b = mime_1.default.getType(path)) !== null && _b !== void 0 ? _b : 'application/octet-stream' }, getParams(key, params)),
});
upload.on('httpUploadProgress', evt => {
spinner.text = chalk_1.default `Syncing...
{dim Uploading {cyan ${key}} ${evt.loaded.toString()}/${evt.total.toString()}}`;
});
await upload.promise();
spinner.text = chalk_1.default `Syncing...\n{dim Uploaded {cyan ${key}}}`;
}
catch (ex) {
console.error(ex);
process.exit(1);
}
}
}));
});
const base = config.protocol && config.hostname ? `${config.protocol}://${config.hostname}` : null;
uploadQueue.push(...redirectObjects.map(redirect => async_1.asyncify(async () => {
var _a;
const { fromPath, toPath: redirectPath } = redirect;
const redirectLocation = base ? url_1.resolve(base, redirectPath) : redirectPath;
let key = util_2.withoutLeadingSlash(fromPath);
if (key.endsWith('/')) {
key = path_1.join(key, 'index.html');
}
key = createSafeS3Key(key);
if (config.bucketPrefix) {
key = util_2.withoutLeadingSlash(`${config.bucketPrefix}/${key}`);
}
const tag = `"${crypto_1.createHash('md5')
.update(redirectLocation)
.digest('hex')}"`;
const objectUnchanged = keyToETagMap[key] === tag;
isKeyInUse[key] = true;
if (objectUnchanged) {
// object with exact hash already exists, abort.
return;
}
try {
const upload = new s3_1.default.ManagedUpload({
service: s3,
params: Object.assign({ Bucket: config.bucketName, Key: key, Body: redirectLocation, ACL: config.acl === null ? undefined : (_a = config.acl) !== null && _a !== void 0 ? _a : 'public-read', ContentType: 'application/octet-stream', WebsiteRedirectLocation: redirectLocation }, getParams(key, params)),
});
await upload.promise();
spinner.text = chalk_1.default `Syncing...
{dim Created Redirect {cyan ${key}} => {cyan ${redirectLocation}}}\n`;
}
catch (ex) {
spinner.fail(chalk_1.default `Upload failure for object {cyan ${key}}`);
console.error(pe.render(ex));
process.exit(1);
}
})));
await stream_to_promise_1.default(stream);
await promisifiedParallelLimit(uploadQueue, config.parallelLimit);
if (config.removeNonexistentObjects) {
const objectsToRemove = objects
.map(obj => ({ Key: obj.Key }))
.filter(obj => {
var _a;
if (!obj.Key || isKeyInUse[obj.Key])
return false;
for (const glob of (_a = config.retainObjectsPatterns) !== null && _a !== void 0 ? _a : []) {
if (minimatch_1.default(obj.Key, glob)) {
return false;
}
}
return true;
});
for (let i = 0; i < objectsToRemove.length; i += OBJECTS_TO_REMOVE_PER_REQUEST) {
const objectsToRemoveInThisRequest = objectsToRemove.slice(i, i + OBJECTS_TO_REMOVE_PER_REQUEST);
spinner.text = `Removing objects ${i + 1} to ${i + objectsToRemoveInThisRequest.length} of ${objectsToRemove.length}`;
await s3
.deleteObjects({
Bucket: config.bucketName,
Delete: {
Objects: objectsToRemoveInThisRequest,
Quiet: true,
},
})
.promise();
}
}
spinner.succeed('Synced.');
if (config.enableS3StaticWebsiteHosting) {
const s3WebsiteDomain = util_2.getS3WebsiteDomainUrl(region !== null && region !== void 0 ? region : 'us-east-1');
console.log(chalk_1.default `
{bold Your website is online at:}
{blue.underline http://${config.bucketName}.${s3WebsiteDomain}}
`);
}
else {
console.log(chalk_1.default `
{bold Your website has now been published to:}
{blue.underline ${config.bucketName}}
`);
}
}
catch (ex) {
spinner.fail('Failed.');
console.error(pe.render(ex));
process.exit(1);
}
};
yargs_1.default
.command(['deploy', '$0'], "Deploy bucket. If it doesn't exist, it will be created. Otherwise, it will be updated.", args => args
.option('yes', {
alias: 'y',
describe: 'Skip confirmation prompt',
boolean: true,
})
.option('bucket', {
alias: 'b',
describe: 'Bucket name (if you wish to override default bucket name)',
})
.option('userAgent', {
describe: 'Allow appending custom text to the User Agent string (Used in automated tests)',
}), exports.deploy)
.wrap(yargs_1.default.terminalWidth())
.demandCommand(1, `Pass --help to see all available commands and options.`)
.strict()
.showHelpOnFail(true)
.recommendCommands()
.parse(process.argv.slice(2));
//# sourceMappingURL=bin.js.map