sindresorhus / global-directory

Get the directory of globally installed packages and binaries
MIT License
75 stars 10 forks source link

Add special case for Homebrew #6

Closed boneskull closed 5 years ago

boneskull commented 5 years ago

After poking at this a bit further, I found we'd need extra I/O to make this work in every case.

If, for example, your executable is ava, we can't use process.env._ because it points to /path/to/ava. If we instead ran node node_modules/.bin/ava, then process.env._ would point to node in your PATH.

I've updated the code to check if node is indeed what the user executed; if that's the case, we can use process.env._ to get at its path. Otherwise, we just use process.execPath. On my system, this is still incorrect when running ava.

If we wanted this to always work, we'd need to use which or something to that effect, and I assume you don't want to be invoking child_process.anything() here.

boneskull commented 5 years ago

Looks like the tests won't run on Node.js v4. I can add a commit to drop v4 from .travis.yml (and update engines field, add new Node.js versions to CI, etc)

boneskull commented 5 years ago

another approach would just be to just look for /usr/local/Cellar/node/ in process.execPath and force it to use /usr/local as the prefix.

boneskull commented 5 years ago

@sindresorhus (ping) I could use some direction here; there's a couple ways to go and I'm not sure which you'd prefer. The choices are:

  1. Consume which or something similar
  2. Add a special case for Homebrew
LitoMore commented 5 years ago

@boneskull @sindresorhus I think we could drop support for Node.js 4. Just do it. Sindre already.

Sindre has removed support for Node.js 4 in many of his repositories.

sindresorhus commented 5 years ago

I would prefer 1, adding a special case for Homebrew.

boneskull commented 5 years ago

@sindresorhus OK, this should be ready.

I tried to add OSX to the build matrix and install node using homebrew, but was running in to trouble and don't have the time to hack on it further atm.

Before this change:

$ npm test

> global-dirs@0.1.1 test /Users/boneskull/projects/sindresorhus/global-dirs
> xo && ava

{ npm:
   { prefix: '/usr/local/Cellar/node/11.14.0_1',
     packages: '/usr/local/Cellar/node/11.14.0_1/lib/node_modules',
     binaries: '/usr/local/Cellar/node/11.14.0_1/bin' },
  yarn:
   { prefix: '/Users/boneskull/.config/yarn',
     packages: '/Users/boneskull/.config/yarn/global/node_modules',
     binaries: '/Users/boneskull/.config/yarn/global/node_modules/.bin' } }

  1 passed
  3 failed

  npm.prefix

  /Users/boneskull/projects/sindresorhus/global-dirs/test.js:10

    9: test('npm.prefix', async t => {
   10:   t.is(globalDirs.npm.prefix, await npm(['prefix', '--global']));
   11: });

  Difference:

  - '/usr/local/Cellar/node/11.14.0_1'
  + '/usr/local'

  npm.packages

  /Users/boneskull/projects/sindresorhus/global-dirs/test.js:14

   13: test('npm.packages', async t => {
   14:   t.is(globalDirs.npm.packages, await npm(['root', '--global']));
   15: });

  Difference:

  - '/usr/local/Cellar/node/11.14.0_1/lib/node_modules'
  + '/usr/local/lib/node_modules'

  npm.binaries

  /Users/boneskull/projects/sindresorhus/global-dirs/test.js:18

   17: test('npm.binaries', async t => {
   18:   t.is(globalDirs.npm.binaries, await npm(['bin', '--global']));
   19: });

  Difference:

  - '/usr/local/Cellar/node/11.14.0_1/bin'
  + '/usr/local/bin'

After this change:

$ npm test

> global-dirs@0.1.1 test /Users/boneskull/projects/sindresorhus/global-dirs
> xo && ava

{ npm:
   { prefix: '/usr/local',
     packages: '/usr/local/lib/node_modules',
     binaries: '/usr/local/bin' },
  yarn:
   { prefix: '/Users/boneskull/.config/yarn',
     packages: '/Users/boneskull/.config/yarn/global/node_modules',
     binaries: '/Users/boneskull/.config/yarn/global/node_modules/.bin' } }

  4 passed
boneskull commented 5 years ago

Once this is released, I'll update create-yo to use this module.

sindresorhus commented 5 years ago

I'll do a new release once the other open PRs are merged.