ci.ts

3.5 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 'fs';89const RESET  = '\x1b[0m';10const GREEN  = '\x1b[32m';11const RED    = '\x1b[31m';12const YELLOW = '\x1b[33m';13const BOLD   = '\x1b[1m';14const DIM    = '\x1b[2m';1516interface Step {17  name: string;18  cmd: string[];19  cwd?: string;20}2122const steps: Step[] = [23  {24    // LV-6: standardize on bun + a frozen lockfile. --frozen-lockfile exits25    // non-zero if bun.lock is stale relative to package.json, so a forgotten26    // lockfile update fails CI instead of drifting silently.27    name: 'Lockfile freshness',28    cmd: ['bun', 'install', '--frozen-lockfile'],29  },30  {31    name: 'TypeScript',32    cmd: ['npx', 'tsc', '--noEmit'],33  },34  {35    name: 'Lint',36    cmd: ['npm', 'run', 'lint'],37  },38  {39    name: 'Tests',40    cmd: ['npm', 'test', '--', '--reporter=verbose'],41  },42  {43    name: 'Chrome build',44    cmd: ['npm', 'run', 'build:chrome'],45  },46  {47    name: 'Firefox build',48    cmd: ['npm', 'run', 'build:firefox'],49  },50  {51    name: 'Mobile build',52    cmd: ['npm', 'run', 'build:mobile'],53  },54];5556function run(step: Step): { ok: boolean; output: string; duration: number } {57  const start = Date.now();58  const result = spawnSync(step.cmd, {59    cwd: step.cwd ?? process.cwd(),60    env: process.env,61    stdout: 'pipe',62    stderr: 'pipe',63  });64  const duration = Date.now() - start;65  const ok = result.exitCode === 0;66  const output = [67    result.stdout?.toString().trim(),68    result.stderr?.toString().trim(),69  ].filter(Boolean).join('\n');7071  return { ok, output, duration };72}7374function formatDuration(ms: number): string {75  if (ms < 1000) return `${ms}ms`;76  return `${(ms / 1000).toFixed(1)}s`;77}7879function printStep(name: string, ok: boolean, duration: number) {80  const icon = ok ? `${GREEN}✓${RESET}` : `${RED}✗${RESET}`;81  const time = `${DIM}${formatDuration(duration)}${RESET}`;82  console.log(`  ${icon}  ${name.padEnd(20)} ${time}`);83}8485async function main() {86  console.log();87  console.log(`${BOLD}peach-browser CI${RESET}`);88  console.log(`${DIM}${new Date().toLocaleTimeString()}${RESET}`);89  console.log();9091  let failed = false;92  const failures: { name: string; output: string }[] = [];9394  for (const step of steps) {95    process.stdout.write(`  ${DIM}…${RESET}  ${step.name.padEnd(20)}\r`);96    const result = run(step);97    printStep(step.name, result.ok, result.duration);9899    if (!result.ok) {100      failed = true;101      failures.push({ name: step.name, output: result.output });102      break; // fail fast103    }104  }105106  console.log();107108  if (!failed) {109    console.log(`${GREEN}${BOLD}All checks passed.${RESET}`);110    console.log();111112    // Show build artifact sizes113    const chromeManifest = 'dist-chrome/manifest.json';114    if (existsSync(chromeManifest)) {115      const manifest = JSON.parse(await Bun.file(chromeManifest).text());116      console.log(`${DIM}Version: ${manifest.version}${RESET}`);117    }118  } else {119    console.log(`${RED}${BOLD}Failed: ${failures[0].name}${RESET}`);120    console.log();121122    // Print the relevant failure output123    const output = failures[0].output;124    if (output) {125      // Show last 30 lines of output — most errors are at the end126      const lines = output.split('\n');127      const relevant = lines.slice(-30).join('\n');128      console.log(`${DIM}${relevant}${RESET}`);129    }130131    console.log();132    process.exit(1);133  }134}135136main();