forked from sanbuphy/learn-coding-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare-src.mjs
More file actions
115 lines (98 loc) · 4.02 KB
/
prepare-src.mjs
File metadata and controls
115 lines (98 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/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(`(?<![\\w'"])${macro.replace('.', '\\.')}(?![\\w'" ])`, 'g'), replacement)
changed = true
}
}
if (changed) {
fs.writeFileSync(filePath, src, 'utf8')
return true
}
return false
}
// ── Main ─────────────────────────────────────────────────────────────────────
console.log('🔧 Preparing source files...\n')
const files = walk(SRC)
let patched = 0
for (const file of files) {
if (patchFile(file)) {
patched++
console.log(` patched: ${path.relative(ROOT, file)}`)
}
}
// Create stub for bun:ffi (only used in upstreamproxy)
const ffiStub = path.join(ROOT, 'stubs', 'bun-ffi.ts')
if (!fs.existsSync(ffiStub)) {
fs.writeFileSync(ffiStub, `// Stub for bun:ffi — not available outside Bun runtime\nexport const ffi = {} as any\nexport function dlopen() { return {} }\n`)
console.log(' created: stubs/bun-ffi.ts')
}
// Create global MACRO type declaration
const macroDecl = path.join(ROOT, 'stubs', 'global.d.ts')
fs.writeFileSync(macroDecl, `// Global compile-time macros (normally injected by Bun bundler)
declare const MACRO: {
VERSION: string
BUILD_TIME: string
FEEDBACK_CHANNEL: string
ISSUES_EXPLAINER: string
NATIVE_PACKAGE_URL: string
PACKAGE_URL: string
VERSION_CHANGELOG: string
}
`)
console.log(' created: stubs/global.d.ts')
console.log(`\n✅ Patched ${patched} / ${files.length} source files`)