|
| 1 | +#!/usr/bin/env bun |
| 2 | +/** |
| 3 | + * Doctor Script - Diagnose common setup issues |
| 4 | + * |
| 5 | + * Run with: bun run doctor |
| 6 | + */ |
| 7 | + |
| 8 | +import { existsSync, readFileSync } from 'node:fs' |
| 9 | +import { join } from 'node:path' |
| 10 | + |
| 11 | +const ROOT = process.cwd() |
| 12 | + |
| 13 | +interface Check { |
| 14 | + name: string |
| 15 | + check: () => boolean | Promise<boolean> |
| 16 | + fix?: string |
| 17 | +} |
| 18 | + |
| 19 | +const colors = { |
| 20 | + green: (s: string) => `\x1b[32m${s}\x1b[0m`, |
| 21 | + red: (s: string) => `\x1b[31m${s}\x1b[0m`, |
| 22 | + yellow: (s: string) => `\x1b[33m${s}\x1b[0m`, |
| 23 | + dim: (s: string) => `\x1b[2m${s}\x1b[0m`, |
| 24 | +} |
| 25 | + |
| 26 | +const checks: Check[] = [ |
| 27 | + { |
| 28 | + name: 'Node.js version >= 22', |
| 29 | + check: () => { |
| 30 | + const version = process.versions.node |
| 31 | + const major = Number.parseInt(version.split('.')[0] ?? '0', 10) |
| 32 | + return major >= 22 |
| 33 | + }, |
| 34 | + fix: 'Install Node.js 22+ from https://nodejs.org or use nvm/fnm', |
| 35 | + }, |
| 36 | + { |
| 37 | + name: 'Bun installed', |
| 38 | + check: () => { |
| 39 | + try { |
| 40 | + Bun.version |
| 41 | + return true |
| 42 | + } catch { |
| 43 | + return false |
| 44 | + } |
| 45 | + }, |
| 46 | + fix: 'Install Bun: curl -fsSL https://bun.sh/install | bash', |
| 47 | + }, |
| 48 | + { |
| 49 | + name: 'Dependencies installed', |
| 50 | + check: () => existsSync(join(ROOT, 'node_modules')), |
| 51 | + fix: 'Run: bun install', |
| 52 | + }, |
| 53 | + { |
| 54 | + name: 'Environment file exists', |
| 55 | + check: () => |
| 56 | + existsSync(join(ROOT, '.env.local')) || existsSync(join(ROOT, '.env')), |
| 57 | + fix: 'Copy .env.example to .env.local and fill in values', |
| 58 | + }, |
| 59 | + { |
| 60 | + name: 'TypeScript config exists', |
| 61 | + check: () => existsSync(join(ROOT, 'tsconfig.json')), |
| 62 | + fix: 'Ensure tsconfig.json exists in project root', |
| 63 | + }, |
| 64 | + { |
| 65 | + name: 'Next.js config valid', |
| 66 | + check: () => |
| 67 | + existsSync(join(ROOT, 'next.config.ts')) || |
| 68 | + existsSync(join(ROOT, 'next.config.js')), |
| 69 | + fix: 'Ensure next.config.ts exists', |
| 70 | + }, |
| 71 | + { |
| 72 | + name: 'Biome config valid', |
| 73 | + check: () => { |
| 74 | + try { |
| 75 | + const biome = readFileSync(join(ROOT, 'biome.json'), 'utf-8') |
| 76 | + JSON.parse(biome) |
| 77 | + return true |
| 78 | + } catch { |
| 79 | + return false |
| 80 | + } |
| 81 | + }, |
| 82 | + fix: 'Check biome.json for syntax errors', |
| 83 | + }, |
| 84 | + { |
| 85 | + name: 'Generated styles exist', |
| 86 | + check: () => existsSync(join(ROOT, 'lib/styles/css/tailwind.css')), |
| 87 | + fix: 'Run: bun run setup:styles', |
| 88 | + }, |
| 89 | + { |
| 90 | + name: 'Public fonts directory exists', |
| 91 | + check: () => existsSync(join(ROOT, 'public/fonts')), |
| 92 | + fix: 'Add fonts to public/fonts/ directory', |
| 93 | + }, |
| 94 | + { |
| 95 | + name: 'Git hooks installed (lefthook)', |
| 96 | + check: () => existsSync(join(ROOT, '.git/hooks/pre-commit')), |
| 97 | + fix: 'Run: bunx lefthook install', |
| 98 | + }, |
| 99 | +] |
| 100 | + |
| 101 | +async function runDoctor() { |
| 102 | + console.log('\n🩺 Satus Doctor\n') |
| 103 | + console.log(colors.dim('Checking your development environment...\n')) |
| 104 | + |
| 105 | + let passed = 0 |
| 106 | + let failed = 0 |
| 107 | + |
| 108 | + for (const { name, check, fix } of checks) { |
| 109 | + try { |
| 110 | + const result = await check() |
| 111 | + if (result) { |
| 112 | + console.log(`${colors.green('✓')} ${name}`) |
| 113 | + passed++ |
| 114 | + } else { |
| 115 | + console.log(`${colors.red('✗')} ${name}`) |
| 116 | + if (fix) { |
| 117 | + console.log(` ${colors.dim(`Fix: ${fix}`)}`) |
| 118 | + } |
| 119 | + failed++ |
| 120 | + } |
| 121 | + } catch (_error) { |
| 122 | + console.log( |
| 123 | + `${colors.yellow('?')} ${name} ${colors.dim('(check failed)')}` |
| 124 | + ) |
| 125 | + failed++ |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + console.log('') |
| 130 | + if (failed === 0) { |
| 131 | + console.log( |
| 132 | + colors.green(`All ${passed} checks passed! Your environment is ready.`) |
| 133 | + ) |
| 134 | + } else { |
| 135 | + console.log( |
| 136 | + `${colors.green(`${passed} passed`)}, ${colors.red(`${failed} failed`)}` |
| 137 | + ) |
| 138 | + console.log( |
| 139 | + colors.dim('\nFix the issues above and run again: bun run doctor') |
| 140 | + ) |
| 141 | + } |
| 142 | + console.log('') |
| 143 | + |
| 144 | + process.exit(failed > 0 ? 1 : 0) |
| 145 | +} |
| 146 | + |
| 147 | +runDoctor() |
0 commit comments