( binShell: string, )
| 412 | * @returns Promise that resolves to the snapshot file path or undefined if creation failed |
| 413 | */ |
| 414 | export const createAndSaveSnapshot = async ( |
| 415 | binShell: string, |
| 416 | ): Promise<string | undefined> => { |
| 417 | const shellType = binShell.includes('zsh') |
| 418 | ? 'zsh' |
| 419 | : binShell.includes('bash') |
| 420 | ? 'bash' |
| 421 | : 'sh' |
| 422 | |
| 423 | logForDebugging(`Creating shell snapshot for ${shellType} (${binShell})`) |
| 424 | |
| 425 | return new Promise(async resolve => { |
| 426 | try { |
| 427 | const configFile = getConfigFile(binShell) |
| 428 | logForDebugging(`Looking for shell config file: ${configFile}`) |
| 429 | const configFileExists = await pathExists(configFile) |
| 430 | |
| 431 | if (!configFileExists) { |
| 432 | logForDebugging( |
| 433 | `Shell config file not found: ${configFile}, creating snapshot with NCode defaults only`, |
| 434 | ) |
| 435 | } |
| 436 | |
| 437 | // Create unique snapshot path with timestamp and random ID |
| 438 | const timestamp = Date.now() |
| 439 | const randomId = Math.random().toString(36).substring(2, 8) |
| 440 | const snapshotsDir = join(getClaudeConfigHomeDir(), 'shell-snapshots') |
| 441 | logForDebugging(`Snapshots directory: ${snapshotsDir}`) |
| 442 | const shellSnapshotPath = join( |
| 443 | snapshotsDir, |
| 444 | `snapshot-${shellType}-${timestamp}-${randomId}.sh`, |
| 445 | ) |
| 446 | |
| 447 | // Ensure snapshots directory exists |
| 448 | try { |
| 449 | await mkdir(snapshotsDir, { recursive: true }) |
| 450 | } catch (error) { |
| 451 | if (isFsInaccessible(error)) { |
| 452 | logForDebugging( |
| 453 | `Skipping shell snapshot creation: snapshots dir unavailable (${String(error)})`, |
| 454 | { level: 'warn' }, |
| 455 | ) |
| 456 | resolve(undefined) |
| 457 | return |
| 458 | } |
| 459 | throw error |
| 460 | } |
| 461 | |
| 462 | try { |
| 463 | await writeFile(shellSnapshotPath, '', { mode: 0o600, flag: 'wx' }) |
| 464 | await unlink(shellSnapshotPath) |
| 465 | } catch (error) { |
| 466 | if (isFsInaccessible(error)) { |
| 467 | logForDebugging( |
| 468 | `Skipping shell snapshot creation: snapshot path unavailable (${String(error)})`, |
| 469 | { level: 'warn' }, |
| 470 | ) |
| 471 | resolve(undefined) |
no test coverage detected