import { spawnSync } from 'node:child_process' import { readFileSync } from 'node:fs' import { setTimeout } from 'node:timers/promises' import { fileURLToPath } from 'node:url' import { resolve } from 'node:path' const root = fileURLToPath(new URL('../', import.meta.url)) export const liveUrl = 'https://kody-connect-talk--kentcdodds.kody.run/' function fetchText(url) { // curl uses the same public HTTP path as the audience, without CLI scopes. const result = spawnSync('curl', ['--fail', '--silent', '--show-error', '--location', '--max-time', '15', '--header', 'Cache-Control: no-cache', url], { encoding: 'utf8', maxBuffer: 10 * 1024 * 1024, }) return result.status === 0 ? result.stdout : null } export async function verifyLive({ timeoutMs = 180_000 } = {}) { const expectedPage = readFileSync(resolve(root, 'dist/index.html'), 'utf8') .replaceAll('/__KODY_BASE__/', '/_assets/') const expectedDeck = readFileSync(resolve(root, 'dist/deck/index.html'), 'utf8') const deadline = Date.now() + timeoutMs console.log(`Waiting for the published build at ${liveUrl}`) do { if (fetchText(liveUrl) === expectedPage && fetchText(new URL('_assets/deck/index.html', liveUrl).href) === expectedDeck) { console.log('Verified: the live page and deck match the local build.') return } if (Date.now() >= deadline) break await setTimeout(5000) } while (Date.now() < deadline) throw new Error('The live build did not match within 3 minutes. Run npm run verify:live to check again; do not republish while the workflow is active.') } if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { verifyLive().catch((error) => { console.error(error.message) process.exitCode = 1 }) }