1 | import * as AWS from "aws-sdk";
|
2 | import * as fs from "fs";
|
3 | import * as glob from "glob";
|
4 | import * as fetch from "isomorphic-fetch";
|
5 | import * as path from "path";
|
6 | import * as pkg from "../../package.json";
|
7 | import { S3Uploader } from "../classes/uploader";
|
8 |
|
9 |
|
10 | const s3 = new AWS.S3();
|
11 | const s3Bucket = "static-asset";
|
12 | const baseKey = "dotcom-core";
|
13 | const version = pkg.version;
|
14 | const libDir = "lib";
|
15 |
|
16 |
|
17 | const uploader = new S3Uploader();
|
18 |
|
19 |
|
20 | const files = glob.sync(`${libDir}/**/*.?(css|html|js|map)`);
|
21 |
|
22 |
|
23 | const keys: Array<[string, string]> = files.map((filename) => {
|
24 | const filePath = path.join(process.cwd(), filename);
|
25 | const basename = path.relative(path.join(process.cwd(), libDir), filename);
|
26 | const body = fs.readFileSync(filePath).toString();
|
27 | const key = `${baseKey}/${version}/${basename}`;
|
28 |
|
29 | return [key, body];
|
30 | });
|
31 |
|
32 |
|
33 | const promises = keys.map(([key, body]) => uploader.upload({ key, body }));
|
34 |
|
35 |
|
36 | const componentsKey = `${baseKey}/version.json`;
|
37 | const componentsBody = JSON.stringify({
|
38 | version,
|
39 | });
|
40 |
|
41 | Promise.all([
|
42 | ...promises,
|
43 | uploader.upload({
|
44 | key: componentsKey,
|
45 | body: componentsBody,
|
46 | }),
|
47 | ])
|
48 | .then(async (results) => {
|
49 |
|
50 | console.log(`Uploaded ${results.length} files to s3.`);
|
51 |
|
52 |
|
53 | const purges = [...keys, [componentsKey, ]].map(([key]) => {
|
54 | const url = `https://assets.staticlp.com/${key}`;
|
55 |
|
56 | console.log(`Purging ${url}.`);
|
57 |
|
58 | return fetch(url, {
|
59 | method: "PURGE",
|
60 | });
|
61 | });
|
62 | const responses = await Promise.all(purges);
|
63 | const messages = await Promise.all(responses.map((r) => r.json()));
|
64 | const oks = messages.filter(m => m.status === "ok").length;
|
65 |
|
66 |
|
67 | console.log(`Done.`);
|
68 | })
|
69 | .catch((error) => {
|
70 |
|
71 | console.error(error);
|
72 |
|
73 | process.exit(1);
|
74 | });
|