UNPKG

2.22 kBPlain TextView Raw
1import * as AWS from "aws-sdk";
2import * as fs from "fs";
3import * as glob from "glob";
4import * as fetch from "isomorphic-fetch";
5import * as path from "path";
6import * as pkg from "../../package.json";
7import { S3Uploader } from "../classes/uploader";
8
9// Stand up s3 and base variables
10const s3 = new AWS.S3();
11const s3Bucket = "static-asset";
12const baseKey = "dotcom-core";
13const version = pkg.version;
14const libDir = "lib";
15
16// Create an instance of the uploader
17const uploader = new S3Uploader();
18
19// Grab built files from the lib directory
20const files = glob.sync(`${libDir}/**/*.?(css|html|js|map)`);
21
22// Generate a list of keys and contents to upload
23const 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// Perform the upload
33const promises = keys.map(([key, body]) => uploader.upload({ key, body }));
34
35// Create a version.json file based on what's in the package.json
36const componentsKey = `${baseKey}/version.json`;
37const componentsBody = JSON.stringify({
38 version,
39});
40
41Promise.all([
42 ...promises,
43 uploader.upload({
44 key: componentsKey,
45 body: componentsBody,
46 }),
47])
48.then(async (results) => {
49 // tslint:disable-next-line:no-console
50 console.log(`Uploaded ${results.length} files to s3.`);
51
52 // Everything is uploaded, now just purge fastly cache in case this is an override of an existing version
53 const purges = [...keys, [componentsKey, ]].map(([key]) => {
54 const url = `https://assets.staticlp.com/${key}`;
55 // tslint:disable-next-line:no-console
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 // tslint:disable-next-line:no-console
67 console.log(`Done.`);
68})
69.catch((error) => {
70 // tslint:disable-next-line:no-console
71 console.error(error);
72
73 process.exit(1);
74});