-
-
Notifications
You must be signed in to change notification settings - Fork 151
/
deploy-example
executable file
·78 lines (64 loc) · 2.41 KB
/
deploy-example
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
#!/usr/bin/env node
const fs = require("fs");
const execSync = require("child_process").execSync;
const getMime = require("@thi.ng/mime").preferredType;
const EXAMPLE = process.argv[2];
const BUILD = `examples/${EXAMPLE}/build/`;
const DEST_DIR = `/umbrella/${EXAMPLE}`;
const BUCKET = `s3://demo.thi.ng${DEST_DIR}`;
const CF_DISTRO = "EL2F1HMDPZ2RL";
const PROFILE = "thing-umbrella";
const OPTS = `--profile ${PROFILE} --acl public-read`;
const GZOPTS = `${OPTS} --content-encoding gzip`;
const NEVER_GZIP = new Set(["mp4"]);
const args = new Set(process.argv.slice(3).map((x) => x.substr(2)));
console.log(args);
execSync(`find examples/${EXAMPLE} -type f -name '*.DS_Store' -ls -delete`);
function* files(dir, ext = "", maxDepth = Infinity, depth = 0) {
if (depth >= maxDepth) return;
try {
for (let f of fs.readdirSync(dir)) {
const curr = dir + "/" + f;
if (f.endsWith(ext)) {
yield curr;
} else if (fs.statSync(curr).isDirectory()) {
yield* files(curr, ext, maxDepth, depth + 1);
}
}
} catch (_) {}
}
const uploadAssets = (dir, opts) => {
opts = { ext: "", gzip: true, depth: Infinity, ...opts };
for (let f of files(`${BUILD}${dir}`, opts.ext, opts.depth)) {
const fd = `${BUCKET}/${f
.replace(BUILD, "")
.substr(dir === "" ? 1 : 0)}`;
const ext = f.substr(f.lastIndexOf(".") + 1);
const type = getMime(ext);
console.log(f, "->", fd, type);
opts.process && opts.process(f);
if (opts.gzip && !NEVER_GZIP.has(ext)) {
execSync(`gzip -9 ${f}`);
execSync(
`aws s3 cp ${f}.gz ${fd} ${GZOPTS} --content-type ${type}`
);
} else {
execSync(`aws s3 cp ${f} ${fd} ${OPTS} --content-type ${type}`);
}
}
};
const interpolateFile = (tpl) => (src) => {
let body = fs.readFileSync(src, "utf-8");
body = body.replace(/\{\{(\w+)\}\}/g, (_, id) => tpl[id]);
fs.writeFileSync(src, body);
};
const include = (id) => args.has(id) || args.has("all");
uploadAssets("assets");
uploadAssets("js", { ext: ".js", depth: 2 });
uploadAssets("", { ext: ".js", depth: 1 });
uploadAssets("", { ext: ".html" });
console.log("invaliding", DEST_DIR);
execSync(
`aws cloudfront create-invalidation --distribution-id ${CF_DISTRO} --paths "${DEST_DIR}/*" --profile ${PROFILE}`
);
console.log("done");