|
| 1 | +import { render } from "ink"; |
| 2 | +import { readFileSync } from "node:fs"; |
| 3 | +import { join, resolve } from "node:path"; |
| 4 | +import { homedir } from "node:os"; |
| 5 | +import { setShellIfWindows, getProjectCode, resolveCurrentSettings } from "@vegamo/deepcode-core"; |
| 6 | +import { checkForNpmUpdate, promptForPendingUpdate } from "./common/update-check"; |
| 7 | +import { AppContainer } from "./ui"; |
| 8 | +import type { ParsedCliArgs } from "./cli-args"; |
| 9 | +import { writeStderrLine, writeStdoutLine } from "./utils/stdio-helpers"; |
| 10 | +import type { PackageJson } from "./utils/package"; |
| 11 | +import { CLI_VERSION } from "./generated/git-commit"; |
| 12 | +import { runExecMode } from "./exec-runner"; |
| 13 | + |
| 14 | +export async function runCliApp(parsed: ParsedCliArgs, packageInfo: PackageJson): Promise<void> { |
| 15 | + const projectRoot = process.cwd(); |
| 16 | + const settingsFile = parsed.settingsFile === undefined ? undefined : resolve(projectRoot, parsed.settingsFile); |
| 17 | + if (settingsFile !== undefined) { |
| 18 | + try { |
| 19 | + resolveCurrentSettings(projectRoot, settingsFile); |
| 20 | + } catch (error) { |
| 21 | + writeStderrLine(`deepcode: ${error instanceof Error ? error.message : String(error)}`); |
| 22 | + process.exitCode = 1; |
| 23 | + return; |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + // Configure Windows shell AFTER --version/--help handling. |
| 28 | + // On Windows without Git Bash, setShellIfWindows() throws and calls process.exit(1). |
| 29 | + // If called before argument parsing, --help and --version would fail on those machines. |
| 30 | + configureWindowsShell(); |
| 31 | + |
| 32 | + let initialPrompt = parsed.prompt; |
| 33 | + let resumeSessionId = parsed.resume; |
| 34 | + let forkSessionId = parsed.fork; |
| 35 | + // Resolve --last to the most recent session ID for the current project |
| 36 | + if (parsed.last) { |
| 37 | + const projectCode = getProjectCode(projectRoot); |
| 38 | + const indexPath = join(homedir(), ".deepcode", "projects", projectCode, "sessions-index.json"); |
| 39 | + try { |
| 40 | + const index = JSON.parse(readFileSync(indexPath, "utf-8")); |
| 41 | + const entries: { id: string; updateTime: string }[] = Array.isArray(index?.entries) ? index.entries : []; |
| 42 | + if (entries.length === 0) { |
| 43 | + writeStderrLine("No previous sessions found for the current project.\n"); |
| 44 | + process.exit(1); |
| 45 | + } |
| 46 | + const mostRecent = entries.reduce((a, b) => (a.updateTime > b.updateTime ? a : b)); |
| 47 | + resumeSessionId = mostRecent.id; |
| 48 | + } catch { |
| 49 | + writeStderrLine("No previous sessions found for the current project.\n"); |
| 50 | + process.exit(1); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + if (forkSessionId === true) { |
| 55 | + const projectCode = getProjectCode(projectRoot); |
| 56 | + const indexPath = join(homedir(), ".deepcode", "projects", projectCode, "sessions-index.json"); |
| 57 | + try { |
| 58 | + const index = JSON.parse(readFileSync(indexPath, "utf-8")); |
| 59 | + const entries: { id: string; updateTime: string }[] = Array.isArray(index?.entries) ? index.entries : []; |
| 60 | + if (entries.length === 0) { |
| 61 | + writeStderrLine("No previous sessions found for the current project.\n"); |
| 62 | + process.exit(1); |
| 63 | + } |
| 64 | + const mostRecent = entries.reduce((a, b) => (a.updateTime > b.updateTime ? a : b)); |
| 65 | + forkSessionId = mostRecent.id; |
| 66 | + } catch { |
| 67 | + writeStderrLine("No previous sessions found for the current project.\n"); |
| 68 | + process.exit(1); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if (parsed.exec) { |
| 73 | + process.exitCode = await runExecMode({ |
| 74 | + prompt: parsed.prompt!, |
| 75 | + projectRoot, |
| 76 | + settingsFile, |
| 77 | + sessionId: parsed.sessionId, |
| 78 | + json: parsed.json, |
| 79 | + resumeSessionId: typeof resumeSessionId === "string" ? resumeSessionId : undefined, |
| 80 | + forkSessionId: typeof forkSessionId === "string" ? forkSessionId : undefined, |
| 81 | + }); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + if (!process.stdin.isTTY) { |
| 86 | + writeStderrLine("deepcode requires an interactive terminal (TTY). Re-run from a real terminal session.\n"); |
| 87 | + process.exit(1); |
| 88 | + } |
| 89 | + |
| 90 | + // Validate --resume <sessionId> before entering TUI |
| 91 | + if (typeof resumeSessionId === "string") { |
| 92 | + const projectCode = getProjectCode(projectRoot); |
| 93 | + const indexPath = join(homedir(), ".deepcode", "projects", projectCode, "sessions-index.json"); |
| 94 | + try { |
| 95 | + const index = JSON.parse(readFileSync(indexPath, "utf-8")); |
| 96 | + const found = |
| 97 | + Array.isArray(index?.entries) && index.entries.some((e: { id: string }) => e.id === resumeSessionId); |
| 98 | + if (!found) { |
| 99 | + writeStderrLine(`No saved session found with ID "${resumeSessionId}".\n`); |
| 100 | + process.exit(1); |
| 101 | + } |
| 102 | + } catch { |
| 103 | + writeStderrLine(`No saved session found with ID "${resumeSessionId}".\n`); |
| 104 | + process.exit(1); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + if (typeof forkSessionId === "string") { |
| 109 | + const projectCode = getProjectCode(projectRoot); |
| 110 | + const indexPath = join(homedir(), ".deepcode", "projects", projectCode, "sessions-index.json"); |
| 111 | + try { |
| 112 | + const index = JSON.parse(readFileSync(indexPath, "utf-8")); |
| 113 | + const found = Array.isArray(index?.entries) && index.entries.some((e: { id: string }) => e.id === forkSessionId); |
| 114 | + if (!found) { |
| 115 | + writeStderrLine(`No saved session found with ID "${forkSessionId}".\n`); |
| 116 | + process.exit(1); |
| 117 | + } |
| 118 | + } catch { |
| 119 | + writeStderrLine(`No saved session found with ID "${forkSessionId}".\n`); |
| 120 | + process.exit(1); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + const updatePromptResult = await promptForPendingUpdate(packageInfo); |
| 125 | + if (updatePromptResult.installed) { |
| 126 | + process.exit(0); |
| 127 | + } |
| 128 | + |
| 129 | + const restartRef: { current: (() => void) | null } = { current: null }; |
| 130 | + |
| 131 | + function startApp(): void { |
| 132 | + let restarting = false; |
| 133 | + const appInitialPrompt = initialPrompt; |
| 134 | + initialPrompt = undefined; |
| 135 | + const appResumeSessionId = resumeSessionId; |
| 136 | + resumeSessionId = undefined; |
| 137 | + const appForkSessionId = forkSessionId; |
| 138 | + forkSessionId = undefined; |
| 139 | + const inkInstance = render( |
| 140 | + <AppContainer |
| 141 | + projectRoot={projectRoot} |
| 142 | + settingsFile={settingsFile} |
| 143 | + version={packageInfo?.version ?? CLI_VERSION} |
| 144 | + initialPrompt={appInitialPrompt} |
| 145 | + resumeSessionId={appResumeSessionId} |
| 146 | + forkSessionId={typeof appForkSessionId === "string" ? appForkSessionId : undefined} |
| 147 | + onRestart={() => restartRef.current?.()} |
| 148 | + />, |
| 149 | + { exitOnCtrlC: false } |
| 150 | + ); |
| 151 | + |
| 152 | + restartRef.current = () => { |
| 153 | + restarting = true; |
| 154 | + writeStdoutLine("\u001B[2J\u001B[3J\u001B[H"); |
| 155 | + inkInstance.unmount(); |
| 156 | + startApp(); |
| 157 | + }; |
| 158 | + |
| 159 | + inkInstance.waitUntilExit().then(() => { |
| 160 | + if (!restarting) { |
| 161 | + restartRef.current = null; |
| 162 | + process.exit(0); |
| 163 | + } |
| 164 | + }); |
| 165 | + } |
| 166 | + |
| 167 | + void checkForNpmUpdate(packageInfo); |
| 168 | + |
| 169 | + startApp(); |
| 170 | +} |
| 171 | + |
| 172 | +/** |
| 173 | + * Configure shell environment for Windows. |
| 174 | + * Sets NoDefaultCurrentDirectoryInExePath and resolves Git Bash path. |
| 175 | + * Must be called after --version/--help handling to avoid blocking those |
| 176 | + * commands on Windows machines without Git Bash installed. |
| 177 | + */ |
| 178 | +function configureWindowsShell(): void { |
| 179 | + process.env.NoDefaultCurrentDirectoryInExePath = "1"; |
| 180 | + try { |
| 181 | + setShellIfWindows(); |
| 182 | + } catch (error) { |
| 183 | + const message = error instanceof Error ? error.message : String(error); |
| 184 | + writeStderrLine(`deepcode: ${message}\n`); |
| 185 | + process.exit(1); |
| 186 | + } |
| 187 | +} |
0 commit comments