benhoskings/babushka — pull request #310 by timriley (2014-06-14 01:32:18+00:00)
Title: Fix `NpmHelper.should_sudo?` with nodenv-style NPM installations

When I use [nodenv](https://github.com/wfarr/nodenv) to install node and NPM, it sits in a `~/.nodenv` directory like Ruby does when installed via rbenv.

Using the previous implementation for `NpmHelper.should_sudo?`, NPM-managed deps all asked for a sudo password, despite the node installation being in my home directory and writable. It was checking the real writability of `bin_path / pkg_binary`, which resolved to `"/Users/tim/.nodenv/bin/npm"`, based on the logic of `PkgHelper.bin_path`. However, that logic doesnt apply properly in this case, since `"/Users/tim/.nodenv/bin"` does not even exist.

To fix this, I took an approach similar to `PipHelper`, which you accepted in https://github.com/benhoskings/babushka/pull/241. This is also similar to the `gem env` approach you use in `Babushka::CurrentRuby`.

It works reliably on my machine, and I've added some specs to cover it. The only thing I'm not 100% sure about is how this works for nodes installed without a version manager like nodenv. Some googling reveals that it should give us the results we expect, such is in this [Stack Overflow answer](http://stackoverflow.com/a/14840304/308563):

> Run this:
>
> `npm config get prefix`
>
> The default on OS X is /usr/local, which means that npm will symlink binaries into /usr/local/bin, which should already be on your PATH (especially if you're using Homebrew).

This suggests that `npm config get prefix` should work for all kinds of installs, including ones that go into other non-home-directory parts of the system, and the writability check on that prefix should reliably determine whether we need sudo or not.

What do you think? This something you could pull in? It's needed for our team to get started with babushka, anyway, since we'd like to use nodenv. If this isn't the best approach, I'm happy to help look for something else.