mirror of
https://github.com/ivuorinen/dotfiles.git
synced 2026-02-17 23:54:35 +00:00
chore: fish: migrate back to nvm
This commit is contained in:
70
config/fish/functions/__nvm_run.fish
Normal file
70
config/fish/functions/__nvm_run.fish
Normal 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
|
||||
3
config/fish/functions/node.fish
Normal file
3
config/fish/functions/node.fish
Normal file
@@ -0,0 +1,3 @@
|
||||
function node -d "Server-side JavaScript runtime" -w node
|
||||
__nvm_run "node" $argv
|
||||
end
|
||||
3
config/fish/functions/npm.fish
Normal file
3
config/fish/functions/npm.fish
Normal file
@@ -0,0 +1,3 @@
|
||||
function npm -d "node package manager" -w npm
|
||||
__nvm_run "npm" $argv
|
||||
end
|
||||
3
config/fish/functions/npx.fish
Normal file
3
config/fish/functions/npx.fish
Normal file
@@ -0,0 +1,3 @@
|
||||
function npx -d "execute npm package binaries" -w npx
|
||||
__nvm_run "npx" $argv
|
||||
end
|
||||
24
config/fish/functions/nvm.fish
Normal file
24
config/fish/functions/nvm.fish
Normal 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
|
||||
49
config/fish/functions/nvm_alias_command.fish
Normal file
49
config/fish/functions/nvm_alias_command.fish
Normal 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
|
||||
28
config/fish/functions/nvm_alias_function.fish
Normal file
28
config/fish/functions/nvm_alias_function.fish
Normal 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
|
||||
3
config/fish/functions/yarn.fish
Normal file
3
config/fish/functions/yarn.fish
Normal file
@@ -0,0 +1,3 @@
|
||||
function yarn -d "yarn package manager" -w yarn
|
||||
__nvm_run "yarn" $argv
|
||||
end
|
||||
Reference in New Issue
Block a user