Skip to content

Commit

Permalink
build(examples): update example build scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Dec 8, 2020
1 parent ef7604b commit 4acc452
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 3 deletions.
4 changes: 2 additions & 2 deletions scripts/build-examples
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ for e in $examples; do
node_modules/.bin/terser -c -m --ecma 6 -o "$js" "$js"
sed -i '' -E 's/Object\.defineProperty([a-z]\+,"__esModule",{value:\!0});//g' "$js"
sed -i '' -E 's/Object\.defineProperty([a-z]\+,"__esModule",{value:\!0}),\([^f]\)/(\1)/g' "$js"
gzip -c "$js" > "$js".gz
ls -la "$js"*
# gzip -c "$js" > "$js".gz
ls -la "$js"
done
# sed -i 's/src="\//src=".\//g' $e/out/index.html
else
Expand Down
27 changes: 27 additions & 0 deletions scripts/cleanup
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env node
const fs = require("fs");

/**
* Recursively reads given directory and returns file names matching
* given extension.
*
* @param {*} dir
* @param {*} ext
*/
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);
}
}
}

for (let f of files("packages", ".map")) {
if (f.indexOf("/lib/") === -1) {
console.log(f);
fs.unlinkSync(f);
}
}
76 changes: 76 additions & 0 deletions scripts/deploy
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/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 = "toxi-full";
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 = "") {
try {
for (let f of fs.readdirSync(dir)) {
const curr = dir + "/" + f;
if (fs.statSync(curr).isDirectory()) {
yield* files(curr, ext);
} else if (f.endsWith(ext)) {
yield curr;
}
}
} catch (_) {}
}

const uploadAssets = (dir, opts) => {
opts = { ext: "", gzip: true, ...opts };
for (let f of files(`${BUILD}${dir}`, opts.ext)) {
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");
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");
2 changes: 1 addition & 1 deletion scripts/make-example
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ module.exports = {
installTypes: true,
},
buildOptions: {
baseUrl: "/$1",
baseUrl: "/umbrella/$1",
},
};
EOF
Expand Down

0 comments on commit 4acc452

Please sign in to comment.