Skip to content

Commit

Permalink
build: update/rewrite deploy scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 2, 2021
1 parent 2a1031d commit e56cae6
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"doc:ae": "lerna run doc:ae && scripts/collect-apis",
"doc:examples": "ts-node -P tools/tsconfig.json tools/src/readme-examples.ts",
"examples": "scripts/build-examples",
"pub": "lerna publish --registry https://registry.npmjs.org/ && yarn doc && scripts/upload-docs",
"pub": "lerna publish --registry https://registry.npmjs.org/ && yarn doc && scripts/deploy-docs",
"test": "yarn build && yarn test:only",
"test:only": "lerna run test",
"tool:imports": "ts-node -P tools/tsconfig.json tools/src/check-imports.ts",
Expand Down
108 changes: 108 additions & 0 deletions scripts/deploy-docs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#!/usr/bin/env node
const fs = require("fs");
const execSync = require("child_process").execSync;

const PKG = process.argv[2];

const BUCKET = "s3://docs.thi.ng";
const PREFIX = "/umbrella";
const S3_OPTS = `--profile thing-umbrella --acl public-read`;
const SYNC_OPTS = `${S3_OPTS} --include "*" --exclude "*.sass" --exclude "*.ts"`;

const MINIFY_OPTS =
"--file-ext html --collapse-whitespace --remove-comments --remove-optional-tags --remove-redundant-attributes --remove-script-type-attributes --remove-tag-whitespace --use-short-doctype --minify-css true";

function* files(dir, ext) {
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);
}
}
}

const sanitizeFile = (f) => {
let updated = false;
const src = fs
.readFileSync(f, "utf-8")
.replace(
/\{@link @thi\.ng\/([a-z0-9-]+)(#(\w+))?\s*\|\s*([^\}]+)\}/g,
(_, id, label) => {
updated = true;
return `<a href="${PREFIX}/${id}/">${label}</a>`;
}
)
.replace(
/\{@link @thi\.ng\/([a-z0-9-]+)#(\w+)?\s*\}/g,
(_, id, sym) => {
updated = true;
let path = `${PREFIX}/${id}/`;
if (sym.startsWith("I")) {
path += `interfaces/${sym.toLowerCase()}.html`;
} else if (/^[a-z]/.test(sym)) {
path += `modules.html#${sym.toLowerCase()}`;
}
return `<a href="${path}">${sym}</a>`;
}
)
.replace(/\{@link @thi\.ng\/([a-z0-9-]+)#?\s*\}/g, (_, id) => {
updated = true;
return `<a href="${PREFIX}/${id}/">@thi.ng/${id}</a>`;
});
if (updated) {
console.log("sanitizing:", f);
fs.writeFileSync(f, src, "utf-8");
}
};

const sanitizePackage = (root) => {
for (let f of files(root, ".html")) {
sanitizeFile(f);
}
};

const minifyPackage = (root) => {
console.log("minifying", root);
execSync(
`node_modules/.bin/html-minifier-terser ${MINIFY_OPTS} --input-dir ${root} --output-dir ${root}`
);
};

const syncPackage = (id, root) => {
console.log("syncing", root);
console.log(
execSync(
`aws s3 sync ${root} ${BUCKET}${PREFIX}/${id} ${SYNC_OPTS}`
).toString()
);
};

const processPackage = (id) => {
console.log("processing", id);
const root = `packages/${id}/doc`;
try {
sanitizePackage(root);
minifyPackage(root);
syncPackage(id, root);
} catch (e) {
console.warn(e);
}
};

if (PKG) {
processPackage(PKG);
} else {
for (let pkg of fs.readdirSync("packages")) {
if (fs.statSync(`packages/${pkg}`).isDirectory()) {
processPackage(pkg);
}
}
}

execSync(
`node_modules/.bin/ts-node -P tools/tsconfig.json tools/src/doc-table.ts`
);

execSync(`aws s3 cp docs.html ${BUCKET}/index.html ${S3_OPTS}`);
2 changes: 1 addition & 1 deletion scripts/deploy → scripts/deploy-example
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const BUILD = `examples/${EXAMPLE}/build/`;
const DEST_DIR = `/umbrella/${EXAMPLE}`;
const BUCKET = `s3://demo.thi.ng${DEST_DIR}`;
const CF_DISTRO = "EL2F1HMDPZ2RL";
const PROFILE = "toxi-full";
const PROFILE = "thing-umbrella";
const OPTS = `--profile ${PROFILE} --acl public-read`;
const GZOPTS = `${OPTS} --content-encoding gzip`;

Expand Down
5 changes: 5 additions & 0 deletions scripts/invalidate
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh
aws cloudfront create-invalidation \
--distribution-id EL2F1HMDPZ2RL \
--paths "/umbrella/$1/*" \
--profile thing-umbrella
23 changes: 0 additions & 23 deletions scripts/upload-docs

This file was deleted.

0 comments on commit e56cae6

Please sign in to comment.