mirror of
https://github.com/ivuorinen/nvm-auto-use.fish.git
synced 2026-02-18 15:52:14 +00:00
feat: refactor plugin architecture, enhance linting, CI & tooling
- Major refactor of core Fish functions for modularity, caching, and error handling - Improved `.editorconfig` and Makefile for stricter formatting and linting standards - Expanded linting support: added EditorConfig checks, auto-install for missing tools, and Makefile targets - Enhanced CI workflow with clearer permissions and job steps in GitHub Actions - Updated documentation in `README.md` and `CLAUDE.md` to reflect new features, advanced developer tools, and contribution guidelines - Improved Node.js version manager detection, switching, and installation logic - Added/updated utility functions for configuration, silent mode, notifications, and version extraction - Various bug fixes, code quality improvements, and expanded test coverage
This commit is contained in:
@@ -1,130 +1,155 @@
|
||||
function nvm_auto_use --on-variable PWD
|
||||
# Detect available Node.js version manager
|
||||
set -l available_managers (nvm_compat_detect 2>/dev/null | tail -n 1)
|
||||
if test -z "$available_managers"
|
||||
# Select the Node.js version manager
|
||||
set -l manager (_nvm_auto_use_select_manager)
|
||||
if test -z "$manager"
|
||||
return
|
||||
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
|
||||
# Debounce rapid directory changes
|
||||
if _nvm_auto_use_should_debounce
|
||||
return
|
||||
end
|
||||
|
||||
if test -z "$manager"
|
||||
set manager $available_managers[1]
|
||||
# Check for excluded directories
|
||||
if _nvm_auto_use_is_excluded_dir
|
||||
return
|
||||
end
|
||||
|
||||
# Check if project detection is enabled and we're in a Node.js project
|
||||
# Project-only mode: only activate in Node.js projects
|
||||
if set -q _nvm_auto_use_project_only
|
||||
if not nvm_project_detect
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
# Export NODE_VERSION environment variable
|
||||
# Export NODE_VERSION environment variable if available
|
||||
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
|
||||
|
||||
# Find version file and its mtime
|
||||
set -l nvmrc_file (nvm_find_nvmrc)
|
||||
set -l nvmrc_mtime (_nvm_auto_use_get_mtime "$nvmrc_file")
|
||||
|
||||
# Cache check: if same file and version, skip processing
|
||||
if test "$nvmrc_file" = "$_nvm_auto_use_cached_file"
|
||||
# Skip if cache is valid
|
||||
if _nvm_auto_use_is_cache_valid "$nvmrc_file" "$nvmrc_mtime"
|
||||
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
|
||||
_nvm_auto_use_switch_version "$manager" "$nvmrc_file" "$nvmrc_mtime"
|
||||
else
|
||||
# Clear cache if no .nvmrc found
|
||||
set -e _nvm_auto_use_cached_file
|
||||
set -e _nvm_auto_use_cached_version
|
||||
_nvm_auto_use_clear_cache
|
||||
end
|
||||
end
|
||||
|
||||
function _nvm_auto_use_select_manager
|
||||
set -l available_managers (nvm_compat_detect 2>/dev/null)
|
||||
if test -z "$available_managers"
|
||||
return
|
||||
end
|
||||
if test -n "$_nvm_auto_use_preferred_manager"
|
||||
if contains "$_nvm_auto_use_preferred_manager" $available_managers
|
||||
echo "$_nvm_auto_use_preferred_manager"
|
||||
return
|
||||
end
|
||||
end
|
||||
echo $available_managers[1]
|
||||
end
|
||||
|
||||
function _nvm_auto_use_should_debounce
|
||||
set -l debounce_ms (_nvm_auto_use_get_debounce)
|
||||
set -l current_time (date +%s%3N 2>/dev/null; or math "(date +%s) * 1000")
|
||||
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 0
|
||||
end
|
||||
end
|
||||
set -g _nvm_auto_use_last_change $current_time
|
||||
return 1
|
||||
end
|
||||
|
||||
function _nvm_auto_use_is_excluded_dir
|
||||
set -l current_dir (pwd)
|
||||
set -l patterns $_nvm_auto_use_excluded_dirs node_modules .git
|
||||
for pattern in $patterns
|
||||
if string match -q "*/$pattern" "$current_dir"; or string match -q "*/$pattern/*" "$current_dir"
|
||||
return 0
|
||||
end
|
||||
end
|
||||
return 1
|
||||
end
|
||||
|
||||
function _nvm_auto_use_get_mtime
|
||||
set -l file $argv[1]
|
||||
if test -n "$file"
|
||||
stat -c %Y "$file" 2>/dev/null; or stat -f %m "$file" 2>/dev/null
|
||||
end
|
||||
end
|
||||
|
||||
function _nvm_auto_use_is_cache_valid
|
||||
set -l file $argv[1]
|
||||
set -l mtime $argv[2]
|
||||
if test "$file" = "$_nvm_auto_use_cached_file"; and test "$mtime" = "$_nvm_auto_use_cached_mtime"
|
||||
return 0
|
||||
end
|
||||
return 1
|
||||
end
|
||||
|
||||
function _nvm_auto_use_switch_version
|
||||
set -l manager $argv[1]
|
||||
set -l nvmrc_file $argv[2]
|
||||
set -l nvmrc_mtime $argv[3]
|
||||
set -l node_version (nvm_extract_version "$nvmrc_file")
|
||||
set -g _nvm_auto_use_cached_file "$nvmrc_file"
|
||||
set -g _nvm_auto_use_cached_mtime "$nvmrc_mtime"
|
||||
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
|
||||
set node_version (string replace -r '^v' '' "$node_version")
|
||||
set -g _nvm_auto_use_cached_version "$node_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"
|
||||
nvm_notify "Switched to Node.js v$node_version"
|
||||
end
|
||||
if not nvm_compat_use $manager $node_version 2>/dev/null
|
||||
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
|
||||
set -g _nvm_auto_use_cached_version "$node_version"
|
||||
set -gx NODE_VERSION "$node_version"
|
||||
end
|
||||
end
|
||||
|
||||
function _nvm_auto_use_clear_cache
|
||||
set -e _nvm_auto_use_cached_file
|
||||
set -e _nvm_auto_use_cached_version
|
||||
set -e _nvm_auto_use_cached_mtime
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user