( binShell: string, )
| 411 | * @returns Promise that resolves to the snapshot file path or undefined if creation failed |
| 412 | */ |
| 413 | export const createAndSaveSnapshot = async ( |
| 414 | binShell: string, |
| 415 | ): Promise<string | undefined> => { |
| 416 | const shellType = binShell.includes('zsh') |
| 417 | ? 'zsh' |
| 418 | : binShell.includes('bash') |
| 419 | ? 'bash' |
| 420 | : 'sh' |
| 421 | |
| 422 | logForDebugging(`Creating shell snapshot for ${shellType} (${binShell})`) |
| 423 | |
| 424 | return new Promise(async resolve => { |
| 425 | try { |
| 426 | const configFile = getConfigFile(binShell) |
| 427 | logForDebugging(`Looking for shell config file: ${configFile}`) |
| 428 | const configFileExists = await pathExists(configFile) |
| 429 | |
| 430 | if (!configFileExists) { |
| 431 | logForDebugging( |
| 432 | `Shell config file not found: ${configFile}, creating snapshot with Claude Code defaults only`, |
| 433 | ) |
| 434 | } |
| 435 | |
| 436 | // Create unique snapshot path with timestamp and random ID |
| 437 | const timestamp = Date.now() |
| 438 | const randomId = Math.random().toString(36).substring(2, 8) |
| 439 | const snapshotsDir = join(getClaudeConfigHomeDir(), 'shell-snapshots') |
| 440 | logForDebugging(`Snapshots directory: ${snapshotsDir}`) |
| 441 | const shellSnapshotPath = join( |
| 442 | snapshotsDir, |
| 443 | `snapshot-${shellType}-${timestamp}-${randomId}.sh`, |
| 444 | ) |
| 445 | |
| 446 | // Ensure snapshots directory exists |
| 447 | await mkdir(snapshotsDir, { recursive: true }) |
| 448 | |
| 449 | const snapshotScript = await getSnapshotScript( |
| 450 | binShell, |
| 451 | shellSnapshotPath, |
| 452 | configFileExists, |
| 453 | ) |
| 454 | logForDebugging(`Creating snapshot at: ${shellSnapshotPath}`) |
| 455 | logForDebugging(`Execution timeout: ${SNAPSHOT_CREATION_TIMEOUT}ms`) |
| 456 | execFile( |
| 457 | binShell, |
| 458 | ['-c', '-l', snapshotScript], |
| 459 | { |
| 460 | env: { |
| 461 | ...((process.env.CLAUDE_CODE_DONT_INHERIT_ENV |
| 462 | ? {} |
| 463 | : subprocessEnv()) as typeof process.env), |
| 464 | SHELL: binShell, |
| 465 | GIT_EDITOR: 'true', |
| 466 | CLAUDECODE: '1', |
| 467 | }, |
| 468 | timeout: SNAPSHOT_CREATION_TIMEOUT, |
| 469 | maxBuffer: 1024 * 1024, // 1MB buffer |
| 470 | encoding: 'utf8', |
no test coverage detected