chore: fish: migrate back to nvm

This commit is contained in:
2025-04-02 18:22:38 +03:00
parent 9cb27eb9dc
commit 8b0148e468
10 changed files with 248 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
# NVM (https://github.com/creationix/nvm) completions for Fish shell
# Inspired on (https://github.com/derekstavis/plugin-nvm)
function __nvm_complete_ls_remote
if not test "$__nvm_ls_remote"
set -g __nvm_ls_remote (nvm ls-remote | grep -Po '(?:iojs-)?v[0-9]+\.[0-9]+\.[0-9]+')
end
printf "%s\n" $__nvm_ls_remote
end
function __nvm_complete_ls
if not test "$__nvm_ls"
set -g __nvm_ls (nvm ls | grep -Po '[[:space:]].\K(v[0-9]+\.[0-9]+\.[0-9]+)')
end
printf "%s\n" $__nvm_ls
end
#Install
complete -f -c nvm -n '__fish_use_subcommand' -a 'install' -d 'Download and install a <version>. Uses .nvmrc if available'
complete -f -c nvm -n "__fish_seen_subcommand_from install" -a "(__nvm_complete_ls_remote)"
complete -f -c nvm -n "__fish_seen_subcommand_from install" -l reinstall-packages-from= -d 'When installing, reinstall packages installed in <node|iojs|node version number>'
complete -f -c nvm -n "__fish_seen_subcommand_from install" -s s -d 'From source'
#Use
complete -f -c nvm -n '__fish_use_subcommand' -a 'use' -d 'Modify PATH to use <version>. Uses .nvmrc if available'
complete -f -c nvm -n "__fish_seen_subcommand_from use" -a "(__nvm_complete_ls)"
complete -f -c nvm -n "__fish_seen_subcommand_from use" -l silent
#Exec
complete -f -c nvm -n '__fish_use_subcommand' -a 'exec' -d 'Run <command> on <version>. Uses .nvmrc if available'
complete -f -c nvm -n "__fish_seen_subcommand_from exec" -a "(__nvm_complete_ls)"
complete -f -c nvm -n "__fish_seen_subcommand_from exec" -l silent
#Run
complete -f -c nvm -n '__fish_use_subcommand' -a 'run' -d 'Run <command> on <version>. Uses .nvmrc if available'
complete -f -c nvm -n "__fish_seen_subcommand_from run" -a "(__nvm_complete_ls)"
complete -f -c nvm -n "__fish_seen_subcommand_from run" -l silent
#Uninstall
complete -f -c nvm -n '__fish_use_subcommand' -a 'uninstall' -d 'Uninstall a version'
complete -f -c nvm -n "__fish_seen_subcommand_from uninstall" -a "(__nvm_complete_ls)"
#Which
complete -f -c nvm -n '__fish_use_subcommand' -a 'which' -d 'Display path to installed node version. Uses .nvmrc if available'
complete -f -c nvm -n "__fish_seen_subcommand_from which" -a "(__nvm_complete_ls)"
#Reinstall-Packages
complete -f -c nvm -n '__fish_use_subcommand' -a 'reinstall-packages' -d 'Reinstall global `npm` packages contained in <version> to current version'
complete -f -c nvm -n "__fish_seen_subcommand_from reinstall-packages" -a "(__nvm_complete_ls)"
#Completions
complete -f -c nvm -n '__fish_use_subcommand' -a 'current' -d 'Display currently activated version'
complete -f -c nvm -n '__fish_use_subcommand' -a 'ls' -d 'List installed versions'
complete -f -c nvm -n '__fish_use_subcommand' -a 'ls-remote' -d 'List remote versions available for install'
complete -f -c nvm -n '__fish_use_subcommand' -a 'version' -d 'Resolve the given description to a single local <version>'
complete -f -c nvm -n '__fish_use_subcommand' -a 'version-remote' -d 'Resolve the given description to a single remote <version>'
complete -f -c nvm -n '__fish_use_subcommand' -a 'deactivate' -d 'Undo effects of `nvm` on current shell'
complete -f -c nvm -n '__fish_use_subcommand' -a 'alias' -d 'Show all aliases beginning with <pattern> or set an alias named <name> pointing to <version>'
complete -f -c nvm -n '__fish_use_subcommand' -a 'unalias' -d 'Deletes the alias named <name>'
complete -f -c nvm -n '__fish_use_subcommand' -a 'unload' -d 'Unload `nvm` from shell'

View File

@@ -6,3 +6,5 @@ jgusta/paths
danhper/fish-ssh-agent
halostatue/fish-brew@v3
edc/bass
jorgebucaran/nvm.fish
fabioantunes/fish-nvm

View File

@@ -0,0 +1,70 @@
function __nvm_run
set count (count $argv)
if test "$count" -le 0
echo 'No params'
return 1
end
if test (uname -s) = 'Darwin'; and string match -q "*versions/node/*/bin" $PATH
set -l nvm_node_path (string match "*versions/node/*/bin" $PATH)
set -l nvm_index (contains -i -- $nvm_node_path $PATH)
if test $nvm_index -gt 1
set -gx PATH $nvm_node_path (string match -v $nvm_node_path $PATH)
end
end
function run_command
set stack (status stack-trace | grep called | cut -d " " -f 7)
set count (count $argv)
if type -fq $argv[1]; and test "$stack[1]" != (type -fP $argv[1])
set count (count $argv)
if test "$count" -ge 2
set args $argv[2..-1]
# https://stackoverflow.com/questions/45237675/proxying-arguments-from-one-function-to-a-command/45238056#45238056
eval (string escape -- (type -fP $argv[1]) $args)
else
eval (string escape -- (type -fP $argv[1]))
end
else
echo (set_color -o)"Fish nvm:"(set_color normal) "'$argv[1]' is currently not installed, try running npm i -g $argv[1]"
return 1
end
end
function can_run_command
if type -P $argv[1] > /dev/null 2>&1; or type -P node > /dev/null 2>&1
return
else
return 1
end
end
function run_default
nvm use default > /dev/null
set -gx NVM_HAS_RUN 1
if can_run_command $argv[1]
run_command $argv
end
end
if not test -n "$NVM_HAS_RUN"
if test -f .nvmrc;
set nvm_output (nvm use)
set nvm_status $status
if test $nvm_status -gt 0
echo $nvm_output
end
if test $nvm_status -eq 0; and can_run_command $argv[1]
set -gx NVM_HAS_RUN 1
run_command $argv
end
else
run_default $argv
end
else
run_command $argv
end
end

View File

@@ -0,0 +1,3 @@
function node -d "Server-side JavaScript runtime" -w node
__nvm_run "node" $argv
end

View File

@@ -0,0 +1,3 @@
function npm -d "node package manager" -w npm
__nvm_run "npm" $argv
end

View File

@@ -0,0 +1,3 @@
function npx -d "execute npm package binaries" -w npx
__nvm_run "npx" $argv
end

View File

@@ -0,0 +1,24 @@
function nvm
if not type -q bass
echo 'Bass is not installed please install it running fisher edc/bass'
return
end
set -q NVM_DIR; or set -gx NVM_DIR ~/.nvm
set -q nvm_prefix; or set -gx nvm_prefix $NVM_DIR
bass source $nvm_prefix/nvm.sh --no-use ';' nvm $argv
set bstatus $status
if test $bstatus -gt 0
return $bstatus
end
if test (count $argv) -lt 1
return 0
end
if test $argv[1] = "use"; or test $argv[1] = "install"
set -gx NVM_HAS_RUN 1
end
end

View File

@@ -0,0 +1,49 @@
function nvm_alias_command -d "Create an alias command"
function __nvm_alias_output
if test -z "$nvm_alias_output"
echo "/usr/local/bin"
else
echo (string replace -r '/$' '' $nvm_alias_output)
end
end
function __create_alias_command
if test -e "$argv[1]"
set_color yellow
echo "Ignored: $argv[1] (already exists)"
set_color normal
return 0
else
set -l template "__nvm_run \"COMMAND\" \$argv"
printf "#!/usr/bin/env fish\n%s\n" (string replace COMMAND $argv[2] $template) > $argv[1]
if test $status -eq 0
printf "\U2705 %s alias command was created at %s\n" $argv[2] $argv[1]
return (chmod +x $argv[1])
else
printf "\U274C failed creating %s alias command at %s\n" $argv[2] $argv[1]
printf "Probably a permissions problem, try running sudo fish, and then nvm_alias_command\n"
end
end
end
set -l outputPath (__nvm_alias_output)
mkdir -p $outputPath
if test $status -ge 1
printf "\U274C failed creating dir $outputPath."
printf "Probably a permissions problem, try running sudo fish, and then nvm_alias_command\n"
exit 1
end
if test (count $argv) -le 0
set -l aliases node npm npx yarn
for alias in $aliases
__create_alias_command "$outputPath/$alias" $alias
end
else
for arg in $argv
__create_alias_command "$outputPath/$arg" $arg
end
end
end

View File

@@ -0,0 +1,28 @@
function nvm_alias_function -d "Create an alias function"
function __create_alias_function
if test -e "$argv[1]"
set_color yellow
echo "Ignored: $argv[1] (already exists)"
set_color normal
return 0
else
set -l line1 "function COMMAND -w COMMAND"
set -l line2 "__nvm_run \"COMMAND\" \$argv"
echo (string replace -a COMMAND $argv[2] $line1) > $argv[1]
echo (string replace COMMAND $argv[2] $line2) >> $argv[1]
echo "end" >> $argv[1]
return 0
end
end
if test (count $argv) -le 0
set_color yellow
echo "Please specify package(s) name(s)"
set_color normal
return 1
else
for arg in $argv
__create_alias_function "$fish_function_path[1]/$arg.fish" $argv
end
end
end

View File

@@ -0,0 +1,3 @@
function yarn -d "yarn package manager" -w yarn
__nvm_run "yarn" $argv
end