-
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build(examples): update example build scripts
- Loading branch information
1 parent
ef7604b
commit 4acc452
Showing
4 changed files
with
106 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters