file-watcher
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseFile Watcher
文件监听器
Use Claude Code's and hooks to create reactive workflows that respond to file system changes.
FileChangedCwdChanged使用 Claude Code 的 和 Hook,创建可响应文件系统变更的响应式工作流。
FileChangedCwdChangedTrigger
触发场景
Use when:
- Setting up auto-reload for config changes
- Watching for dependency updates
- Monitoring build output
- Creating reactive development workflows
适用于:
- 为配置变更设置自动重新加载
- 监听依赖项更新
- 监控构建输出
- 创建响应式开发工作流
How File Watching Works
文件监听的工作原理
Claude Code's and hooks support returning to register file watchers. The current script focuses on env injection; to add watch registration, your hook script must output this JSON structure:
SessionStartCwdChangedwatchPathscwd-changed.jsjson
{
"hookSpecificOutput": {
"hookEventName": "SessionStart",
"watchPaths": [
"/absolute/path/to/.env",
"/absolute/path/to/package.json"
]
}
}When watched files change, the hook fires with:
FileChangedjson
{
"hook_event_name": "FileChanged",
"file_path": "/path/to/changed/file",
"event": "change"
}Claude Code 的 和 Hook 支持返回 以注册文件监听器。当前的 脚本侧重于 env 注入;要添加监听注册,Hook 脚本必须输出以下 JSON 结构:
SessionStartCwdChangedwatchPathscwd-changed.jsjson
{
"hookSpecificOutput": {
"hookEventName": "SessionStart",
"watchPaths": [
"/absolute/path/to/.env",
"/absolute/path/to/package.json"
]
}
}被监听的文件发生变更时, Hook 会触发并携带以下信息:
FileChangedjson
{
"hook_event_name": "FileChanged",
"file_path": "/path/to/changed/file",
"event": "change"
}Environment Injection
环境变量注入
CwdChangedFileChangedCLAUDE_ENV_FILEbash
echo "export PROJECT_TYPE=node" >> "$CLAUDE_ENV_FILE"
echo "export TEST_CMD='npm test'" >> "$CLAUDE_ENV_FILE"CwdChangedFileChangedCLAUDE_ENV_FILEbash
echo "export PROJECT_TYPE=node" >> "$CLAUDE_ENV_FILE"
echo "export TEST_CMD='npm test'" >> "$CLAUDE_ENV_FILE"Common Watch Patterns
常见监听模式
Watch .env for Changes
监听 .env 的变更
javascript
const envFile = path.join(projectRoot, '.env');
if (fs.existsSync(envFile)) {
output.hookSpecificOutput = {
hookEventName: 'SessionStart',
watchPaths: [envFile]
};
}javascript
const envFile = path.join(projectRoot, '.env');
if (fs.existsSync(envFile)) {
output.hookSpecificOutput = {
hookEventName: 'SessionStart',
watchPaths: [envFile]
};
}Watch package.json for Dependency Changes
监听 package.json 中的依赖项变更
Detect when dependencies change and remind to run .
npm install检测依赖项发生变更时,提醒运行 。
npm installWatch tsconfig.json for Config Changes
监听 tsconfig.json 的配置变更
Remind to restart TypeScript checks when config changes.
配置发生变更时,提醒重新启动 TypeScript 检查。
Setup
设置
Add to hooks.json:
json
{
"FileChanged": [{
"matcher": ".env|package.json|tsconfig.json",
"hooks": [{
"type": "command",
"command": "node scripts/file-changed.js"
}]
}]
}在 hooks.json 中添加:
json
{
"FileChanged": [{
"matcher": ".env|package.json|tsconfig.json",
"hooks": [{
"type": "command",
"command": "node scripts/file-changed.js"
}]
}]
}Rules
规则
- Use absolute paths for watchPaths (required by Claude Code)
- Matcher uses pipe-separated filenames
- Watcher uses 500ms stability threshold and 200ms poll interval
- Keep file-changed handlers fast (<5s) to avoid blocking
- Use for injecting env vars, not direct export
CLAUDE_ENV_FILE
- 必须使用绝对路径(Claude Code 的要求)
watchPaths - Matcher 使用竖线分隔文件名
- 文件监听器采用 500ms 稳定阈值和 200ms 轮询间隔
- 文件变更处理程序应保持快速(少于 5 秒),以避免阻塞
- 使用 注入环境变量,不要直接 export
CLAUDE_ENV_FILE