* Creates the appropriate shell script for capturing environment
( shellPath: string, snapshotFilePath: string, configFileExists: boolean, )
| 343 | * Creates the appropriate shell script for capturing environment |
| 344 | */ |
| 345 | async function getSnapshotScript( |
| 346 | shellPath: string, |
| 347 | snapshotFilePath: string, |
| 348 | configFileExists: boolean, |
| 349 | ): Promise<string> { |
| 350 | const configFile = getConfigFile(shellPath) |
| 351 | const isZsh = configFile.endsWith('.zshrc') |
| 352 | |
| 353 | // Generate the user content and Claude Code content |
| 354 | const userContent = configFileExists |
| 355 | ? getUserSnapshotContent(configFile) |
| 356 | : !isZsh |
| 357 | ? // we need to manually force alias expansion in bash - normally `getUserSnapshotContent` takes care of this |
| 358 | 'echo "shopt -s expand_aliases" >> "$SNAPSHOT_FILE"' |
| 359 | : '' |
| 360 | const claudeCodeContent = await getClaudeCodeSnapshotContent() |
| 361 | |
| 362 | const script = `SNAPSHOT_FILE=${quote([snapshotFilePath])} |
| 363 | ${configFileExists ? `source "${configFile}" < /dev/null` : '# No user config file to source'} |
| 364 | |
| 365 | # First, create/clear the snapshot file |
| 366 | echo "# Snapshot file" >| "$SNAPSHOT_FILE" |
| 367 | |
| 368 | # When this file is sourced, we first unalias to avoid conflicts |
| 369 | # This is necessary because aliases get "frozen" inside function definitions at definition time, |
| 370 | # which can cause unexpected behavior when functions use commands that conflict with aliases |
| 371 | echo "# Unset all aliases to avoid conflicts with functions" >> "$SNAPSHOT_FILE" |
| 372 | echo "unalias -a 2>/dev/null || true" >> "$SNAPSHOT_FILE" |
| 373 | |
| 374 | ${userContent} |
| 375 | |
| 376 | ${claudeCodeContent} |
| 377 | |
| 378 | # Exit silently on success, only report errors |
| 379 | if [ ! -f "$SNAPSHOT_FILE" ]; then |
| 380 | echo "Error: Snapshot file was not created at $SNAPSHOT_FILE" >&2 |
| 381 | exit 1 |
| 382 | fi |
| 383 | ` |
| 384 | |
| 385 | return script |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * Creates and saves the shell environment snapshot by loading the user's shell configuration |
no test coverage detected