wbyoung / avn

Automatic Version Switching for Node
MIT License
1.14k stars 54 forks source link

How can I programmatically detect if `avn` is installed? #86

Closed alanhogan closed 5 years ago

alanhogan commented 5 years ago

My team maintains a script that is designed to ensure a developer's environment is fully prepared to develop on our project.

Part of this is detecting avn and installing it if messing.

We used to do this like this:

  which avn > /dev/null
  if [ $? -ne 0 ]; then
    nvm use default
    try "Installing \`avn\`" "npm install -g avn avn-nvm avn-n"
    avn setup
  fi

However at least in some cases, avn will be fully functional, switching node versions on cd as designed, but which avn will fail to find avn.

For example, this is happening on my machine. The default node is v5.10.1 and avn is installed globally with that node version. However avn is "missing" after avn itself switched to our project's node version of 8.9.x.

Is there a recommended way to test for the presence of avn?

Details

The output of __avn_debug in the directory with a .node-version file is:

avn activated v8.9.3 (avn-nvm v8.9.3)
nvm use v8.9.3 > /dev/null;

avn is loaded in my ~/.{bash|zsh}{_profile|rc} file with:

[[ -s "$HOME/.avn/bin/avn.sh" ]] && source "$HOME/.avn/bin/avn.sh" # load avn

nvm specific

alanhogan commented 5 years ago

I am probably going to proceed with if [ command -v __avn_debug ] as my test. This should succeed whenever avn has been successfully loaded into the shell.

wbyoung commented 5 years ago

There are numerous things you could do, but this feels like it's outside the scope of concern for the project. There are too many variables at play. Which shell does everyone have? Is which a built in or a program? This is a problem that's present in other contexts as well (i.e. in build/configure systems). Usually, it's more important to test that a program actually functions than it is to ensure it's installed. In this case, you'd likely have to load your while shell environment first.

Regardless, there's no issue here and you already found something that it sounds like may work, so I'm going to close this.

ljharb commented 5 years ago

Why not actually cd into two different directories that declare different node versions and see if the node version changes?

In other words, feature test instead of user agent sniff.

alanhogan commented 5 years ago

@ljharb Because that's slow and my goal is not to write a test suite for avn but rather to see if it is installed.