ci.ts

4.2 KB · raw

1#!/usr/bin/env bun2// peach-browser/scripts/ci.ts3// Run at the end of a work session to verify everything is solid.4// Usage: bun run scripts/ci.ts56import { spawnSync } from 'bun';7import { existsSync } from 'node:fs';8import { resolveBin } from './release-lib';910const RESET  = '\x1b[0m';11const GREEN  = '\x1b[32m';12const RED    = '\x1b[31m';13const BOLD   = '\x1b[1m';14const DIM    = '\x1b[2m';15const RUN_FIREFOX_SENDER_ONLY = process.env.PEACH_E2E_FIREFOX === '1';1617interface Step {18  name: string;19  cmd: string[] | (() => string[]);20  cwd?: string;21}2223const steps: Step[] = [24  {25    // LV-6: standardize on bun + a frozen lockfile. --frozen-lockfile exits26    // non-zero if bun.lock is stale relative to package.json, so a forgotten27    // lockfile update fails CI instead of drifting silently.28    name: 'Lockfile freshness',29    cmd: ['bun', 'install', '--frozen-lockfile'],30  },31  {32    name: 'TypeScript',33    cmd: () => [resolveBin('tsc'), '--noEmit'],34  },35  {36    name: 'Lint',37    cmd: ['npm', 'run', 'lint'],38  },39  {40    // PB-26-003: rebuild the Noise WASM from crypto-core and fail if the41    // committed blob drifts — "reviewed source → shipped binary" enforced.42    name: 'Noise WASM provenance',43    cmd: ['node', 'scripts/check-noise-core.mjs'],44  },45  {46    name: 'Chrome build',47    cmd: ['npm', 'run', 'build:chrome'],48  },49  {50    name: 'Firefox build',51    cmd: ['npm', 'run', 'build:firefox'],52  },53  {54    // Runs after both builds: the web-accessible-resource assertions read the55    // shipped dist-chrome/ and dist-firefox/ manifests, so a Tests step ahead56    // of the builds would skip them on a clean checkout.57    name: 'Tests',58    cmd: ['npm', 'test', '--', '--reporter=verbose'],59  },60  ...(RUN_FIREFOX_SENDER_ONLY61    ? [{62        name: 'Firefox sender context',63        cmd: ['npm', 'run', 'test:firefox:sender'],64      }]65    : []),66];6768function run(step: Step): { ok: boolean; output: string; duration: number } {69  const start = Date.now();70  const cmd = typeof step.cmd === 'function' ? step.cmd() : step.cmd;71  const result = spawnSync(cmd, {72    cwd: step.cwd ?? process.cwd(),73    env: process.env,74    stdout: 'pipe',75    stderr: 'pipe',76  });77  const duration = Date.now() - start;78  const ok = result.exitCode === 0;79  const output = [80    result.stdout?.toString().trim(),81    result.stderr?.toString().trim(),82  ].filter(Boolean).join('\n');8384  return { ok, output, duration };85}8687function formatDuration(ms: number): string {88  if (ms < 1000) return `${ms}ms`;89  return `${(ms / 1000).toFixed(1)}s`;90}9192function printStep(name: string, ok: boolean, duration: number) {93  const icon = ok ? `${GREEN}✓${RESET}` : `${RED}✗${RESET}`;94  const time = `${DIM}${formatDuration(duration)}${RESET}`;95  console.log(`  ${icon}  ${name.padEnd(20)} ${time}`);96}9798async function main() {99  console.log();100  console.log(`${BOLD}peach-browser CI${RESET}`);101  console.log(`${DIM}${new Date().toLocaleTimeString()}${RESET}`);102  console.log();103104  let failed = false;105  const failures: { name: string; output: string }[] = [];106107  for (const step of steps) {108    process.stdout.write(`  ${DIM}…${RESET}  ${step.name.padEnd(20)}\r`);109    const result = run(step);110    printStep(step.name, result.ok, result.duration);111112    if (!result.ok) {113      failed = true;114      failures.push({ name: step.name, output: result.output });115      break; // fail fast116    }117  }118119  console.log();120121  if (!failed) {122    console.log(`${GREEN}${BOLD}All checks passed.${RESET}`);123    console.log();124125    // Show build artifact sizes126    const chromeManifest = 'dist-chrome/manifest.json';127    if (existsSync(chromeManifest)) {128      const manifest = JSON.parse(await Bun.file(chromeManifest).text());129      console.log(`${DIM}Version: ${manifest.version}${RESET}`);130    }131  } else {132    console.log(`${RED}${BOLD}Failed: ${failures[0].name}${RESET}`);133    console.log();134135    // Print the relevant failure output136    const output = failures[0].output;137    if (output) {138      // Show last 30 lines of output — most errors are at the end139      const lines = output.split('\n');140      const relevant = lines.slice(-30).join('\n');141      console.log(`${DIM}${relevant}${RESET}`);142    }143144    console.log();145    process.exit(1);146  }147}148149main();