File size: 4,797 Bytes
064bfd6 | 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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | import { chmodSync, existsSync, mkdirSync } from 'fs'
import { dirname } from 'path'
const pkg = await Bun.file(new URL('../package.json', import.meta.url)).json() as {
name: string
version: string
}
const args = process.argv.slice(2)
const compile = args.includes('--compile')
const dev = args.includes('--dev')
const fullExperimentalFeatures = [
'AGENT_MEMORY_SNAPSHOT',
'AGENT_TRIGGERS',
'AGENT_TRIGGERS_REMOTE',
'AWAY_SUMMARY',
'BASH_CLASSIFIER',
'BUDDY',
'BRIDGE_MODE',
'BUILTIN_EXPLORE_PLAN_AGENTS',
'CACHED_MICROCOMPACT',
'CCR_AUTO_CONNECT',
'CCR_MIRROR',
'CCR_REMOTE_SETUP',
'COMPACTION_REMINDERS',
'CONNECTOR_TEXT',
'EXTRACT_MEMORIES',
'HISTORY_PICKER',
'HOOK_PROMPTS',
'KAIROS_BRIEF',
'KAIROS_CHANNELS',
'LODESTONE',
'MCP_RICH_OUTPUT',
'MESSAGE_ACTIONS',
'NATIVE_CLIPBOARD_IMAGE',
'NEW_INIT',
'POWERSHELL_AUTO_MODE',
'PROMPT_CACHE_BREAK_DETECTION',
'QUICK_SEARCH',
'SHOT_STATS',
'TEAMMEM',
'TOKEN_BUDGET',
'TREE_SITTER_BASH',
'TREE_SITTER_BASH_SHADOW',
'ULTRAPLAN',
'ULTRATHINK',
'UNATTENDED_RETRY',
'VERIFICATION_AGENT',
'VOICE_MODE',
] as const
function runCommand(cmd: string[]): string | null {
const proc = Bun.spawnSync({
cmd,
cwd: process.cwd(),
stdout: 'pipe',
stderr: 'pipe',
})
if (proc.exitCode !== 0) {
return null
}
return new TextDecoder().decode(proc.stdout).trim() || null
}
function getDevVersion(baseVersion: string): string {
const timestamp = new Date().toISOString()
const date = timestamp.slice(0, 10).replaceAll('-', '')
const time = timestamp.slice(11, 19).replaceAll(':', '')
const sha = runCommand(['git', 'rev-parse', '--short=8', 'HEAD']) ?? 'unknown'
return `${baseVersion}-dev.${date}.t${time}.sha${sha}`
}
function getVersionChangelog(): string {
return (
runCommand(['git', 'log', '--format=%h %s', '-20']) ??
'Local development build'
)
}
const defaultFeatures = ['VOICE_MODE']
const featureSet = new Set(defaultFeatures)
for (let i = 0; i < args.length; i += 1) {
const arg = args[i]
if (arg === '--feature-set' && args[i + 1]) {
if (args[i + 1] === 'dev-full') {
for (const feature of fullExperimentalFeatures) {
featureSet.add(feature)
}
}
i += 1
continue
}
if (arg === '--feature-set=dev-full') {
for (const feature of fullExperimentalFeatures) {
featureSet.add(feature)
}
continue
}
if (arg === '--feature' && args[i + 1]) {
featureSet.add(args[i + 1]!)
i += 1
continue
}
if (arg.startsWith('--feature=')) {
featureSet.add(arg.slice('--feature='.length))
}
}
const features = [...featureSet]
const outfile = compile
? dev
? './dist/cli-dev'
: './dist/cli'
: dev
? './cli-dev'
: './cli'
const buildTime = new Date().toISOString()
const version = dev ? getDevVersion(pkg.version) : pkg.version
mkdirSync(dirname(outfile), { recursive: true })
const externals = [
'@ant/*',
'audio-capture-napi',
'image-processor-napi',
'modifiers-napi',
'url-handler-napi',
]
const defines = {
'process.env.USER_TYPE': JSON.stringify('external'),
'process.env.CLAUDE_CODE_FORCE_FULL_LOGO': JSON.stringify('true'),
...(dev
? { 'process.env.NODE_ENV': JSON.stringify('development') }
: {}),
...(dev
? {
'process.env.CLAUDE_CODE_EXPERIMENTAL_BUILD': JSON.stringify('true'),
}
: {}),
'process.env.CLAUDE_CODE_VERIFY_PLAN': JSON.stringify('false'),
'process.env.CCR_FORCE_BUNDLE': JSON.stringify('true'),
'MACRO.VERSION': JSON.stringify(version),
'MACRO.BUILD_TIME': JSON.stringify(buildTime),
'MACRO.PACKAGE_URL': JSON.stringify(pkg.name),
'MACRO.NATIVE_PACKAGE_URL': 'undefined',
'MACRO.FEEDBACK_CHANNEL': JSON.stringify('github'),
'MACRO.ISSUES_EXPLAINER': JSON.stringify(
'This reconstructed source snapshot does not include Anthropic internal issue routing.',
),
'MACRO.VERSION_CHANGELOG': JSON.stringify(
dev ? getVersionChangelog() : 'https://github.com/paoloanzn/claude-code',
),
} as const
const cmd = [
'bun',
'build',
'./src/entrypoints/cli.tsx',
'--compile',
'--target',
'bun',
'--format',
'esm',
'--outfile',
outfile,
'--minify',
'--bytecode',
'--packages',
'bundle',
'--conditions',
'bun',
]
for (const external of externals) {
cmd.push('--external', external)
}
for (const feature of features) {
cmd.push(`--feature=${feature}`)
}
for (const [key, value] of Object.entries(defines)) {
cmd.push('--define', `${key}=${value}`)
}
const proc = Bun.spawnSync({
cmd,
cwd: process.cwd(),
stdout: 'inherit',
stderr: 'inherit',
})
if (proc.exitCode !== 0) {
process.exit(proc.exitCode ?? 1)
}
if (existsSync(outfile)) {
chmodSync(outfile, 0o755)
}
console.log(`Built ${outfile}`)
|