mirror of
https://github.com/ivuorinen/nvm-auto-use.fish.git
synced 2026-02-04 07:45:50 +00:00
- 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
56 lines
2.0 KiB
Fish
56 lines
2.0 KiB
Fish
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"
|
|
# Strip leading 'v'
|
|
set version (string replace -r '^v' '' "$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' '')
|
|
else if command -q node
|
|
set version (node -v 2>/dev/null | string replace -r '^v' '')
|
|
end
|
|
end
|
|
echo "$version"
|
|
return 0
|
|
end
|
|
end
|
|
|
|
return 1
|
|
end
|