mirror of
https://github.com/ivuorinen/dotfiles.git
synced 2026-02-15 01:53:22 +00:00
feat(config): use nvm with bass, simplify setup
Signed-off-by: Ismo Vuorinen <ismo@ivuorinen.net>
This commit is contained in:
@@ -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
|
||||
16
config/fish/functions/load_nvm.fish
Normal file
16
config/fish/functions/load_nvm.fish
Normal file
@@ -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
|
||||
@@ -1,3 +0,0 @@
|
||||
function node -d "Server-side JavaScript runtime" -w node
|
||||
__nvm_run "node" $argv
|
||||
end
|
||||
@@ -1,3 +0,0 @@
|
||||
function npm -d "node package manager" -w npm
|
||||
__nvm_run "npm" $argv
|
||||
end
|
||||
@@ -1,3 +0,0 @@
|
||||
function npx -d "execute npm package binaries" -w npx
|
||||
__nvm_run "npx" $argv
|
||||
end
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
3
config/fish/functions/nvm_find_nvmrc.fish
Normal file
3
config/fish/functions/nvm_find_nvmrc.fish
Normal file
@@ -0,0 +1,3 @@
|
||||
function nvm_find_nvmrc
|
||||
bass source $NVM_DIR/nvm.sh --no-use ';' nvm_find_nvmrc
|
||||
end
|
||||
@@ -1,3 +0,0 @@
|
||||
function yarn -d "yarn package manager" -w yarn
|
||||
__nvm_run "yarn" $argv
|
||||
end
|
||||
Reference in New Issue
Block a user