
Le truc qui change TOUT sur Claude Code (les hooks)
AI Summary
This video explains Cloud Code hooks, which are small scripts that execute at specific moments during Cloud Code operations. They can be configured in a `settings.json` file, either per project or globally for all projects. Hooks are powerful because they can prevent Cloud Code from making mistakes, acting as a "wall" rather than a mere suggestion.
The video highlights five useful hooks:
1. **`pre_tool_use`**: Executes before Cloud Code uses a tool. This is the only moment you can block Cloud Code's actions.
2. **`post_tool_use`**: Executes after a tool has been used, useful for tasks like code formatting or logging.
3. **`on_stop`**: Triggers when Cloud Code finishes a task, allowing for notifications like playing a sound.
4. **`on_notification`**: Activates when Cloud Code requires user validation in the middle of a task.
5. **`session_start`**: Runs at the beginning of a session, ideal for injecting fresh context (e.g., current Git branch, pending to-dos) into Cloud Code.
The first hook demonstrated plays a sound notification when Cloud Code finishes a task or waits for confirmation. This prevents users from missing Cloud Code's prompts and ensures productivity.
The second hook, configured with `pre_tool_use`, prevents dangerous commands like `rm -rf`, `git push --force`, or `drop database`. It acts as a guard, blocking these destructive actions.
The third hook uses `post_tool_use` to automatically format code with Prettier after Cloud Code performs an action, ensuring consistent code style.
The fourth hook, also using `pre_tool_use`, protects critical files like `.env` or `.git` from being deleted or modified by Cloud Code. It blocks commands that attempt to touch these protected files.
The final hook, `session_start`, injects dynamic context into the Cloud Code session. This can include the current Git branch, recent commits, or test statuses, providing Cloud Code with up-to-date information.
While `cloud.md` provides suggestions, hooks enforce strict rules. It's recommended to use a few well-understood hooks rather than many that could slow down Cloud Code. Users are cautioned to be careful with hooks, as they run with full user permissions and a misconfigured hook could expose secrets or delete files. If working in a team, the `.cloud/settings.json` file is shared, so all team members should be aware of its contents.