跳到主要内容

应用入口

源码: main.tsx (804KB) | setup.ts | bootstrap/state.ts

1. 主入口 main.tsx

Claude Code 的 CLI 入口是 main.tsx,使用 Bun 作为运行时。

// source/src/main.tsx
import { render } from 'ink'
import { App } from './components/App'

// 解析命令行参数
const args = parseCommandLineArgs()

// 初始化全局状态
initializeGlobalState(args)

// 渲染终端 UI
render(<App {...args} />, { exitOnCtrlC: false })

main.tsx 是整个应用的起点,负责:

  • 解析命令行参数和选项
  • 初始化全局状态(通过 bootstrap/state.ts
  • 创建 Ink 渲染树
  • 设置信号处理(SIGINT, SIGTERM)

2. 启动流程

用户运行 claude


┌─────────────────────────┐
│ 1. main.tsx 入口 │
│ - 解析 CLI 参数 │
│ - 检查 Node/Bun 版本 │
└────────────┬────────────┘


┌─────────────────────────┐
│ 2. setup.ts 初始化 │
│ - 加载环境变量 │
│ - 初始化配置目录 │
│ - 检查认证状态 │
└────────────┬────────────┘


┌─────────────────────────┐
│ 3. bootstrap/state.ts │
│ - 创建全局状态单例 │
│ - 初始化 beta flags │
│ - 加载 settings │
└────────────┬────────────┘


┌─────────────────────────┐
│ 4. render(<App />) │
│ - Ink 终端 UI 渲染 │
│ - REPL 交互循环 │
└─────────────────────────┘

3. 初始化状态 bootstrap/state.ts

// source/src/bootstrap/state.ts
export interface BootstrapState {
systemPromptSectionsCleared: boolean
betaHeaderLatchesCleared: boolean
// 全局单例状态
}

let globalState: BootstrapState | null = null

export function getBootstrapState(): BootstrapState {
if (!globalState) {
globalState = createInitialState()
}
return globalState
}

4. 关键设计

特性实现
运行时Bun(非 Node.js)
入口文件main.tsx (804KB, 包含 bundle)
UI 框架Ink (React for Terminal)
状态初始化延迟单例模式
信号处理SIGINT/SIGTERM 优雅退出

上一节:文档导航 | 下一节:初始化状态