Skip to content

Document opt-in auto-use recipe in README#253

Open
esimkowitz wants to merge 1 commit into
jorgebucaran:mainfrom
esimkowitz:readme-auto-use
Open

Document opt-in auto-use recipe in README#253
esimkowitz wants to merge 1 commit into
jorgebucaran:mainfrom
esimkowitz:readme-auto-use

Conversation

@esimkowitz

Copy link
Copy Markdown

Summary

Adds a README section showing how users can opt into automatic nvm use on cd, mirroring the recipe-style approach used in the upstream nvm-sh README.

The snippet:

  • Registers a --on-variable PWD handler so it only runs in interactive shells
  • Walks up from $PWD to find the nearest .nvmrc or .node-version
  • Calls nvm use --silent when a file is found
  • Caches the last activated file so it doesn't re-fire when you cd between subdirectories of the same project
  • Runs once at config load so freshly opened shells already have the right version

Alternative to #252

This is a documentation-only alternative to #252, which added a built-in nvm_auto_use variable. Keeping the behavior as a README recipe avoids growing the plugin's surface area and lets users tweak the function for their own preferences (e.g., falling back to a default version, integrating with other tools). I'll close #252 in favor of this approach.

Test plan

  • Sourced the snippet in an interactive fish shell and verified that cd-ing into a directory with a .nvmrc fires the hook
  • Verified subsequent cds within the same project are no-ops (cached file path)
  • Verified the hook does not fire in non-interactive shells (fish -c '...')

Adds a fish snippet that hooks --on-variable PWD to walk up from the
current directory for an .nvmrc/.node-version and call nvm use.
Mirrors the recipe-style approach used by the upstream nvm-sh README.
@jorgebucaran

Copy link
Copy Markdown
Owner

This function is a bit too niche. We should provide a minimal boilerplate so people can start experimenting.

Also, this seems to bypass nvm use entirely.

nvm.fish/functions/nvm.fish

Lines 196 to 201 in cc70373

function _nvm_find_up --argument-names path file
test -e "$path/$file" && echo $path/$file || begin
test ! -z "$path" || return
_nvm_find_up (string replace --regex -- '/[^/]*$' "" $path) $file
end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants