Add Git-style hooks mechanism for Node.js installation #1459
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This PR introduces a powerful Git-style hooks system for fnm, starting with comprehensive support for Node.js installation hooks. Users can now create custom scripts that automatically execute at key points during the installation process, enabling automation of environment setup, package management, notifications, and more.
Fixes #1060
Disclaimer
I am not a Rust developer, but I love Fnm and I feel that this feature would be extremely useful. So I used Claude Sonnet 4 to generate a Hooks system.
The following is the prompt I used to generate this code:
I hope this PR is either good enough to merge or can act as a good starting point for discussion and implementation.
Features
Hook Types
pre-install: Executed before downloading and installing a Node.js versionpost-install: Executed after successful installation of a Node.js versioninstall-failed`: Executed when Node.js installation fails
Hook Location
Hooks are stored in the
hooks/subdirectory of the fnm directory:Default:
~/.fnm/hooks/Custom:
$FNM_DIR/hooks/(when using custom FNM_DIR)Environment Variables
All hooks receive rich context through environment variables:
FNM_VERSION: Node.js version being installed (e.g., "v18.17.0")FNM_ARCH: Architecture being used (e.g., "x64", "arm64")FNM_DIR: fnm installation directoryFNM_INSTALLATION_DIR: Specific installation directory for this versionTechnical Implementation
Core Components
New hooks module
hooks.rs: Complete hooks management system with cross-platform executable detection and error handlingInstall command integration
install.rs: Seamless hook execution at appropriate lifecycle pointsConfiguration support
config.rs: Addedbase_dir()method for hooks directory resolutionCross-Platform Support
Error Handling
Usage Examples
🔥 Auto-install global packages 🔥
This replicates a killer feature of NVM and is the main reason I wanted to add this feature
Environment setup
Project dependency management
Error logging and notifications
Testing
Documentation
HOOKS.md: Comprehensive documentation with examples, troubleshooting, and best practicesHook templates: Ready-to-use examples for common scenarios
API reference: Complete environment variable and error handling documentation
Future Enhancements
This PR establishes the foundation for a comprehensive hooks system. Future enhancements could include:
pre-use,post-use,pre-uninstall,post-uninstall,pre-exec,post-exec)Testing Instructions
cargo build --releasemkdir -p ~/.fnm/hooksfnm install 18.17.0This implementation provides a solid foundation for user automation and workflow integration while maintaining fnm's simplicity and performance characteristics.