javascript - Her dizinde bir .nvmrc dosyası olduğunda otomatik olarak `nvm use` çalıştır
Kabuğumu nasıl yapılandırabilirim, böylece bir dizinde .nvmrc dosyası olduğunda nvm use otomatik olarak çalışsın ve .nvmrc dosyası olmadığında en son sürümü veya global bir yapılandırmayı kullansın?
Cevap #1
Eğer zsh (z kabuğu) kullanıyorsanız:
.nvmrc dosyası olan bir dizinde otomatik olarak 'nvm use' çağırma
Bunu, içinde bulunduğunuz dizinin bir .nvmrc dosyası içerdiğinde ve nvm'ye hangi node'u kullanacağını söyleyen bir dize bulunduğunda nvm use komutunu otomatik olarak çağırmak için $HOME/.zshrc dosyanıza ekleyin:
place this after nvm initialization!
autoload -U add-zsh-hook
load-nvmrc() {
local node_version="$(nvm version)"
local nvmrc_path="$(nvm_find_nvmrc)"
if [ -n "$nvmrc_path" ]; then
local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")
if [ "$nvmrc_node_version" = "N/A" ]; then
nvm install
elif [ "$nvmrc_node_version" != "$node_version" ]; then
nvm use
fi
elif [ "$node_version" != "$(nvm version default)" ]; then
echo "Reverting to nvm default version"
nvm use default
fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc
Daha fazla bilgi: https://github.com/creationix/nvm#zsh
Cevap #2
Eğer bash kullanıyorsanız, bunu ~/.bashrc dosyanıza ekleyebilirsiniz:
_nvmrc_hook() {
if [[ $PWD == $PREV_PWD ]]; then
return
fi
PREV_PWD=$PWD
[[ -f ".nvmrc" ]] && nvm use
}
if ! [[ "${PROMPT_COMMAND:-}" =~ _nvmrc_hook ]]; then
PROMPT_COMMAND="_nvmrc_hook${PROMPT_COMMAND:+;$PROMPT_COMMAND}"
fi
Cevap #3
@devius'tan mükemmel cevap.
Bunu, .nvmrc bulunan bir dizinden bulunmayan bir dizine geçerken varsayılan sürüme geri dönebilmesi için uzattım.
~/.bashrc:
#
Run 'nvm use' automatically every time there's
a .nvmrc file in the directory. Also, revert to default
version when entering a directory without .nvmrc
#
enter_directory() {
if [[ $PWD == $PREV_PWD ]]; then
return
fi
PREV_PWD=$PWD
if [[ -f ".nvmrc" ]]; then
nvm use
NVM_DIRTY=true
elif [[ $NVM_DIRTY = true ]]; then
nvm use default
NVM_DIRTY=false
fi
}
export PROMPT_COMMAND="$PROMPT_COMMAND; enter_directory"
@doug-barbieri'nin önerisini takiben, aşağıdaki betik, mevcut dizinde bir .nvmrc dosyası yoksa ancak üst alt dizinde varsa node'u varsayılan sürüme geri değiştirmeyecektir.
~/.bashrc:
enter_directory() {
if [[ $PWD == $PREV_PWD ]]; then
return
fi
if [[ "$PWD" =~ "$PREV_PWD" && ! -f ".nvmrc" ]]; then
return
fi
PREV_PWD=$PWD
if [[ -f ".nvmrc" ]]; then
nvm use
NVM_DIRTY=true
elif [[ $NVM_DIRTY = true ]]; then
nvm use default
NVM_DIRTY=false
fi
}
İşte hile burada:
if [[ "$PWD" =~ "$PREV_PWD" && ! -f ".nvmrc" ]]; then
return
fi
PWD'nin PREV_PWD'yi içerip içermediğini kontrol eder. Örneğin, /home/user1/a/b /home/user1/a içeriyorsa.
Bu, starship_precmd_user_func kullanarak Starship ile (hatta sadece WSL değil Windows için Git Bash üzerinde) çalışacak şekilde genişletilebilir
set_win_title() {
BASEPWD=$(basename "$PWD")
echo -ne "\033]0; 📁 $BASEPWD \a" < /dev/null
if [[ $PWD == $PREV_PWD ]]; then
return
fi
if [[ "$PWD" =~ "$PREV_PWD" && ! -f ".nvmrc" ]]; then
return
fi
PREV_PWD=$PWD
if [[ -f ".nvmrc" ]]; then
nvm use
NVM_DIRTY=true
elif [[ $NVM_DIRTY = true ]]; then
nvm use default
NVM_DIRTY=false
fi
}
starship_precmd_user_func="set_win_title"
eval "$(starship init bash)"
Cevap #4
Node.js için Otomatik Versiyon Değiştirme hakkında yeni öğrendim https://github.com/wbyoung/avn, bunu kullanabilirsin.
npm install -g avn avn-nvm avn-n
avn setup
Ayrıca bu konuyu da takip edebilirsiniz https://github.com/creationix/nvm/issues/110
Cevap #5
Bu cevap resmî nvm dokümantasyonundan alınmıştır.
Aşağıdakileri $HOME/.bashrc dosyanızın sonuna ekleyin:
find-up () {
path=$(pwd)
while [[ "$path" != "" && ! -e "$path/$1" ]]; do
path=${path%/*}
done
echo "$path"
}
cdnvm(){
cd "$@";
nvm_path=$(find-up .nvmrc | tr -d '[:space:]')
# If there are no .nvmrc file, use the default nvm version
if [[ ! $nvm_path = *[^[:space:]]* ]]; then
declare default_version;
default_version=$(nvm version default);
# If there is no default version, set it to `node`
# This will use the latest version on your machine
if [[ $default_version == "N/A" ]]; then
nvm alias default node;
default_version=$(nvm version default);
fi
# If the current version is not the default version, set it to use the default version
if [[ $(nvm current) != "$default_version" ]]; then
nvm use default;
fi
elif [[ -s $nvm_path/.nvmrc && -r $nvm_path/.nvmrc ]]; then
declare nvm_version
nvm_version=$(<"$nvm_path"/.nvmrc)
# Add the `v` suffix if it does not exists in the .nvmrc file
if [[ $nvm_version != v* ]]; then
nvm_version="v""$nvm_version"
fi
# If it is not already installed, install it
if [[ $(nvm ls "$nvm_version" | tr -d '[:space:]') == "N/A" ]]; then
nvm install "$nvm_version";
fi
if [[ $(nvm current) != "$nvm_version" ]]; then
nvm use "$nvm_version";
fi
fi
}
alias cd='cdnvm'
Bu, ... üzerine bir gelişmedir:
- @Gabo Esquivel'in cevabı - çünkü başka bir araca (
avn) geçmek zorunda kalmayacaksınız - @devius ve @Adriano P’nin cevapları - bir projenin içinde olduğunuz durumlarla ilgilenmiyor
Bu takma ad, bir .nvmrc dosyasını tespit etmek için mevcut dizininizden 'yukarı' doğru arama yapacaktır. Eğer bulursa, o sürüme geçecektir; bulamazsa, varsayılan sürümü kullanacaktır.
Cevap #6
Bunun için birçok çözüm denedim ve hiçbir şey istediğim gibi çalışmadı, bu yüzden kendi çözümümü yazdım:
Doğru Node sürümüne otomatik geçiş yapmak için ZSH fonksiyonu
Bildiğim kadarıyla, bu, aşağıdaki tüm kriterleri karşılayan tek olandır:
- dizin ağacında en yakın
.nvmrc'yi bulmak için arama yaparak her zaman doğru sürümde olmanızı garanti eder (tıpkınvm usegibi); - herhangi bir geçerli
.nvmrcformatını işleyebilir; - yüklü bir sürüm
.nvmrc'yi karşılamıyorsa açıkça sizi uyarır, - herhangi bir
.nvmrcdosyası ağaçta yukarıda yoksadefaultistiyorsunuz varsayar; - tamamen sessiz ve hızlıdır eğer zaten doğru Node sürümündeyseniz.
Cevap #7
Eğer fish kabuğu ile yüzüyorsanız, dizinde bir .nvmrc dosyası olduğunda nvm use komutunu nasıl çalıştıracağınız şöyle:
TODO: save this as `$HOME/.config/fish/conf.d/use_nvmrc.fish`
HOW IT WORKS
`nvm use` whenever .nvmrc is present in $PWD when using fish shell
when traveling deeper, use the parent .nvmrc unless otherwise set
also go back to default nvm when leaving the nvmrc-specified zone
function set_nvm --on-event fish_prompt
# runs whenever the fish_prompt event occurs
# if the current directory hasn't changed, do nothing
string match -q $PWD $PREV_PWD; and return 1
# if the current directory is within the previous one where we found an nvmrc
# and there is no subsequent .nvmrc here, do nothing, we are in the same repo
string match -eq $PREV_PWD $PWD; and not test -e '.nvmrc'; and return 1
# if we clear those checks, keep track of where we are
set -g PREV_PWD $PWD
if test -e '.nvmrc'
# if we find .nvmrc, run nvm use
nvm use
# and remember that we used that node
set NVM_DIRTY true
else if not string match $NVM_DIRTY true
# if we have set nvm and have stepped out of that repo
# go back to default node, if not already on it
not string match -eq (nvm current) (nvm alias default); and nvm use default
# and clear the flag
set NVM_DIRTY
end
end
