file-watcher
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseFile Watcher Skill
File Watcher Skill
Overview
概述
Sets up a file watcher using chokidar that monitors a path for changes and runs a Claude prompt against every changed file. The prompt supports a placeholder that gets replaced with the changed file path.
{{file}}使用Chokidar搭建文件监视器,监控指定路径下的文件变更,并针对每个变更的文件运行Claude提示词。提示词支持占位符,会被替换为变更文件的路径。
{{file}}Quick Start
快速开始
bash
undefinedbash
undefinedWatch src/ and analyze any changed file
监控src/目录并分析所有变更的文件
node scripts/watch-files.mjs ./src 'Analyze {{file}} for issues'
node scripts/watch-files.mjs ./src 'Analyze {{file}} for issues'
Watch only .tsx files with 1s debounce
仅监控.tsx文件,设置1秒防抖
node scripts/watch-files.mjs ./src 'Review {{file}} for React best practices' --glob '*.tsx' --debounce 1000
node scripts/watch-files.mjs ./src 'Review {{file}} for React best practices' --glob '*.tsx' --debounce 1000
Via package.json script (pass args after --)
通过package.json脚本运行(参数在--后传递)
bun run watch:files -- ./src 'Check {{file}}'
undefinedbun run watch:files -- ./src 'Check {{file}}'
undefinedCLI Reference
CLI 参考
node scripts/watch-files.mjs <path> <prompt> [--glob '<pattern>'] [--debounce <ms>]| Arg | Required | Description |
|---|---|---|
| Yes | File or directory to watch |
| Yes | Claude prompt. Use |
| No | Glob filter (e.g. |
| No | Debounce delay in milliseconds (default: 500) |
node scripts/watch-files.mjs <path> <prompt> [--glob '<pattern>'] [--debounce <ms>]| 参数 | 是否必填 | 描述 |
|---|---|---|
| 是 | 要监控的文件或目录 |
| 是 | Claude提示词。使用 |
| 否 | Glob过滤规则(例如 |
| 否 | 防抖延迟时间(毫秒,默认值:500) |
How It Works
工作原理
- Chokidar watches the target path for and
addeventschange - Changes are debounced (default 500ms) to batch rapid saves
- For each changed file, the placeholder in the prompt is replaced with the actual file path
{{file}} - is executed via
claude -p '<prompt>' --printchild_process.execSync - Claude's output is logged to stdout
- Chokidar监控目标路径下的和
add事件change - 变更事件会被防抖处理(默认500毫秒),以合并频繁的保存操作
- 对于每个变更的文件,提示词中的占位符会被替换为实际文件路径
{{file}} - 通过执行
child_process.execSync命令claude -p '<prompt>' --print - Claude的输出会被记录到标准输出
Example Prompts
提示词示例
| Use Case | Prompt |
|---|---|
| Code review | |
| Type checking | |
| Design sync | |
| Test generation | |
| Documentation | |
| 使用场景 | 提示词 |
|---|---|
| 代码审查 | |
| 类型检查 | |
| 设计同步 | |
| 测试生成 | |
| 文档更新 | |
Configuration
配置
Glob Patterns
Glob 规则
Filter which files trigger the watcher:
bash
--glob '*.tsx' # Only TypeScript React files
--glob '*.css' # Only CSS files
--glob '*.{ts,tsx}' # TypeScript files (not supported by chokidar glob — use without braces)过滤哪些文件会触发监视器:
bash
--glob '*.tsx' # 仅监控TypeScript React文件
--glob '*.css' # 仅监控CSS文件
--glob '*.{ts,tsx}' # TypeScript文件(Chokidar的glob不支持大括号,使用时请去掉大括号)Debounce
防抖设置
Control how long to wait after the last change before triggering:
bash
--debounce 200 # Fast (200ms) — good for single-file saves
--debounce 1000 # Slow (1s) — good for batch operations
--debounce 5000 # Very slow (5s) — good for build output directories控制最后一次变更后等待多久再触发操作:
bash
--debounce 200 # 快速(200毫秒)——适合单文件保存
--debounce 1000 # 慢速(1秒)——适合批量操作
--debounce 5000 # 极慢(5秒)——适合构建输出目录Troubleshooting
故障排除
| Issue | Solution |
|---|---|
| Install Claude Code: |
| Watcher not detecting changes | Check the path exists and you have read permissions |
| Too many invocations | Increase |
| Prompt with special characters | Wrap prompt in single quotes; |
| Permission denied | Ensure the watched directory is readable |
| 问题 | 解决方案 |
|---|---|
| 安装Claude Code: |
| 监视器未检测到变更 | 检查路径是否存在,且您拥有读取权限 |
| 调用过于频繁 | 增大 |
| 提示词包含特殊字符 | 使用单引号包裹提示词;仅支持 |
| 权限被拒绝 | 确保被监控的目录可读 |
Dependencies
依赖项
- (devDependency) — file system watcher
chokidar - CLI — must be installed globally and authenticated
claude
- (开发依赖)——文件系统监视器
chokidar - CLI —— 必须全局安装并完成身份验证
claude