A ZSH plugin that automatically switches Node.js versions (using nvm
) and manages pnpm
package manager versions (using corepack
) when changing directories.
- 🔄 Automatically detects and switches to the correct Node.js version specified in
.npmrc
files - 📦 Handles
pnpm
package manager versions via Corepack - 🚀 Seamlessly activates the correct environment when you
cd
into a project - 🔧 Installs missing Node.js versions automatically when needed
- 🏠 Special workspace directory integration with optional project listing
- 😶 Silent operation by default - only shows messages when needed
- ⚙️ Interactive configuration via command line
- 🔁 Smart installation/update process that preserves existing settings
- 📖 Built-in help system with detailed command documentation
- 🔄 Self-update capability to keep the plugin current
- 🗑️ Clean uninstallation option to remove all traces of the plugin
You can install the plugin directly from GitHub using one of the following methods:
For automated/unattended installation (using default settings):
git clone https://github.com/spencerbeggs/zsh-nvm-pnpm-auto-switch.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-nvm-pnpm-auto-switch && ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-nvm-pnpm-auto-switch/src/install.sh --unattended
Download and run the installer wizard:
git clone https://github.com/spencerbeggs/zsh-nvm-pnpm-auto-switch.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-nvm-pnpm-auto-switch && ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-nvm-pnpm-auto-switch/src/install.sh
For automated/unattended installation (using default settings):
curl -fsSL https://github.com/spencerbeggs/zsh-nvm-pnpm-auto-switch/releases/latest/download/install-remote.sh | zsh -c "zsh /dev/stdin --unattended"
Download and run the installer wizard:
curl -fsSL https://github.com/spencerbeggs/zsh-nvm-pnpm-auto-switch/releases/latest/download/install-remote.sh -o /tmp/install-remote.sh && zsh /tmp/install-remote.sh
For automated/unattended installation (using default settings):
wget -O- https://github.com/spencerbeggs/zsh-nvm-pnpm-auto-switch/releases/latest/download/install-remote.sh | zsh -c "zsh /dev/stdin --unattended"
Download and run the installer wizard:
wget -O /tmp/install-remote.sh https://github.com/spencerbeggs/zsh-nvm-pnpm-auto-switch/releases/latest/download/install-remote.sh && zsh /tmp/install-remote.sh
Note: These installation methods use GitHub's
/releases/latest
endpoint, which automatically redirects to the most recent stable release.
To update the plugin to the latest version, you can use the built-in update command:
# Update the plugin to the latest version
nvm_pnpm_auto_switch_update
This command will automatically detect the best method to update the plugin (Git, curl, or wget), and will handle the update process for you.
Alternatively, you can manually update using one of the installation methods:
# If you used Git
cd ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-nvm-pnpm-auto-switch
git pull
./src/install.sh
# Or use the remote installer again
curl -fsSL https://github.com/spencerbeggs/zsh-nvm-pnpm-auto-switch/releases/latest/download/install-remote.sh | zsh
Using the nvm_pnpm_auto_switch_update
command will run the update in unattended mode, preserving your existing configuration. If you want to update and reconfigure your settings, you can use one of the manual update methods.
To remove the plugin and all related files and settings, use the built-in uninstall command:
# Uninstall the plugin
nvm_pnpm_auto_switch_uninstall
This command will:
- Remove all environment variables from your
.zshenv
file - Remove the plugin from your
.zshrc
file - Delete the plugin directory
- Unset all environment variables for the current session
- Provide instructions for completing the uninstallation
-
Copy the plugin files to your ZSH plugins directory:
# Create the plugin directory if it doesn't exist mkdir -p ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-nvm-pnpm-auto-switch # Copy the main plugin file cp src/zsh-nvm-pnpm-auto-switch.plugin.zsh ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-nvm-pnpm-auto-switch/ # Copy the workspace configuration file cp src/workspace-config.zsh ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-nvm-pnpm-auto-switch/
-
Add the plugin to your
.zshrc
:plugins=(... zsh-nvm-pnpm-auto-switch)
-
Add the required environment variables to your
.zshenv
:# Workspace directory for zsh-nvm-pnpm-auto-switch plugin export NVM_PNPM_AUTO_SWITCH_WORKSPACE="$HOME/workspace" # Enable project listing in workspace (0=off, 1=on) export NVM_PNPM_AUTO_SWITCH_LIST_PROJECTS=0 # Enable debug mode for zsh-nvm-pnpm-auto-switch (0=off, 1=on) export NVM_PNPM_AUTO_SWITCH_DEBUG=0
-
Restart your shell or run:
source ~/.zshrc
Once installed, the plugin works automatically:
- When you
cd
into a directory containing apackage.json
file, the plugin activates. - If a
.npmrc
file exists with anode-version
specification, the plugin switches to that version. - If the project uses
pnpm
(specified in thepackageManager
field ofpackage.json
), the plugin ensures Corepack is enabled and the correct pnpm version is activated.
The plugin only displays messages when it needs to switch Node.js versions or update package managers.
After installation, the following commands are available to help you manage the plugin:
Command | Description |
---|---|
nvm_pnpm_auto_switch_help |
Display detailed help information about all available commands |
nvm_pnpm_auto_switch_man |
Alias for nvm_pnpm_auto_switch_help |
nvm_pnpm_auto_switch_configure |
Run the interactive configuration wizard |
nvm_pnpm_auto_switch_workspace [path] |
View or change the workspace directory |
nvm_pnpm_auto_switch_list_projects |
Toggle automatic project listing on/off |
nvm_pnpm_auto_switch_debug |
Toggle debug mode on/off |
nvm_pnpm_auto_switch_update |
Update the plugin to the latest version from GitHub |
nvm_pnpm_auto_switch_uninstall |
Uninstall the plugin and clean up all related files and settings |
# Display full help information
nvm_pnpm_auto_switch_help
# Alias for help (same as above)
nvm_pnpm_auto_switch_man
The help command provides detailed information about all available commands and environment variables. It's a great starting point if you're new to the plugin or need a refresher on what commands are available.
# Run the interactive configuration wizard
nvm_pnpm_auto_switch_configure
This command launches an interactive configuration wizard that helps you set up or change the plugin's settings. It shows current settings and allows you to update them with guided prompts. Changes are saved to your .zshenv
file automatically.
# Check the current workspace directory
nvm_pnpm_auto_switch_workspace
# Set a new workspace directory
nvm_pnpm_auto_switch_workspace ~/dev
The workspace directory is a special directory where you can enable additional features like project listing. By default, this is set to ~/workspace
, but you can change it to any directory you prefer.
# Toggle project listing on/off
nvm_pnpm_auto_switch_list_projects
When project listing is enabled, the plugin will display a list of available projects when you enter your workspace directory. This is useful for quickly seeing what projects are available.
# Toggle debug mode on/off
nvm_pnpm_auto_switch_debug
Debug mode provides detailed information about what the plugin is doing, which can be helpful for troubleshooting. When enabled, you'll see messages about detected Node.js versions, package managers, and more.
# Update the plugin to the latest version
nvm_pnpm_auto_switch_update
This command will automatically update the plugin to the latest version from GitHub. It will try to use Git if you installed via git clone, or fall back to curl or wget if necessary.
# Remove the plugin and clean up all related files and settings
nvm_pnpm_auto_switch_uninstall
If you no longer want to use the plugin, this command will completely remove it and clean up all related files and settings. It will ask for confirmation before proceeding.
The plugin uses the following environment variables to control its behavior:
Variable | Description | Default |
---|---|---|
NVM_PNPM_AUTO_SWITCH_WORKSPACE |
Path to your workspace directory | $HOME/workspace |
NVM_PNPM_AUTO_SWITCH_LIST_PROJECTS |
Enable project listing in workspace (0=off, 1=on) | 0 |
NVM_PNPM_AUTO_SWITCH_DEBUG |
Enable debug mode (0=off, 1=on) | 0 |
You can set these variables in your .zshenv
file manually, or use the nvm_pnpm_auto_switch_configure
command to set them interactively.
node-version=18.16.0
{
"name": "my-project",
"packageManager": "[email protected]"
}
The plugin hooks into ZSH's chpwd
function which is triggered whenever you change directories. It then:
- Checks if the current directory is your configured workspace
- Checks if the current directory is a Node.js project
- Reads the Node.js version from
.npmrc
- Switches Node versions using nvm if needed
- Checks if the project uses pnpm as its package manager
- Enables Corepack and activates the correct pnpm version if needed
MIT
Contributions are welcome! Please see our CONTRIBUTING.md file for details on how to contribute to this project. For security-related issues, please review our SECURITY.md file.