Configure file watching hooks to auto-react to config changes, env file updates, and dependency modifications. Use to set up reactive workflows.
git clone https://github.com/rohitg00/pro-workflow.git--- name: file-watcher description: Configure file watching hooks to auto-react to config changes, env file updates, and dependency modifications. Use to set up reactive workflows. user-invocable: true --- # File Watcher Use Claude Code's `FileChanged` and `CwdChanged` hooks to create reactive workflows that respond to file system changes. ## Trigger 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 `SessionStart` and `CwdChanged` hooks support returning `watchPaths` to register file watchers. The current `cwd-changed.js` script focuses on env injection; to add watch registration, your hook script must output this JSON structure: ```json { "hookSpecificOutput": { "hookEventName": "SessionStart", "watchPaths": [ "/absolute/path/to/.env", "/absolute/path/to/package.json" ] } } ``` When watched files change, the `FileChanged` hook fires with: ```json { "hook_event_name": "FileChanged", "file_path": "/path/to/changed/file", "event": "change" } ``` ## Environment Injection `CwdChanged` and `FileChanged` hooks can write to `CLAUDE_ENV_FILE` to inject environment variables into subsequent Bash commands: ```bash echo "export PROJECT_TYPE=node" >> "$CLAUDE_ENV_FILE" echo "export TEST_CMD='npm test'" >> "$CLAUDE_ENV_FILE" ``` ## Common Watch Patterns ### Watch .env for Changes ```javascript const envFile = path.join(projectRoot, '.env'); if (fs.existsSync(envFile)) { output.hookSpecificOutput = { hookEventName: 'SessionStart', watchPaths: [envFile] }; } ``` ### Watch package.json for Dependency Changes Detect when dependencies change and remind to run `npm install`. ### Watch tsconfig.json for Config Changes Remind to restart TypeScript checks when config changes. ## Setup Add to 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 `CLAUDE_ENV_FILE` for injecting env vars, not direct export
["Install a file watcher tool (e.g., `nodemon` for Node.js, `entr` for Unix, or `watchdog` for Python).","Specify the directory path and file types to watch using the tool's configuration flags (e.g., `--watch ./config --ext .env,.json`).","Define the action to perform on changes (e.g., restart a service, reload a config, or run a script) using the tool's `--exec` or equivalent flag.","Set a debounce delay (e.g., `--delay 500`) to ignore rapid successive changes and prevent unnecessary reactions.","Test the setup by manually editing a watched file and verifying the action is triggered as expected. Review logs to confirm behavior."]
No install command available. Check the GitHub repository for manual installation instructions.
git clone https://github.com/rohitg00/pro-workflow/tree/main/skills/file-watcherCopy the install command above and run it in your terminal.
Launch Claude Code, Cursor, or your preferred AI coding agent.
Use the prompt template or examples below to test the skill.
Adapt the skill to your specific use case and workflow.
Set up a file watcher to monitor [DIRECTORY_PATH] for changes to [FILE_TYPES_OR_NAMES]. When a change is detected, automatically [ACTION_TO_PERFORM]. Include a debounce delay of [DEBOUNCE_MS] to avoid triggering on rapid successive changes. Log all actions to [LOG_FILE_PATH].
File watcher configured to monitor `/home/user/project/config/` for changes to `.env` and `*.json` files. Changes detected at 14:22:17 to `/home/user/project/config/.env` (line 42 modified from `DB_HOST=localhost` to `DB_HOST=10.0.1.5`). Debounce delay of 500ms applied; no action triggered due to rapid succession (next change detected at 14:22:17.300). At 14:22:18, a single `.json` file (`/home/user/project/config/settings.json`) was modified (added `"debug_mode": true`). Action triggered: Docker container restarted via `docker compose restart app`. Log entry written to `/var/log/file_watcher.log`: `2024-05-20 14:22:18 - settings.json updated - container restarted`.
skills-collection
Take a free 3-minute scan and get personalized AI skill recommendations.
Take free scan