file-watcher

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

File 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
{{file}}
placeholder that gets replaced with the changed file path.
使用Chokidar搭建文件监视器,监控指定路径下的文件变更,并针对每个变更的文件运行Claude提示词。提示词支持
{{file}}
占位符,会被替换为变更文件的路径。

Quick Start

快速开始

bash
undefined
bash
undefined

Watch 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}}'
undefined
bun run watch:files -- ./src 'Check {{file}}'
undefined

CLI Reference

CLI 参考

node scripts/watch-files.mjs <path> <prompt> [--glob '<pattern>'] [--debounce <ms>]
ArgRequiredDescription
<path>
YesFile or directory to watch
<prompt>
YesClaude prompt. Use
{{file}}
for the changed file path
--glob
NoGlob filter (e.g.
*.tsx
,
*.css
)
--debounce
NoDebounce delay in milliseconds (default: 500)
node scripts/watch-files.mjs <path> <prompt> [--glob '<pattern>'] [--debounce <ms>]
参数是否必填描述
<path>
要监控的文件或目录
<prompt>
Claude提示词。使用
{{file}}
作为变更文件路径的占位符
--glob
Glob过滤规则(例如
*.tsx
,
*.css
--debounce
防抖延迟时间(毫秒,默认值:500)

How It Works

工作原理

  1. Chokidar watches the target path for
    add
    and
    change
    events
  2. Changes are debounced (default 500ms) to batch rapid saves
  3. For each changed file, the
    {{file}}
    placeholder in the prompt is replaced with the actual file path
  4. claude -p '<prompt>' --print
    is executed via
    child_process.execSync
  5. Claude's output is logged to stdout
  1. Chokidar监控目标路径下的
    add
    change
    事件
  2. 变更事件会被防抖处理(默认500毫秒),以合并频繁的保存操作
  3. 对于每个变更的文件,提示词中的
    {{file}}
    占位符会被替换为实际文件路径
  4. 通过
    child_process.execSync
    执行
    claude -p '<prompt>' --print
    命令
  5. Claude的输出会被记录到标准输出

Example Prompts

提示词示例

Use CasePrompt
Code review
'Review {{file}} for bugs and suggest fixes'
Type checking
'Check {{file}} for TypeScript type issues'
Design sync
'The design file {{file}} changed. Update the corresponding React component'
Test generation
'Generate unit tests for {{file}}'
Documentation
'Update documentation for changes in {{file}}'
使用场景提示词
代码审查
'Review {{file}} for bugs and suggest fixes'
类型检查
'Check {{file}} for TypeScript type issues'
设计同步
'The design file {{file}} changed. Update the corresponding React component'
测试生成
'Generate unit tests for {{file}}'
文档更新
'Update documentation for changes in {{file}}'

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

故障排除

IssueSolution
claude
CLI not found
Install Claude Code:
npm install -g @anthropic-ai/claude-code
Watcher not detecting changesCheck the path exists and you have read permissions
Too many invocationsIncrease
--debounce
value
Prompt with special charactersWrap prompt in single quotes;
{{file}}
is the only placeholder
Permission deniedEnsure the watched directory is readable
问题解决方案
claude
CLI未找到
安装Claude Code:
npm install -g @anthropic-ai/claude-code
监视器未检测到变更检查路径是否存在,且您拥有读取权限
调用过于频繁增大
--debounce
的值
提示词包含特殊字符使用单引号包裹提示词;仅支持
{{file}}
这一个占位符
权限被拒绝确保被监控的目录可读

Dependencies

依赖项

  • chokidar
    (devDependency) — file system watcher
  • claude
    CLI — must be installed globally and authenticated
  • chokidar
    (开发依赖)——文件系统监视器
  • claude
    CLI —— 必须全局安装并完成身份验证