#!/usr/bin/env node /** * prepare-src.mjs — Pre-build source transformation * * This script patches the source tree to make it compilable without Bun: * 1. Replace `import { feature } from 'bun:bundle'` with our stub * 2. Replace `MACRO.X` references with runtime values * 3. Create missing type declarations */ import fs from 'node:fs' import path from 'node:path' import { fileURLToPath } from 'node:url' const __dirname = path.dirname(fileURLToPath(import.meta.url)) const ROOT = path.resolve(__dirname, '..') const SRC = path.join(ROOT, 'src') const VERSION = '2.1.88' // ── Helpers ────────────────────────────────────────────────────────────────── function walk(dir, ext = '.ts') { const results = [] for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { const full = path.join(dir, entry.name) if (entry.isDirectory() && entry.name !== 'node_modules') { results.push(...walk(full, ext)) } else if (entry.name.endsWith(ext) || entry.name.endsWith('.tsx')) { results.push(full) } } return results } function patchFile(filePath) { let src = fs.readFileSync(filePath, 'utf8') let changed = false // 1. Replace `import { feature } from 'bun:bundle'` / `"bun:bundle"` if (src.includes("from 'bun:bundle'") || src.includes('from "bun:bundle"')) { src = src.replace(/import\s*\{\s*feature\s*\}\s*from\s*['"]bun:bundle['"]/g, "import { feature } from '../stubs/bun-bundle.js'") // Fix relative depth based on file location const rel = path.relative(SRC, path.dirname(filePath)) const depth = rel ? '../'.repeat(rel.split('/').length) : '' if (depth) { src = src.replace("from '../stubs/bun-bundle.js'", `from '${depth}stubs/bun-bundle.js'`) } changed = true } // 2. Replace MACRO.X with string literals const macroReplacements = { 'MACRO.VERSION': `'${VERSION}'`, 'MACRO.BUILD_TIME': `'${new Date().toISOString()}'`, 'MACRO.FEEDBACK_CHANNEL': `'https://github.com/anthropics/claude-code/issues'`, 'MACRO.ISSUES_EXPLAINER': `'https://github.com/anthropics/claude-code/issues/new/choose'`, 'MACRO.NATIVE_PACKAGE_URL': `'@anthropic-ai/claude-code'`, 'MACRO.PACKAGE_URL': `'@anthropic-ai/claude-code'`, 'MACRO.VERSION_CHANGELOG': `''`, } for (const [macro, replacement] of Object.entries(macroReplacements)) { if (src.includes(macro)) { // Don't replace inside strings src = src.replace(new RegExp(`(?