Skip to content

Commit

Permalink
Fix issues with nvmrc locating
Browse files Browse the repository at this point in the history
  • Loading branch information
abejfehr committed Feb 13, 2022
1 parent a8ac788 commit 7bd1428
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
bin
.DS_Store
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
}

Expand Down
18 changes: 12 additions & 6 deletions resolve_node_version.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 7bd1428

Please sign in to comment.