From 322d2b6c1fe1cf5d8f5ce86de242119a5043feec Mon Sep 17 00:00:00 2001 From: Gal Schlezinger Date: Thu, 5 Nov 2020 17:37:27 +0200 Subject: [PATCH] Run `fnm use` on shell initialization in Bash (#322) --- src/shell/bash.rs | 14 ++++++++------ src/shell/fish.rs | 4 +--- src/shell/zsh.rs | 4 +--- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/shell/bash.rs b/src/shell/bash.rs index 4ba5e43..ec1c517 100644 --- a/src/shell/bash.rs +++ b/src/shell/bash.rs @@ -21,17 +21,19 @@ impl Shell for Bash { fn use_on_cd(&self, _config: &crate::config::FnmConfig) -> String { indoc!( r#" - __fnmcd () { - cd "$@" - - if [[ -f .node-version && .node-version ]]; then - fnm use - elif [[ -f .nvmrc && .nvmrc ]]; then + __fnm_use_if_file_found() { + if [[ -f .node-version || -f .nvmrc ]]; then fnm use fi } + __fnmcd () { + cd "$@" || return $? + __fnm_use_if_file_found + } + alias cd=__fnmcd + __fnm_use_if_file_found "# ) .into() diff --git a/src/shell/fish.rs b/src/shell/fish.rs index 24886a0..a0a36ec 100644 --- a/src/shell/fish.rs +++ b/src/shell/fish.rs @@ -23,9 +23,7 @@ impl Shell for Fish { r#" function _fnm_autoload_hook --on-variable PWD --description 'Change Node version on directory change' status --is-command-substitution; and return - if test -f .node-version - fnm use - else if test -f .nvmrc + if test -f .node-version -o -f .nvmrc fnm use end end diff --git a/src/shell/zsh.rs b/src/shell/zsh.rs index 8a1b9bf..c79a89a 100644 --- a/src/shell/zsh.rs +++ b/src/shell/zsh.rs @@ -23,9 +23,7 @@ impl Shell for Zsh { r#" autoload -U add-zsh-hook _fnm_autoload_hook () { - if [[ -f .node-version && -r .node-version ]]; then - fnm use - elif [[ -f .nvmrc && -r .nvmrc ]]; then + if [[ -f .node-version || -f .nvmrc ]]; then fnm use fi }