Skip to content

Commit

Permalink
fix module paths with npm prefix in windows (#114) (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
thoussem authored and SBoudrias committed Jun 24, 2019
1 parent c2143b4 commit a3082a2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
3 changes: 2 additions & 1 deletion lib/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ resolver.getNpmPaths = function () {
const testNpm = spawn.sync('npm', ['-g', 'prefix'], {encoding: 'utf8'});
if (!testNpm.error) {
const npmBase = testNpm.stdout.trim();
paths.push(path.resolve(npmBase, 'lib/node_modules'));
const globalInstall = win32 ? `${npmBase}/node_modules` : `${npmBase}/lib/node_modules`;
paths.push(path.resolve(globalInstall));
}

// Adds support for generator resolving when yeoman-generator has been linked
Expand Down
26 changes: 20 additions & 6 deletions test/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const sinon = require('sinon');
const spawn = require('cross-spawn');
const Environment = require('../lib/environment');

Expand All @@ -22,9 +23,9 @@ describe('Environment Resolver', function () {
spawn.sync('npm', ['install', '-g', 'generator-dummytest', 'generator-dummy', '--no-package-lock']);

fs.symlinkSync(
path.resolve('../generator-extend'),
path.resolve('node_modules/generator-extend'),
'dir'
path.resolve('../generator-extend'),
path.resolve('node_modules/generator-extend'),
'dir'
);

if (!fs.existsSync(scopedFolder)) {
Expand All @@ -33,9 +34,9 @@ describe('Environment Resolver', function () {

if (!fs.existsSync(scopedGenerator)) {
fs.symlinkSync(
path.resolve('../generator-scoped'),
scopedGenerator,
'dir'
path.resolve('../generator-scoped'),
scopedGenerator,
'dir'
);
}
});
Expand Down Expand Up @@ -202,5 +203,18 @@ describe('Environment Resolver', function () {
assert(this.env.getNpmPaths().indexOf(this.bestBet2) >= 0);
});
});

describe('with npm global prefix', () => {
it('append npm modules path depending on your OS', function () {
const npmPrefix = '/npm_prefix';
const spawnStub = sinon.stub(spawn, 'sync').returns({stdout: npmPrefix});
if (process.platform === 'win32') {
assert(this.env.getNpmPaths().indexOf(path.resolve(npmPrefix, 'node_modules')) > 0);
} else {
assert(this.env.getNpmPaths().indexOf(path.resolve(npmPrefix, 'lib/node_modules')) > 0);
}
spawnStub.restore();
});
});
});
});

0 comments on commit a3082a2

Please sign in to comment.