mirror of
https://github.com/ivuorinen/nvm-auto-use.fish.git
synced 2026-02-19 10:52:16 +00:00
feat: multiple improvements, additions, vibin'
This commit is contained in:
@@ -1,17 +1,130 @@
|
||||
function nvm_auto_use --on-variable PWD
|
||||
set -l nvmrc_file (nvm_find_nvmrc)
|
||||
if test -n "$nvmrc_file"
|
||||
set -l node_version (cat "$nvmrc_file")
|
||||
|
||||
# Check the current version
|
||||
set -l current_version (node -v 2>/dev/null | sed 's/v//')
|
||||
|
||||
if test "$node_version" != "$current_version"
|
||||
if not nvm use $node_version 2>/dev/null
|
||||
echo "Node.js version $node_version not found, installing..."
|
||||
nvm install $node_version && nvm use $node_version
|
||||
end
|
||||
# Detect available Node.js version manager
|
||||
set -l available_managers (nvm_compat_detect 2>/dev/null | tail -n 1)
|
||||
if test -z "$available_managers"
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Use preferred manager or first available
|
||||
set -l manager
|
||||
if test -n "$_nvm_auto_use_preferred_manager"
|
||||
if contains "$_nvm_auto_use_preferred_manager" $available_managers
|
||||
set manager "$_nvm_auto_use_preferred_manager"
|
||||
end
|
||||
end
|
||||
|
||||
if test -z "$manager"
|
||||
set manager $available_managers[1]
|
||||
end
|
||||
|
||||
# Check if project detection is enabled and we're in a Node.js project
|
||||
if set -q _nvm_auto_use_project_only
|
||||
if not nvm_project_detect
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
# Export NODE_VERSION environment variable
|
||||
if command -q node
|
||||
set -gx NODE_VERSION (node -v 2>/dev/null | string replace 'v' '')
|
||||
end
|
||||
|
||||
# Check for excluded directories
|
||||
set -l current_dir (pwd)
|
||||
for pattern in $_nvm_auto_use_excluded_dirs node_modules .git
|
||||
if string match -q "*/$pattern" "$current_dir"; or string match -q "*/$pattern/*" "$current_dir"
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
# Debouncing: prevent rapid switching
|
||||
set -l debounce_ms (_nvm_auto_use_get_debounce)
|
||||
set -l current_time (date +%s%3N 2>/dev/null; or date +%s)
|
||||
if test -n "$_nvm_auto_use_last_change"
|
||||
set -l time_diff (math "$current_time - $_nvm_auto_use_last_change")
|
||||
if test $time_diff -lt $debounce_ms
|
||||
return
|
||||
end
|
||||
end
|
||||
set -g _nvm_auto_use_last_change $current_time
|
||||
|
||||
set -l nvmrc_file (nvm_find_nvmrc)
|
||||
|
||||
# Cache check: if same file and version, skip processing
|
||||
if test "$nvmrc_file" = "$_nvm_auto_use_cached_file"
|
||||
return
|
||||
end
|
||||
|
||||
if test -n "$nvmrc_file"
|
||||
set -l node_version (nvm_extract_version "$nvmrc_file")
|
||||
|
||||
# Cache the file path
|
||||
set -g _nvm_auto_use_cached_file "$nvmrc_file"
|
||||
|
||||
# Validate node version format (basic semver or major version)
|
||||
if not string match -qr '^v?[0-9]+(\..*)?$' "$node_version"
|
||||
if not set -q _nvm_auto_use_silent
|
||||
echo "Invalid Node.js version format in $nvmrc_file: $node_version" >&2
|
||||
end
|
||||
return 1
|
||||
end
|
||||
|
||||
# Remove 'v' prefix if present
|
||||
set node_version (string replace -r '^v' '' "$node_version")
|
||||
|
||||
# Cache the version
|
||||
set -g _nvm_auto_use_cached_version "$node_version"
|
||||
|
||||
# Check the current version
|
||||
set -l current_version
|
||||
if command -q node
|
||||
set current_version (node -v 2>/dev/null | sed 's/v//')
|
||||
end
|
||||
|
||||
if test "$node_version" != "$current_version"
|
||||
if not set -q _nvm_auto_use_silent
|
||||
echo "Switching to Node.js v$node_version"
|
||||
end
|
||||
|
||||
# Send notification if enabled
|
||||
if not set -q _nvm_auto_use_silent
|
||||
nvm_notify "Switched to Node.js v$node_version"
|
||||
end
|
||||
|
||||
if not nvm_compat_use $manager $node_version 2>/dev/null
|
||||
# Check if auto-install is disabled
|
||||
if set -q _nvm_auto_use_no_install
|
||||
if not set -q _nvm_auto_use_silent
|
||||
echo "Node.js version $node_version not found (auto-install disabled)" >&2
|
||||
end
|
||||
return 1
|
||||
end
|
||||
|
||||
if not set -q _nvm_auto_use_silent
|
||||
echo "Node.js version $node_version not found, installing..."
|
||||
end
|
||||
if nvm_compat_install $manager $node_version >/dev/null 2>&1
|
||||
nvm_compat_use $manager $node_version >/dev/null 2>&1
|
||||
if not set -q _nvm_auto_use_silent
|
||||
echo "Installed and switched to Node.js v$node_version"
|
||||
end
|
||||
else
|
||||
if not set -q _nvm_auto_use_silent
|
||||
echo "Failed to install Node.js version $node_version" >&2
|
||||
end
|
||||
return 1
|
||||
end
|
||||
end
|
||||
|
||||
# Update cached version after successful switch
|
||||
set -g _nvm_auto_use_cached_version "$node_version"
|
||||
|
||||
# Update NODE_VERSION environment variable
|
||||
set -gx NODE_VERSION "$node_version"
|
||||
end
|
||||
else
|
||||
# Clear cache if no .nvmrc found
|
||||
set -e _nvm_auto_use_cached_file
|
||||
set -e _nvm_auto_use_cached_version
|
||||
end
|
||||
end
|
||||
|
||||
100
functions/nvm_auto_use_config.fish
Normal file
100
functions/nvm_auto_use_config.fish
Normal file
@@ -0,0 +1,100 @@
|
||||
function nvm_auto_use_config -d "Configure nvm-auto-use settings"
|
||||
if test (count $argv) -eq 0
|
||||
echo "nvm-auto-use configuration:"
|
||||
echo " auto_install: "(test -n "$_nvm_auto_use_no_install"; and echo "disabled"; or echo "enabled")
|
||||
echo " silent: "(test -n "$_nvm_auto_use_silent"; and echo "enabled"; or echo "disabled")
|
||||
echo " debounce_ms: "(_nvm_auto_use_get_debounce)
|
||||
echo " excluded_dirs: "(_nvm_auto_use_get_excluded_dirs)
|
||||
echo " preferred_manager: "(test -n "$_nvm_auto_use_preferred_manager"; and echo "$_nvm_auto_use_preferred_manager"; or echo "auto-detect")
|
||||
return
|
||||
end
|
||||
|
||||
switch $argv[1]
|
||||
case auto_install
|
||||
switch $argv[2]
|
||||
case on enable true 1
|
||||
set -e _nvm_auto_use_no_install
|
||||
echo "Auto-install enabled"
|
||||
case off disable false 0
|
||||
set -g _nvm_auto_use_no_install 1
|
||||
echo "Auto-install disabled"
|
||||
case '*'
|
||||
echo "Usage: nvm_auto_use_config auto_install [on|off]"
|
||||
end
|
||||
case silent
|
||||
switch $argv[2]
|
||||
case on enable true 1
|
||||
set -g _nvm_auto_use_silent 1
|
||||
echo "Silent mode enabled"
|
||||
case off disable false 0
|
||||
set -e _nvm_auto_use_silent
|
||||
echo "Silent mode disabled"
|
||||
case '*'
|
||||
echo "Usage: nvm_auto_use_config silent [on|off]"
|
||||
end
|
||||
case debounce
|
||||
if test -n "$argv[2]" -a (string match -r '^[0-9]+$' "$argv[2]")
|
||||
set -g _nvm_auto_use_debounce_ms $argv[2]
|
||||
echo "Debounce set to $argv[2]ms"
|
||||
else
|
||||
echo "Usage: nvm_auto_use_config debounce <milliseconds>"
|
||||
end
|
||||
case exclude
|
||||
if test -n "$argv[2]"
|
||||
set -g _nvm_auto_use_excluded_dirs $_nvm_auto_use_excluded_dirs $argv[2]
|
||||
echo "Added $argv[2] to excluded directories"
|
||||
else
|
||||
echo "Usage: nvm_auto_use_config exclude <directory_pattern>"
|
||||
end
|
||||
case include
|
||||
if test -n "$argv[2]"
|
||||
set -l index (contains -i "$argv[2]" $_nvm_auto_use_excluded_dirs)
|
||||
if test -n "$index"
|
||||
set -e _nvm_auto_use_excluded_dirs[$index]
|
||||
echo "Removed $argv[2] from excluded directories"
|
||||
else
|
||||
echo "$argv[2] was not in excluded directories"
|
||||
end
|
||||
else
|
||||
echo "Usage: nvm_auto_use_config include <directory_pattern>"
|
||||
end
|
||||
case manager
|
||||
if test -n "$argv[2]"
|
||||
if contains "$argv[2]" nvm fnm volta asdf
|
||||
set -g _nvm_auto_use_preferred_manager "$argv[2]"
|
||||
echo "Preferred manager set to $argv[2]"
|
||||
else
|
||||
echo "Unsupported manager. Supported: nvm, fnm, volta, asdf"
|
||||
end
|
||||
else
|
||||
set -e _nvm_auto_use_preferred_manager
|
||||
echo "Reset to auto-detect manager"
|
||||
end
|
||||
case reset
|
||||
set -e _nvm_auto_use_no_install
|
||||
set -e _nvm_auto_use_silent
|
||||
set -e _nvm_auto_use_debounce_ms
|
||||
set -e _nvm_auto_use_excluded_dirs
|
||||
set -e _nvm_auto_use_preferred_manager
|
||||
echo "Configuration reset to defaults"
|
||||
case '*'
|
||||
echo "Usage: nvm_auto_use_config [auto_install|silent|debounce|exclude|include|manager|reset] [value]"
|
||||
return 1
|
||||
end
|
||||
end
|
||||
|
||||
function _nvm_auto_use_get_debounce
|
||||
if test -n "$_nvm_auto_use_debounce_ms"
|
||||
echo "$_nvm_auto_use_debounce_ms"
|
||||
else
|
||||
echo 500
|
||||
end
|
||||
end
|
||||
|
||||
function _nvm_auto_use_get_excluded_dirs
|
||||
if test -n "$_nvm_auto_use_excluded_dirs"
|
||||
string join ', ' $_nvm_auto_use_excluded_dirs
|
||||
else
|
||||
echo "node_modules, .git"
|
||||
end
|
||||
end
|
||||
22
functions/nvm_auto_use_silent.fish
Normal file
22
functions/nvm_auto_use_silent.fish
Normal file
@@ -0,0 +1,22 @@
|
||||
function nvm_auto_use_silent -d "Enable or disable silent mode for nvm-auto-use"
|
||||
if test (count $argv) -eq 0
|
||||
if set -q _nvm_auto_use_silent
|
||||
echo "Silent mode: enabled"
|
||||
else
|
||||
echo "Silent mode: disabled"
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
switch $argv[1]
|
||||
case on enable true 1
|
||||
set -g _nvm_auto_use_silent 1
|
||||
echo "Silent mode enabled"
|
||||
case off disable false 0
|
||||
set -e _nvm_auto_use_silent
|
||||
echo "Silent mode disabled"
|
||||
case '*'
|
||||
echo "Usage: nvm_auto_use_silent [on|off]"
|
||||
return 1
|
||||
end
|
||||
end
|
||||
71
functions/nvm_compat_detect.fish
Normal file
71
functions/nvm_compat_detect.fish
Normal file
@@ -0,0 +1,71 @@
|
||||
function nvm_compat_detect -d "Detect available Node.js version managers"
|
||||
set -l managers
|
||||
|
||||
if command -q nvm
|
||||
set managers $managers nvm
|
||||
end
|
||||
|
||||
if command -q fnm
|
||||
set managers $managers fnm
|
||||
end
|
||||
|
||||
if command -q volta
|
||||
set managers $managers volta
|
||||
end
|
||||
|
||||
if command -q asdf; and test -f ~/.tool-versions
|
||||
if grep -q nodejs ~/.tool-versions 2>/dev/null
|
||||
set managers $managers asdf
|
||||
end
|
||||
end
|
||||
|
||||
if test (count $managers) -eq 0
|
||||
echo "No supported Node.js version managers found"
|
||||
return 1
|
||||
end
|
||||
|
||||
echo "Available managers:" (string join ", " $managers)
|
||||
echo $managers
|
||||
end
|
||||
|
||||
function nvm_compat_use -a manager version -d "Use specified version with detected manager"
|
||||
if test -z "$manager" -o -z "$version"
|
||||
echo "Usage: nvm_compat_use <manager> <version>"
|
||||
return 1
|
||||
end
|
||||
|
||||
switch $manager
|
||||
case nvm
|
||||
nvm use $version
|
||||
case fnm
|
||||
fnm use $version
|
||||
case volta
|
||||
volta pin node@$version
|
||||
case asdf
|
||||
asdf local nodejs $version
|
||||
case '*'
|
||||
echo "Unsupported manager: $manager"
|
||||
return 1
|
||||
end
|
||||
end
|
||||
|
||||
function nvm_compat_install -a manager version -d "Install specified version with detected manager"
|
||||
if test -z "$manager" -o -z "$version"
|
||||
echo "Usage: nvm_compat_install <manager> <version>"
|
||||
return 1
|
||||
end
|
||||
|
||||
switch $manager
|
||||
case nvm
|
||||
nvm install $version
|
||||
case fnm
|
||||
fnm install $version
|
||||
case volta
|
||||
volta install node@$version
|
||||
case asdf
|
||||
asdf install nodejs $version
|
||||
case '*'
|
||||
echo "Unsupported manager: $manager"
|
||||
return 1
|
||||
end
|
||||
end
|
||||
51
functions/nvm_extract_version.fish
Normal file
51
functions/nvm_extract_version.fish
Normal file
@@ -0,0 +1,51 @@
|
||||
function nvm_extract_version -a file_path -d "Extract Node.js version from various file formats"
|
||||
if test -z "$file_path"
|
||||
return 1
|
||||
end
|
||||
|
||||
set -l file_name (basename "$file_path")
|
||||
set -l version_info (string split ':' "$file_path")
|
||||
set -l actual_file $version_info[1]
|
||||
set -l format $version_info[2]
|
||||
|
||||
if not test -f "$actual_file" -a -r "$actual_file"
|
||||
return 1
|
||||
end
|
||||
|
||||
switch "$format"
|
||||
case engines.node
|
||||
# Extract from package.json engines.node field
|
||||
if command -q jq
|
||||
set -l version (jq -r '.engines.node // empty' "$actual_file" 2>/dev/null)
|
||||
if test -n "$version" -a "$version" != null
|
||||
# Handle version ranges - extract first valid version
|
||||
set version (string replace -r '^[^0-9]*([0-9]+\.?[0-9]*\.?[0-9]*).*' '$1' "$version")
|
||||
echo "$version"
|
||||
return 0
|
||||
end
|
||||
end
|
||||
case nodejs
|
||||
# Extract from .tool-versions nodejs line
|
||||
set -l version (grep '^nodejs ' "$actual_file" | cut -d' ' -f2 | string trim)
|
||||
if test -n "$version"
|
||||
echo "$version"
|
||||
return 0
|
||||
end
|
||||
case '*'
|
||||
# Standard .nvmrc or .node-version file
|
||||
set -l version (cat "$actual_file" | string trim)
|
||||
if test -n "$version"
|
||||
# Handle nvm aliases
|
||||
switch "$version"
|
||||
case 'lts/*' lts latest stable node
|
||||
if command -q nvm
|
||||
set version (nvm version-remote "$version" 2>/dev/null | string replace 'v' '')
|
||||
end
|
||||
end
|
||||
echo "$version"
|
||||
return 0
|
||||
end
|
||||
end
|
||||
|
||||
return 1
|
||||
end
|
||||
@@ -1,11 +1,40 @@
|
||||
function nvm_find_nvmrc
|
||||
set -l dir (pwd)
|
||||
while test "$dir" != "/"
|
||||
if test -f "$dir/.nvmrc"
|
||||
echo "$dir/.nvmrc"
|
||||
return
|
||||
end
|
||||
set dir (dirname "$dir")
|
||||
end
|
||||
end
|
||||
set -l dir (pwd)
|
||||
|
||||
# Validate current directory
|
||||
if test -z "$dir"
|
||||
return 1
|
||||
end
|
||||
|
||||
while test "$dir" != /
|
||||
# Check for multiple file formats in order of preference
|
||||
for file in .nvmrc .node-version .tool-versions package.json
|
||||
set -l file_path "$dir/$file"
|
||||
if test -f "$file_path" -a -r "$file_path"
|
||||
switch $file
|
||||
case package.json
|
||||
# Extract engines.node field from package.json
|
||||
if command -q jq
|
||||
set -l node_version (jq -r '.engines.node // empty' "$file_path" 2>/dev/null)
|
||||
if test -n "$node_version" -a "$node_version" != null
|
||||
echo "$file_path:engines.node"
|
||||
return 0
|
||||
end
|
||||
end
|
||||
case .tool-versions
|
||||
# Check if .tool-versions contains nodejs entry
|
||||
if grep -q '^nodejs ' "$file_path" 2>/dev/null
|
||||
echo "$file_path:nodejs"
|
||||
return 0
|
||||
end
|
||||
case '*'
|
||||
echo "$file_path"
|
||||
return 0
|
||||
end
|
||||
end
|
||||
end
|
||||
set dir (dirname "$dir")
|
||||
end
|
||||
|
||||
return 1
|
||||
end
|
||||
|
||||
32
functions/nvm_notify.fish
Normal file
32
functions/nvm_notify.fish
Normal file
@@ -0,0 +1,32 @@
|
||||
function nvm_notify -a message -d "Send notification for Node.js version changes"
|
||||
if test -z "$message"
|
||||
return 1
|
||||
end
|
||||
|
||||
# Check if notifications are enabled
|
||||
if set -q _nvm_auto_use_no_notifications
|
||||
return
|
||||
end
|
||||
|
||||
# Try different notification methods
|
||||
if command -q osascript # macOS
|
||||
osascript -e "display notification \"$message\" with title \"nvm-auto-use\""
|
||||
else if command -q notify-send # Linux
|
||||
notify-send nvm-auto-use "$message"
|
||||
else if command -q terminal-notifier # macOS alternative
|
||||
terminal-notifier -title nvm-auto-use -message "$message"
|
||||
end
|
||||
end
|
||||
|
||||
function nvm_project_detect -d "Check if current directory is a Node.js project"
|
||||
set -l dir (pwd)
|
||||
|
||||
while test "$dir" != /
|
||||
if test -f "$dir/package.json"
|
||||
return 0
|
||||
end
|
||||
set dir (dirname "$dir")
|
||||
end
|
||||
|
||||
return 1
|
||||
end
|
||||
26
functions/nvm_version_prompt.fish
Normal file
26
functions/nvm_version_prompt.fish
Normal file
@@ -0,0 +1,26 @@
|
||||
function nvm_version_prompt -d "Display current Node.js version for prompt integration"
|
||||
if command -q node
|
||||
set -l version (node -v 2>/dev/null | string replace 'v' '')
|
||||
if test -n "$version"
|
||||
echo "⬢ $version"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function nvm_version_status -d "Show detailed Node.js version status"
|
||||
if command -q node
|
||||
set -l version (node -v 2>/dev/null)
|
||||
set -l npm_version (npm -v 2>/dev/null)
|
||||
|
||||
echo "Node.js: $version"
|
||||
if test -n "$npm_version"
|
||||
echo "npm: v$npm_version"
|
||||
end
|
||||
|
||||
if set -q _nvm_auto_use_cached_file
|
||||
echo "Auto-use: $_nvm_auto_use_cached_file"
|
||||
end
|
||||
else
|
||||
echo "Node.js: not installed"
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user