MCPcopy
hub / github.com/claude-code-best/claude-code / createAndSaveSnapshot

Function createAndSaveSnapshot

src/utils/bash/ShellSnapshot.ts:413–583  ·  view source on GitHub ↗
(
  binShell: string,
)

Source from the content-addressed store, hash-verified

411 * @returns Promise that resolves to the snapshot file path or undefined if creation failed
412 */
413export 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 // biome-ignore lint/suspicious/noAsyncPromiseExecutor: async needed for sequential awaits inside executor
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 Claude Code 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 await mkdir(snapshotsDir, { recursive: true })
449
450 const snapshotScript = await getSnapshotScript(
451 binShell,
452 shellSnapshotPath,
453 configFileExists,
454 )
455 logForDebugging(`Creating snapshot at: ${shellSnapshotPath}`)
456 logForDebugging(`Execution timeout: ${SNAPSHOT_CREATION_TIMEOUT}ms`)
457 execFile(
458 binShell,
459 ['-c', '-l', snapshotScript],
460 {
461 env: {
462 ...((process.env.CLAUDE_CODE_DONT_INHERIT_ENV
463 ? {}
464 : subprocessEnv()) as typeof process.env),
465 SHELL: binShell,
466 GIT_EDITOR: 'true',
467 CLAUDECODE: '1',
468 },
469 timeout: SNAPSHOT_CREATION_TIMEOUT,
470 maxBuffer: 1024 * 1024, // 1MB buffer

Callers 1

createBashShellProviderFunction · 0.85

Calls 15

getConfigFileFunction · 0.85
pathExistsFunction · 0.85
mkdirFunction · 0.85
getSnapshotScriptFunction · 0.85
subprocessEnvFunction · 0.85
getCwdFunction · 0.85
logEventFunction · 0.85
statFunction · 0.85
registerCleanupFunction · 0.85
getFsImplementationFunction · 0.85
nowMethod · 0.80
toStringMethod · 0.65

Tested by

no test coverage detected