|
| 1 | +#!/usr/bin/env bun |
| 2 | +/** |
| 3 | + * Comprehensive SDK verification script. |
| 4 | + * Runs all build, typecheck, and compatibility tests in one command. |
| 5 | + * |
| 6 | + * Usage: |
| 7 | + * bun run verify # Run full verification |
| 8 | + * bun run verify --skip-build # Skip build step (use existing dist) |
| 9 | + */ |
| 10 | + |
| 11 | +import { execSync, type ExecSyncOptions } from 'child_process' |
| 12 | +import { existsSync, rmSync } from 'fs' |
| 13 | +import { join } from 'path' |
| 14 | + |
| 15 | +const sdkRoot = join(import.meta.dirname, '..') |
| 16 | +const testDir = join(sdkRoot, 'test') |
| 17 | +const distPath = join(sdkRoot, 'dist') |
| 18 | + |
| 19 | +// Test subprojects that use dist copy setup |
| 20 | +const DIST_COPY_SUBPROJECTS = [ |
| 21 | + 'cjs-compatibility', |
| 22 | + 'esm-compatibility', |
| 23 | + 'ripgrep-bundling', |
| 24 | +] |
| 25 | + |
| 26 | +// Test subprojects that use file reference (different setup) |
| 27 | +const FILE_REF_SUBPROJECTS = ['tree-sitter-queries'] |
| 28 | + |
| 29 | +interface VerifyOptions { |
| 30 | + skipBuild?: boolean |
| 31 | +} |
| 32 | + |
| 33 | +function log(message: string) { |
| 34 | + console.log(`\n${'='.repeat(60)}`) |
| 35 | + console.log(`📦 ${message}`) |
| 36 | + console.log('='.repeat(60)) |
| 37 | +} |
| 38 | + |
| 39 | +function step(message: string) { |
| 40 | + console.log(`\n → ${message}`) |
| 41 | +} |
| 42 | + |
| 43 | +function success(message: string) { |
| 44 | + console.log(` ✅ ${message}`) |
| 45 | +} |
| 46 | + |
| 47 | +function run(command: string, options: ExecSyncOptions = {}) { |
| 48 | + execSync(command, { cwd: sdkRoot, stdio: 'inherit', ...options }) |
| 49 | +} |
| 50 | + |
| 51 | +function removeOldSdk(projectPath: string) { |
| 52 | + const sdkPath = join(projectPath, 'node_modules', '@codebuff', 'sdk') |
| 53 | + if (existsSync(sdkPath)) { |
| 54 | + step(`Removing old SDK at ${sdkPath}`) |
| 55 | + rmSync(sdkPath, { recursive: true, force: true }) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +function cleanAndBuild() { |
| 60 | + log('Step 1: Clean and Build') |
| 61 | + |
| 62 | + step('Cleaning dist directory...') |
| 63 | + rmSync(distPath, { recursive: true, force: true }) |
| 64 | + |
| 65 | + step('Building SDK...') |
| 66 | + run('bun run build') |
| 67 | + |
| 68 | + success('Build complete') |
| 69 | +} |
| 70 | + |
| 71 | +function typecheck() { |
| 72 | + log('Step 2: TypeScript Check') |
| 73 | + |
| 74 | + step('Running typecheck on source...') |
| 75 | + run('tsc --noEmit -p .') |
| 76 | + |
| 77 | + success('TypeScript check passed') |
| 78 | +} |
| 79 | + |
| 80 | +function smokeTest() { |
| 81 | + log('Step 3: Smoke Test') |
| 82 | + |
| 83 | + step('Running dist smoke tests...') |
| 84 | + run('bun run smoke-test-dist.ts') |
| 85 | + |
| 86 | + success('Smoke test passed') |
| 87 | +} |
| 88 | + |
| 89 | +function runDistCopySubproject(projectName: string) { |
| 90 | + const projectPath = join(testDir, projectName) |
| 91 | + |
| 92 | + if (!existsSync(projectPath)) { |
| 93 | + throw new Error(`Project not found: ${projectPath}`) |
| 94 | + } |
| 95 | + |
| 96 | + step(`Testing ${projectName}...`) |
| 97 | + |
| 98 | + // Remove old SDK installation |
| 99 | + removeOldSdk(projectPath) |
| 100 | + |
| 101 | + // Install dependencies |
| 102 | + execSync('npm install --silent', { cwd: projectPath, stdio: 'inherit' }) |
| 103 | + |
| 104 | + // Run setup (copies dist to node_modules) |
| 105 | + execSync('npm run setup', { cwd: projectPath, stdio: 'inherit' }) |
| 106 | + |
| 107 | + // Run all tests (imports + types) |
| 108 | + execSync('npm run test:all', { cwd: projectPath, stdio: 'inherit' }) |
| 109 | + |
| 110 | + success(`${projectName} passed`) |
| 111 | +} |
| 112 | + |
| 113 | +function runFileRefSubproject(projectName: string) { |
| 114 | + const projectPath = join(testDir, projectName) |
| 115 | + |
| 116 | + if (!existsSync(projectPath)) { |
| 117 | + throw new Error(`Project not found: ${projectPath}`) |
| 118 | + } |
| 119 | + |
| 120 | + step(`Testing ${projectName}...`) |
| 121 | + |
| 122 | + // Install dependencies (uses file: reference) |
| 123 | + execSync('npm install --silent', { cwd: projectPath, stdio: 'inherit' }) |
| 124 | + |
| 125 | + // Run all tests |
| 126 | + execSync('npm run test:all', { cwd: projectPath, stdio: 'inherit' }) |
| 127 | + |
| 128 | + success(`${projectName} passed`) |
| 129 | +} |
| 130 | + |
| 131 | +function compatibilityTests() { |
| 132 | + log('Step 4: Compatibility Tests') |
| 133 | + |
| 134 | + // Run dist-copy subprojects (CJS, ESM, ripgrep) |
| 135 | + for (const project of DIST_COPY_SUBPROJECTS) { |
| 136 | + runDistCopySubproject(project) |
| 137 | + } |
| 138 | + |
| 139 | + // Run file-reference subprojects (tree-sitter-queries) |
| 140 | + for (const project of FILE_REF_SUBPROJECTS) { |
| 141 | + runFileRefSubproject(project) |
| 142 | + } |
| 143 | + |
| 144 | + success('All compatibility tests passed') |
| 145 | +} |
| 146 | + |
| 147 | +function parseArgs(): VerifyOptions { |
| 148 | + const args = process.argv.slice(2) |
| 149 | + return { |
| 150 | + skipBuild: args.includes('--skip-build'), |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +async function main() { |
| 155 | + const options = parseArgs() |
| 156 | + |
| 157 | + console.log('🔍 SDK Verification Script') |
| 158 | + console.log('==========================') |
| 159 | + console.log('') |
| 160 | + console.log('This script will:') |
| 161 | + console.log(' 1. Clean and build the SDK') |
| 162 | + console.log(' 2. Run TypeScript checks') |
| 163 | + console.log(' 3. Run smoke tests on dist') |
| 164 | + console.log(' 4. Run all compatibility tests (CJS, ESM, ripgrep, tree-sitter)') |
| 165 | + |
| 166 | + if (options.skipBuild) { |
| 167 | + console.log('\n⚠️ Skipping build step (--skip-build flag)') |
| 168 | + if (!existsSync(distPath)) { |
| 169 | + throw new Error('dist directory not found. Run without --skip-build first.') |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + const startTime = Date.now() |
| 174 | + |
| 175 | + // Step 1: Clean and Build |
| 176 | + if (!options.skipBuild) { |
| 177 | + cleanAndBuild() |
| 178 | + } |
| 179 | + |
| 180 | + // Step 2: TypeScript Check |
| 181 | + typecheck() |
| 182 | + |
| 183 | + // Step 3: Smoke Test |
| 184 | + smokeTest() |
| 185 | + |
| 186 | + // Step 4: Compatibility Tests |
| 187 | + compatibilityTests() |
| 188 | + |
| 189 | + // Summary |
| 190 | + const duration = ((Date.now() - startTime) / 1000).toFixed(1) |
| 191 | + console.log('') |
| 192 | + console.log('='.repeat(60)) |
| 193 | + console.log(`🎉 All verification steps passed! (${duration}s)`) |
| 194 | + console.log('='.repeat(60)) |
| 195 | +} |
| 196 | + |
| 197 | +main().catch((error) => { |
| 198 | + console.error('\n❌ Verification failed:', error.message) |
| 199 | + process.exit(1) |
| 200 | +}) |
0 commit comments