From 7bd1428f58fd497eeb5f68e2c35629ead816b330 Mon Sep 17 00:00:00 2001 From: Abe Fehr Date: Sun, 13 Feb 2022 14:53:09 -0500 Subject: [PATCH] Fix issues with nvmrc locating --- .gitignore | 1 + README.md | 3 ++- resolve_node_version.ts | 18 ++++++++++++------ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index ba077a4..6fefe6c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ bin +.DS_Store diff --git a/README.md b/README.md index 8c9ed5e..353a78b 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ If you already have nvm installed, remove anything nvm-related you might already Next, run the following script to download the `resolve_node_version` binary to your machine: ```sh -curl -O --output-dir $HOME/.nvm URL_ONCE_I_HAVE_IT +(cd $HOME/.nvm/ && curl -L -O https://github.com/abejfehr/fast-nvm-switcher/releases/download/0.1.1/resolve_node_version) ``` Add the following lines to your .zshrc @@ -29,6 +29,7 @@ nvm() { # Resolves node version based on nearest nvmrc and adds its directory to the PATH load-nvmrc() { NODE_PATH=$(${NVM_DIR}/resolve_node_version) + echo "Updating node location to $NODE_PATH" PATH="$PATH:$NODE_PATH" } diff --git a/resolve_node_version.ts b/resolve_node_version.ts index 2d1a622..456ae0c 100644 --- a/resolve_node_version.ts +++ b/resolve_node_version.ts @@ -1,11 +1,17 @@ -import { dirname, fromFileUrl, join } from "https://deno.land/std/path/mod.ts"; +import { dirname, join } from "https://deno.land/std/path/mod.ts"; import { existsSync } from "https://deno.land/std/fs/mod.ts"; import * as semver from "https://deno.land/x/semver/mod.ts"; // this program's whole purpose is to just output to stdout the path to the node version to the shell can put it on the path -const getNpmrcPath = () => { - let pwd = dirname(fromFileUrl(import.meta.url)); +const getNvmrcPath = () => { + let pwd = Deno.cwd(); + + let nvmrcPath = join(pwd, '.nvmrc'); + + if (existsSync(nvmrcPath)) { + return nvmrcPath; + } while (pwd !== '/') { pwd = dirname(pwd); // goes up one @@ -40,12 +46,12 @@ const getNodePath = async (version: string) => { } const main = async () => { - const npmrcPath = getNpmrcPath(); + const nvmrcPath = getNvmrcPath(); let version: string | undefined; - if (npmrcPath) { - version = await Deno.readTextFile(npmrcPath); + if (nvmrcPath) { + version = await Deno.readTextFile(nvmrcPath); } if (!version) {