Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: auto invoke nvm use on PWD change #181

Conversation

edouard-lopez
Copy link

@edouard-lopez edouard-lopez commented Apr 14, 2022

Hello @jorgebucaran,
long time no see :)

As I was not satisfied by the Deeper Shell Integration for Fish solution from _classic NVM, I sought something simpler.

I added a mechanism to automatically invoke nvm use on PWD changes based on @ygormutti's idea wbyoung/avn#12 (comment).

Current version is pretty dumb, but wanted to check if that was of interest to you first. So please let me know if you want such feature?
Feature behind a flag:

set --universal auto_invoke_nvm true

@jorgebucaran
Copy link
Owner

Hi, @edouard-lopez! Perhaps this should be a plug-in? Definitely not the default behavior.

@edouard-lopez
Copy link
Author

Can you elaborate on the plugin idea? Do you mean with feature flag, a dedicated repo, or something else?

@jorgebucaran
Copy link
Owner

For starters this function could be a different plugin, installed separately. Or as you say, behind a feature flag (universal variable).

@edouard-lopez
Copy link
Author

update with feature flag

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@@ -1,5 +1,8 @@
function _nvm_auto_invoke \
--on-variable PWD \
--description 'Use Node.js version specified by project automatically'
nvm use 2>/tmp/_nvm_auto_invoke.log

if set --query auto_invoke_nvm; and test "$auto_invoke_nvm" = true
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather have the flag prevent _nvm_auto_invoke from being defined at all, rather than define the --on-variable PWD hooks and incur this check every time.

Copy link
Author

@edouard-lopez edouard-lopez Apr 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing something like below break the autoloading mechanism:

if set --query nvm_use_on_pwd_change; and test "$nvm_use_on_pwd_change" = true
    function _nvm_use_on_pwd_change \
        --on-variable PWD \
…
    end
end

Should I hook _nvm_use_on_pwd_chang on both the flag variable ($nvm_use_on_pwd_change) AND $PWD? Or do something like https://github.com/jorgebucaran/hydro/blob/d4875065ceea226f58ead97dd9b2417937344d6e/conf.d/hydro.fish#L119-L120 ?

Copy link
Owner

@jorgebucaran jorgebucaran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for adding tests, good work. Left you several comments.

@jorgebucaran jorgebucaran added the enhancement New feature or fix label Apr 14, 2022
Copy link
Owner

@jorgebucaran jorgebucaran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, more comments left. Thank you!

if test "$nvm_use_on_pwd_change" = true
nvm use 2>/tmp/_nvm_use_on_pwd_change.log
end
end && _nvm_use_on_pwd_change
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll end up running the function whenever the file is sourced.

Copy link
Author

@edouard-lopez edouard-lopez May 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok the --on-variable option is enough, my bad. I will remove the call

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -0,0 +1,9 @@
function _nvm_use_on_pwd_change \
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only need to define the PWD change function when the feature flag is set. This may involve defining another function inside the function or some other trick. 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about using funcsave but the manual states:

Note that because fish loads functions on-demand, saved functions will not function as event handlers until they are run or sourced otherwise. To activate an event handler for every new shell, add the function to your configuration file instead of using funcsave.

So need to add a call in conf.d/nvm.fish

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we get even fancier? Consider:

function _nvm_use_on_pwd_change --argument-names _ op --on-variable nvm_use_on_pwd_change
    if test "$op" = ERASE
        functions --erase _nvm_pwd_change
    else
        function _nvm_pwd_change --on-variable PWD
            nvm use --silent 2>/dev/null
        end
    end
end

--on-variable PWD \
--description 'Use Node.js version specified by project automatically'

if test "$nvm_use_on_pwd_change" = true
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also reasonable would be to check if the variable is just set. Maybe that's even fishier.

README.md Outdated Show resolved Hide resolved
functions/_nvm_use_on_pwd_change.fish Show resolved Hide resolved
Co-authored-by: Simon Lydell <[email protected]>
@@ -0,0 +1,9 @@
function _nvm_use_on_pwd_change \
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we get even fancier? Consider:

function _nvm_use_on_pwd_change --argument-names _ op --on-variable nvm_use_on_pwd_change
    if test "$op" = ERASE
        functions --erase _nvm_pwd_change
    else
        function _nvm_pwd_change --on-variable PWD
            nvm use --silent 2>/dev/null
        end
    end
end

@jorgebucaran
Copy link
Owner

Good work, @edouard-lopez. Left you one suggestion. 👋

@bastianwegge
Copy link

Just pinging, we just ran into the same scenario, used your code and it works flawlessly. Is there any chance for this to make it to master/release? :)

@jorgebucaran
Copy link
Owner

I left some comments that weren't resolved. I also don't want to bloat this tool with features, hence the long silence.

@edouard-lopez
Copy link
Author

I haven't the need for this currently, so won't push forward, @bastianwegge feel free to push on my MR or fork it.

You might be interested in volta.

@lydell
Copy link

lydell commented Jan 20, 2023

In case someone else comes across this PR and wants this functionality, here’s a way to do it in userland:

#186 (comment)

@jorgebucaran
Copy link
Owner

Took another peek while fixing bugs and closing old issues. This one's a bit of a tricky issue, as it adds a lot of complication to nvm.fish for everyone. Personally, I don't have a need for it, but I get that some folks do. That's why we worked on #186 to improve switch times and encourage folks to come up with their own tiny functions (remember how cool it is to extend your Fish shell?), like @lydell did. 💯

@edouard-lopez edouard-lopez deleted the feat/auto-invoke-on-pwd-change branch May 2, 2023 13:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants