-
-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Open
Description
Operating system and version: Linux with bash and perl interpreter
The bash snippet suggested at https://github.com/nvm-sh/nvm?tab=readme-ov-file#bash is tremendously slow - on every single 'cd' the user types, there is a delay before reaching the target dir, regardless of whether the same or a different version of node needs to be used, and this can be counter-productive and annoying.
This is why I wrote the following .bashrc snippet, which can replace the aforementioned. Nevertheless, it uses Perl (even though it's a .bashrc snippet). However, if you do have Perl installed, it is incredibly faster than the (currently) official one.
cdnvm() {
command cd "$@" || return $?
eval $(perl <<'END'
use File::Basename;
use Cwd;
use feature 'say';
my $dir = getcwd;
while (1) {
if (-f "$dir/.nvmrc") {
my $current_v = `node -v` =~ s/^v|\s+\z//gr;
open my $fh, '<', "$dir/.nvmrc" or die "Couldn't read $dir/.nvmrc: $!";
my $this_v = <$fh> =~ s/^v|\s+\z//gr;
if ($this_v ne $current_v) {
say 'nvm use';
}
last;
}
last if $dir eq '/';
$dir = dirname $dir;
}
END
)
}
alias cd='cdnvm'
cdnvm "$PWD" || exit
Metadata
Metadata
Assignees
Labels
No labels