From 6ffe5813262f727687848573585facee7adfdb33 Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Fri, 4 Apr 2025 16:33:32 +0300 Subject: [PATCH] feat(config): use nvm with bass, simplify setup Signed-off-by: Ismo Vuorinen --- config/fish/completions/nvm.fish | 63 ----------------- config/fish/config.fish | 1 + config/fish/fish_plugins | 2 - config/fish/functions/__nvm_run.fish | 70 ------------------- config/fish/functions/load_nvm.fish | 16 +++++ 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/nvm_find_nvmrc.fish | 3 + config/fish/functions/yarn.fish | 3 - 13 files changed, 22 insertions(+), 246 deletions(-) delete mode 100644 config/fish/completions/nvm.fish delete mode 100644 config/fish/functions/__nvm_run.fish create mode 100644 config/fish/functions/load_nvm.fish delete mode 100644 config/fish/functions/node.fish delete mode 100644 config/fish/functions/npm.fish delete mode 100644 config/fish/functions/npx.fish delete mode 100644 config/fish/functions/nvm_alias_command.fish delete mode 100644 config/fish/functions/nvm_alias_function.fish create mode 100644 config/fish/functions/nvm_find_nvmrc.fish delete mode 100644 config/fish/functions/yarn.fish diff --git a/config/fish/completions/nvm.fish b/config/fish/completions/nvm.fish deleted file mode 100644 index 053c7a2..0000000 --- a/config/fish/completions/nvm.fish +++ /dev/null @@ -1,63 +0,0 @@ -# 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/config.fish b/config/fish/config.fish index ada4c27..ed3a597 100644 --- a/config/fish/config.fish +++ b/config/fish/config.fish @@ -22,4 +22,5 @@ type -q rbenv; and source (rbenv init -|psub) type -q pyenv; and source (pyenv init -|psub) type -q goenv; and source (goenv init -|psub) type -q fnm; and fnm env --use-on-cd --shell fish | source +type -q load_nvm; and load_nvm > /dev/stderr diff --git a/config/fish/fish_plugins b/config/fish/fish_plugins index 1e3398b..17c02e8 100644 --- a/config/fish/fish_plugins +++ b/config/fish/fish_plugins @@ -6,5 +6,3 @@ 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 deleted file mode 100644 index 39e0dfd..0000000 --- a/config/fish/functions/__nvm_run.fish +++ /dev/null @@ -1,70 +0,0 @@ -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/load_nvm.fish b/config/fish/functions/load_nvm.fish new file mode 100644 index 0000000..55ed127 --- /dev/null +++ b/config/fish/functions/load_nvm.fish @@ -0,0 +1,16 @@ +function load_nvm --on-variable="PWD" + set -l default_node_version (nvm version default) + set -l node_version (nvm version) + set -l nvmrc_path (nvm_find_nvmrc) + if test -n "$nvmrc_path" + set -l nvmrc_node_version (nvm version (cat $nvmrc_path)) + if test "$nvmrc_node_version" = "N/A" + nvm install (cat $nvmrc_path) + else if test "$nvmrc_node_version" != "$node_version" + nvm use $nvmrc_node_version + end + else if test "$node_version" != "$default_node_version" + echo "Reverting to default Node version" + nvm use default + end +end diff --git a/config/fish/functions/node.fish b/config/fish/functions/node.fish deleted file mode 100644 index b95dd43..0000000 --- a/config/fish/functions/node.fish +++ /dev/null @@ -1,3 +0,0 @@ -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 deleted file mode 100644 index 94b2939..0000000 --- a/config/fish/functions/npm.fish +++ /dev/null @@ -1,3 +0,0 @@ -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 deleted file mode 100644 index 533c43a..0000000 --- a/config/fish/functions/npx.fish +++ /dev/null @@ -1,3 +0,0 @@ -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 index ec2397b..575d0e3 100644 --- a/config/fish/functions/nvm.fish +++ b/config/fish/functions/nvm.fish @@ -1,24 +1,4 @@ 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 + bass source $NVM_DIR/nvm.sh --no-use ';' nvm $argv end + diff --git a/config/fish/functions/nvm_alias_command.fish b/config/fish/functions/nvm_alias_command.fish deleted file mode 100644 index f7c0fd2..0000000 --- a/config/fish/functions/nvm_alias_command.fish +++ /dev/null @@ -1,49 +0,0 @@ -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 deleted file mode 100644 index 770caa7..0000000 --- a/config/fish/functions/nvm_alias_function.fish +++ /dev/null @@ -1,28 +0,0 @@ -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/nvm_find_nvmrc.fish b/config/fish/functions/nvm_find_nvmrc.fish new file mode 100644 index 0000000..359c1f9 --- /dev/null +++ b/config/fish/functions/nvm_find_nvmrc.fish @@ -0,0 +1,3 @@ +function nvm_find_nvmrc + bass source $NVM_DIR/nvm.sh --no-use ';' nvm_find_nvmrc +end diff --git a/config/fish/functions/yarn.fish b/config/fish/functions/yarn.fish deleted file mode 100644 index c7f59c2..0000000 --- a/config/fish/functions/yarn.fish +++ /dev/null @@ -1,3 +0,0 @@ -function yarn -d "yarn package manager" -w yarn - __nvm_run "yarn" $argv -end