ci.ts

3.4 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];5152function run(step: Step): { ok: boolean; output: string; duration: number } {53  const start = Date.now();54  const result = spawnSync(step.cmd, {55    cwd: step.cwd ?? process.cwd(),56    env: process.env,57    stdout: 'pipe',58    stderr: 'pipe',59  });60  const duration = Date.now() - start;61  const ok = result.exitCode === 0;62  const output = [63    result.stdout?.toString().trim(),64    result.stderr?.toString().trim(),65  ].filter(Boolean).join('\n');6667  return { ok, output, duration };68}6970function formatDuration(ms: number): string {71  if (ms < 1000) return `${ms}ms`;72  return `${(ms / 1000).toFixed(1)}s`;73}7475function printStep(name: string, ok: boolean, duration: number) {76  const icon = ok ? `${GREEN}✓${RESET}` : `${RED}✗${RESET}`;77  const time = `${DIM}${formatDuration(duration)}${RESET}`;78  console.log(`  ${icon}  ${name.padEnd(20)} ${time}`);79}8081async function main() {82  console.log();83  console.log(`${BOLD}peach-browser CI${RESET}`);84  console.log(`${DIM}${new Date().toLocaleTimeString()}${RESET}`);85  console.log();8687  let failed = false;88  const failures: { name: string; output: string }[] = [];8990  for (const step of steps) {91    process.stdout.write(`  ${DIM}…${RESET}  ${step.name.padEnd(20)}\r`);92    const result = run(step);93    printStep(step.name, result.ok, result.duration);9495    if (!result.ok) {96      failed = true;97      failures.push({ name: step.name, output: result.output });98      break; // fail fast99    }100  }101102  console.log();103104  if (!failed) {105    console.log(`${GREEN}${BOLD}All checks passed.${RESET}`);106    console.log();107108    // Show build artifact sizes109    const chromeManifest = 'dist-chrome/manifest.json';110    if (existsSync(chromeManifest)) {111      const manifest = JSON.parse(await Bun.file(chromeManifest).text());112      console.log(`${DIM}Version: ${manifest.version}${RESET}`);113    }114  } else {115    console.log(`${RED}${BOLD}Failed: ${failures[0].name}${RESET}`);116    console.log();117118    // Print the relevant failure output119    const output = failures[0].output;120    if (output) {121      // Show last 30 lines of output — most errors are at the end122      const lines = output.split('\n');123      const relevant = lines.slice(-30).join('\n');124      console.log(`${DIM}${relevant}${RESET}`);125    }126127    console.log();128    process.exit(1);129  }130}131132main();