MCPcopy
hub / github.com/codeaashu/claude-code / initializeKeybindingWatcher

Function initializeKeybindingWatcher

src/keybindings/loadUserBindings.ts:353–404  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

351 * For external users, this is a no-op since user customization is disabled.
352 */
353export async function initializeKeybindingWatcher(): Promise<void> {
354 if (initialized || disposed) return
355
356 // Skip file watching for external users
357 if (!isKeybindingCustomizationEnabled()) {
358 logForDebugging(
359 '[keybindings] Skipping file watcher - user customization disabled',
360 )
361 return
362 }
363
364 const userPath = getKeybindingsPath()
365 const watchDir = dirname(userPath)
366
367 // Only watch if parent directory exists
368 try {
369 const stats = await stat(watchDir)
370 if (!stats.isDirectory()) {
371 logForDebugging(
372 `[keybindings] Not watching: ${watchDir} is not a directory`,
373 )
374 return
375 }
376 } catch {
377 logForDebugging(`[keybindings] Not watching: ${watchDir} does not exist`)
378 return
379 }
380
381 // Set initialized only after we've confirmed we can watch
382 initialized = true
383
384 logForDebugging(`[keybindings] Watching for changes to ${userPath}`)
385
386 watcher = chokidar.watch(userPath, {
387 persistent: true,
388 ignoreInitial: true,
389 awaitWriteFinish: {
390 stabilityThreshold: FILE_STABILITY_THRESHOLD_MS,
391 pollInterval: FILE_STABILITY_POLL_INTERVAL_MS,
392 },
393 ignorePermissionErrors: true,
394 usePolling: false,
395 atomic: true,
396 })
397
398 watcher.on('add', handleChange)
399 watcher.on('change', handleChange)
400 watcher.on('unlink', handleDelete)
401
402 // Register cleanup
403 registerCleanup(async () => disposeKeybindingWatcher())
404}
405
406/**
407 * Clean up the file watcher.

Callers 1

KeybindingSetupFunction · 0.85

Calls 8

logForDebuggingFunction · 0.85
getKeybindingsPathFunction · 0.85
statFunction · 0.85
registerCleanupFunction · 0.85
disposeKeybindingWatcherFunction · 0.85
watchMethod · 0.80
onMethod · 0.80

Tested by

no test coverage detected