From 8b0148e46883bda57e1168276696efc6d2c5cc77 Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Wed, 2 Apr 2025 18:22:38 +0300 Subject: [PATCH] chore: fish: migrate back to nvm --- config/fish/completions/nvm.fish | 63 +++++++++++++++++ config/fish/fish_plugins | 2 + config/fish/functions/__nvm_run.fish | 70 +++++++++++++++++++ config/fish/functions/node.fish | 3 + config/fish/functions/npm.fish | 3 + config/fish/functions/npx.fish | 3 + config/fish/functions/nvm.fish | 24 +++++++ config/fish/functions/nvm_alias_command.fish | 49 +++++++++++++ config/fish/functions/nvm_alias_function.fish | 28 ++++++++ config/fish/functions/yarn.fish | 3 + 10 files changed, 248 insertions(+) create mode 100644 config/fish/completions/nvm.fish create mode 100644 config/fish/functions/__nvm_run.fish create mode 100644 config/fish/functions/node.fish create mode 100644 config/fish/functions/npm.fish create mode 100644 config/fish/functions/npx.fish create mode 100644 config/fish/functions/nvm.fish create mode 100644 config/fish/functions/nvm_alias_command.fish create mode 100644 config/fish/functions/nvm_alias_function.fish create mode 100644 config/fish/functions/yarn.fish diff --git a/config/fish/completions/nvm.fish b/config/fish/completions/nvm.fish new file mode 100644 index 0000000..053c7a2 --- /dev/null +++ b/config/fish/completions/nvm.fish @@ -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 . 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 ' +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 . 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 on . 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 on . 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 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 ' +complete -f -c nvm -n '__fish_use_subcommand' -a 'version-remote' -d 'Resolve the given description to a single remote ' +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 or set an alias named pointing to ' +complete -f -c nvm -n '__fish_use_subcommand' -a 'unalias' -d 'Deletes the alias named ' +complete -f -c nvm -n '__fish_use_subcommand' -a 'unload' -d 'Unload `nvm` from shell' diff --git a/config/fish/fish_plugins b/config/fish/fish_plugins index 17c02e8..1e3398b 100644 --- a/config/fish/fish_plugins +++ b/config/fish/fish_plugins @@ -6,3 +6,5 @@ jgusta/paths danhper/fish-ssh-agent halostatue/fish-brew@v3 edc/bass +jorgebucaran/nvm.fish +fabioantunes/fish-nvm diff --git a/config/fish/functions/__nvm_run.fish b/config/fish/functions/__nvm_run.fish new file mode 100644 index 0000000..39e0dfd --- /dev/null +++ b/config/fish/functions/__nvm_run.fish @@ -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 diff --git a/config/fish/functions/node.fish b/config/fish/functions/node.fish new file mode 100644 index 0000000..b95dd43 --- /dev/null +++ b/config/fish/functions/node.fish @@ -0,0 +1,3 @@ +function node -d "Server-side JavaScript runtime" -w node + __nvm_run "node" $argv +end diff --git a/config/fish/functions/npm.fish b/config/fish/functions/npm.fish new file mode 100644 index 0000000..94b2939 --- /dev/null +++ b/config/fish/functions/npm.fish @@ -0,0 +1,3 @@ +function npm -d "node package manager" -w npm + __nvm_run "npm" $argv +end diff --git a/config/fish/functions/npx.fish b/config/fish/functions/npx.fish new file mode 100644 index 0000000..533c43a --- /dev/null +++ b/config/fish/functions/npx.fish @@ -0,0 +1,3 @@ +function npx -d "execute npm package binaries" -w npx + __nvm_run "npx" $argv +end diff --git a/config/fish/functions/nvm.fish b/config/fish/functions/nvm.fish new file mode 100644 index 0000000..ec2397b --- /dev/null +++ b/config/fish/functions/nvm.fish @@ -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 diff --git a/config/fish/functions/nvm_alias_command.fish b/config/fish/functions/nvm_alias_command.fish new file mode 100644 index 0000000..f7c0fd2 --- /dev/null +++ b/config/fish/functions/nvm_alias_command.fish @@ -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 diff --git a/config/fish/functions/nvm_alias_function.fish b/config/fish/functions/nvm_alias_function.fish new file mode 100644 index 0000000..770caa7 --- /dev/null +++ b/config/fish/functions/nvm_alias_function.fish @@ -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 diff --git a/config/fish/functions/yarn.fish b/config/fish/functions/yarn.fish new file mode 100644 index 0000000..c7f59c2 --- /dev/null +++ b/config/fish/functions/yarn.fish @@ -0,0 +1,3 @@ +function yarn -d "yarn package manager" -w yarn + __nvm_run "yarn" $argv +end