// Writes app/crafted-link.js with an arewehuman replay link for recipe.md. // The link carries the whole recording in its URL fragment (gzip + base64url), // which the viewer accepts as-is, so nothing needs to be hosted. // Until recipe.md has been recorded, the link is null and stays hidden. import { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'; import { gzipSync } from 'node:zlib'; const dir = '.arewehuman/recipe.md'; const logs = existsSync(dir) ? readdirSync(dir).filter((f) => f.endsWith('.awh.jsonl')) : []; if (logs.length > 1) { throw new Error(`Expected one recording in ${dir}, found ${logs.length}`); } let url = null; if (logs.length === 1) { const recording = readFileSync(`${dir}/${logs[0]}`); const z = gzipSync(recording, { level: 9 }).toString('base64url'); url = `https://magland.github.io/arewehuman/#/view?z=${z}`; } writeFileSync('app/crafted-link.js', `// Generated by scripts/crafted-link.mjs; do not edit.\nwindow.CRAFTED_LINK = ${JSON.stringify(url)};\n`); console.log(url ? `app/crafted-link.js: replay link for ${logs[0]} (${url.length} characters)` : `app/crafted-link.js: no recording in ${dir} yet`);