Skip to content

Commit c5dd269

Browse files
committed
create initializeApp function
1 parent 6fad8d4 commit c5dd269

File tree

3 files changed

+74
-67
lines changed

3 files changed

+74
-67
lines changed

cli/src/index.tsx

Lines changed: 4 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,10 @@ import { validateAgents } from '@codebuff/sdk'
99
import { render } from '@opentui/react'
1010
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
1111
import { Command } from 'commander'
12-
import { enableMapSet } from 'immer'
1312
import React from 'react'
1413

15-
// Enable Map and Set support in Immer globally (once at app initialization)
16-
enableMapSet()
17-
1814
import { App } from './app'
19-
import { initializeThemeStore } from './hooks/use-theme'
15+
import { initializeApp } from './init/init-app'
2016
import { getProjectRoot } from './project-files'
2117
import { getUserCredentials } from './utils/auth'
2218
import { loadAgentDefinitions } from './utils/load-agent-definitions'
@@ -28,58 +24,11 @@ import type { FileTreeNode } from '@codebuff/common/util/file'
2824
const require = createRequire(import.meta.url)
2925

3026
const INTERNAL_OSC_FLAG = '--internal-osc-detect'
31-
const OSC_DEBUG_ENABLED = process.env.CODEBUFF_OSC_DEBUG === '1'
32-
33-
function logOscDebug(message: string, data?: Record<string, unknown>) {
34-
if (!OSC_DEBUG_ENABLED) return
35-
const payload = data ? ` ${JSON.stringify(data)}` : ''
36-
console.error(`[osc:subprocess] ${message}${payload}`)
37-
}
3827

3928
function isOscDetectionRun(): boolean {
4029
return process.argv.includes(INTERNAL_OSC_FLAG)
4130
}
4231

43-
async function runOscDetectionSubprocess(): Promise<void> {
44-
// Set env vars to keep subprocess quiet
45-
process.env.__INTERNAL_OSC_DETECT = '1'
46-
process.env.CODEBUFF_GITHUB_ACTIONS = 'true'
47-
if (process.env.CODEBUFF_OSC_DEBUG === undefined) {
48-
process.env.CODEBUFF_OSC_DEBUG = '1'
49-
}
50-
logOscDebug('Starting OSC detection flag run')
51-
52-
// Avoid importing logger or other modules that produce output
53-
const { detectTerminalTheme, terminalSupportsOSC } = await import(
54-
'./utils/terminal-color-detection'
55-
)
56-
57-
const oscSupported = terminalSupportsOSC()
58-
logOscDebug('terminalSupportsOSC result', { oscSupported })
59-
60-
if (!oscSupported) {
61-
logOscDebug('Terminal does not support OSC queries, returning null theme')
62-
console.log(JSON.stringify({ theme: null }))
63-
await new Promise((resolve) => setImmediate(resolve))
64-
process.exit(0)
65-
}
66-
67-
try {
68-
const theme = await detectTerminalTheme()
69-
logOscDebug('detectTerminalTheme resolved', { theme })
70-
console.log(JSON.stringify({ theme }))
71-
await new Promise((resolve) => setImmediate(resolve))
72-
} catch (error) {
73-
logOscDebug('detectTerminalTheme threw', {
74-
error: error instanceof Error ? error.message : String(error),
75-
})
76-
console.log(JSON.stringify({ theme: null }))
77-
await new Promise((resolve) => setImmediate(resolve))
78-
}
79-
80-
process.exit(0)
81-
}
82-
8332
function loadPackageVersion(): string {
8433
if (process.env.CODEBUFF_CLI_VERSION) {
8534
return process.env.CODEBUFF_CLI_VERSION
@@ -97,8 +46,6 @@ function loadPackageVersion(): string {
9746
return 'dev'
9847
}
9948

100-
const VERSION = loadPackageVersion()
101-
10249
function createQueryClient(): QueryClient {
10350
return new QueryClient({
10451
defaultOptions: {
@@ -131,7 +78,7 @@ function parseArgs(): ParsedArgs {
13178
program
13279
.name('codebuff')
13380
.description('Codebuff CLI - AI-powered coding assistant')
134-
.version(VERSION, '-v, --version', 'Print the CLI version')
81+
.version(loadPackageVersion(), '-v, --version', 'Print the CLI version')
13582
.option(
13683
'--agent <agent-id>',
13784
'Specify which agent to use (e.g., "base", "ask", "file-picker")',
@@ -163,7 +110,7 @@ function parseArgs(): ParsedArgs {
163110
}
164111
}
165112

166-
async function bootstrapCli(): Promise<void> {
113+
async function main(): Promise<void> {
167114
const {
168115
initialPrompt,
169116
agent,
@@ -172,7 +119,7 @@ async function bootstrapCli(): Promise<void> {
172119
continueId,
173120
} = parseArgs()
174121

175-
initializeThemeStore()
122+
await initializeApp({ isOscDetectionRun: isOscDetectionRun() })
176123

177124
if (clearLogs) {
178125
clearLogFile()
@@ -236,7 +183,6 @@ async function bootstrapCli(): Promise<void> {
236183
}, [])
237184

238185
return (
239-
// Hi!
240186
<App
241187
initialPrompt={initialPrompt}
242188
agentId={agent}
@@ -262,13 +208,4 @@ async function bootstrapCli(): Promise<void> {
262208
)
263209
}
264210

265-
async function main(): Promise<void> {
266-
if (isOscDetectionRun()) {
267-
await runOscDetectionSubprocess()
268-
return
269-
}
270-
271-
await bootstrapCli()
272-
}
273-
274211
void main()

cli/src/init/init-app.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { enableMapSet } from 'immer'
2+
3+
import { initializeThemeStore } from '../hooks/use-theme'
4+
import { getProjectRoot } from '../project-files'
5+
import { runOscDetectionSubprocess } from './osc-subprocess'
6+
7+
export async function initializeApp(params: {
8+
isOscDetectionRun: boolean
9+
}): Promise<void> {
10+
const { isOscDetectionRun } = params
11+
12+
if (isOscDetectionRun) {
13+
await runOscDetectionSubprocess()
14+
return
15+
}
16+
17+
getProjectRoot()
18+
19+
// Enable Map and Set support in Immer globally (once at app initialization)
20+
enableMapSet()
21+
22+
initializeThemeStore()
23+
}

cli/src/init/osc-subprocess.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const OSC_DEBUG_ENABLED = process.env.CODEBUFF_OSC_DEBUG === '1'
2+
3+
function logOscDebug(message: string, data?: Record<string, unknown>) {
4+
if (!OSC_DEBUG_ENABLED) return
5+
const payload = data ? ` ${JSON.stringify(data)}` : ''
6+
console.error(`[osc:subprocess] ${message}${payload}`)
7+
}
8+
9+
export async function runOscDetectionSubprocess(): Promise<void> {
10+
// Set env vars to keep subprocess quiet
11+
process.env.__INTERNAL_OSC_DETECT = '1'
12+
process.env.CODEBUFF_GITHUB_ACTIONS = 'true'
13+
if (process.env.CODEBUFF_OSC_DEBUG === undefined) {
14+
process.env.CODEBUFF_OSC_DEBUG = '1'
15+
}
16+
logOscDebug('Starting OSC detection flag run')
17+
18+
// Avoid importing logger or other modules that produce output
19+
const { detectTerminalTheme, terminalSupportsOSC } = await import(
20+
'../utils/terminal-color-detection'
21+
)
22+
23+
const oscSupported = terminalSupportsOSC()
24+
logOscDebug('terminalSupportsOSC result', { oscSupported })
25+
26+
if (!oscSupported) {
27+
logOscDebug('Terminal does not support OSC queries, returning null theme')
28+
console.log(JSON.stringify({ theme: null }))
29+
await new Promise((resolve) => setImmediate(resolve))
30+
process.exit(0)
31+
}
32+
33+
try {
34+
const theme = await detectTerminalTheme()
35+
logOscDebug('detectTerminalTheme resolved', { theme })
36+
console.log(JSON.stringify({ theme }))
37+
await new Promise((resolve) => setImmediate(resolve))
38+
} catch (error) {
39+
logOscDebug('detectTerminalTheme threw', {
40+
error: error instanceof Error ? error.message : String(error),
41+
})
42+
console.log(JSON.stringify({ theme: null }))
43+
await new Promise((resolve) => setImmediate(resolve))
44+
}
45+
46+
process.exit(0)
47+
}

0 commit comments

Comments
 (0)