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

Add localOnly argument to generator lookup #113

Merged
merged 3 commits into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 54 additions & 14 deletions lib/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,21 @@ const resolver = module.exports;
* So this index file `node_modules/generator-dummy/lib/generators/yo/index.js` would be
* registered as `dummy:yo` generator.
*
* @param {boolean} [localOnly = false] - Set trueto skip lookups of
* globally-installed generators.
* @param {function} cb - Callback called once the lookup is done. Take err as first
* parameter.
*/
resolver.lookup = function (cb) {
const generatorsModules = this.findGeneratorsIn(this.getNpmPaths().reverse());
resolver.lookup = function (localOnly, cb) {
// Resolve signature where localOnly is omitted.
if (typeof localOnly !== 'boolean') {
if (typeof localOnly === 'function') {
cb = localOnly;
}
localOnly = false;
}

const generatorsModules = this.findGeneratorsIn(this.getNpmPaths(localOnly).reverse());
const patterns = [];

for (const lookup of this.lookups) {
Expand Down Expand Up @@ -123,9 +133,50 @@ resolver._tryRegistering = function (generatorReference) {

/**
* Get the npm lookup directories (`node_modules/`)
* @param {boolean} [localOnly = false] - Set true to exclude global lookup
* directories.
* @return {Array} lookup paths
*/
resolver.getNpmPaths = function (localOnly = false) {
// Start with the local paths.
let paths = this._getLocalNpmPaths();

// Append global paths, unless they should be excluded.
if (!localOnly) {
paths = paths.concat(this._getGlobalNpmPaths());
}

return _.uniq(paths);
};

/**
* Get the local npm lookup directories
* @private
* @return {Array} lookup paths
*/
resolver._getLocalNpmPaths = function () {
const paths = [];

// Walk up the CWD and add `node_modules/` folder lookup on each level
process.cwd().split(path.sep).forEach((part, i, parts) => {
let lookup = path.join(...parts.slice(0, i + 1), 'node_modules');

if (!win32) {
lookup = `/${lookup}`;
}

paths.push(lookup);
});

return _.uniq(paths.reverse());
};

/**
* Get the global npm lookup directories
* @private
* @return {Array} lookup paths
*/
resolver.getNpmPaths = function () {
resolver._getGlobalNpmPaths = function () {
let paths = [];

// Default paths for each system
Expand Down Expand Up @@ -175,17 +226,6 @@ resolver.getNpmPaths = function () {
paths.push(path.join(path.dirname(process.argv[1]), '../..'));
}

// Walk up the CWD and add `node_modules/` folder lookup on each level
process.cwd().split(path.sep).forEach((part, i, parts) => {
let lookup = path.join(...parts.slice(0, i + 1), 'node_modules');

if (!win32) {
lookup = `/${lookup}`;
}

paths.push(lookup);
});

return _.uniq(paths.reverse());
};

Expand Down
65 changes: 65 additions & 0 deletions test/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,36 @@ describe('Environment Resolver', function () {
assert.ok(resolved.indexOf('subdir') !== -1, `Couldn't find 'subdir' in ${resolved}`);
});
});

describe('when localOnly argument is true', () => {
beforeEach(function () {
this.env = new Environment();
assert.equal(this.env.namespaces().length, 0, 'ensure env is empty');
this.env.lookup(true);
});

it('register local generators', function () {
assert.ok(this.env.get('dummy:app'));
assert.ok(this.env.get('dummy:yo'));
});

it('register generators in scoped packages', function () {
assert.ok(this.env.get('@dummyscope/scoped:app'));
});

it('register non-dependency local generator', function () {
assert.ok(this.env.get('jquery:app'));
});

it('register symlinked generators', function () {
assert.ok(this.env.get('extend:support'));
});

globalLookupTest('does not register global generators', function () {
assert.ok(!this.env.get('dummytest:app'));
assert.ok(!this.env.get('dummytest:controller'));
});
});
});

describe('#getNpmPaths()', () => {
Expand Down Expand Up @@ -202,5 +232,40 @@ describe('Environment Resolver', function () {
assert(this.env.getNpmPaths().indexOf(this.bestBet2) >= 0);
});
});

describe('when localsOnly argument is true', () => {
afterEach(() => {
delete process.env.NODE_PATH;
delete process.env.NVM_PATH;
});

it('walk up the CWD lookups dir', function () {
const paths = this.env.getNpmPaths();
assert.equal(paths[0], path.join(process.cwd(), 'node_modules'));
assert.equal(paths[1], path.join(process.cwd(), '../node_modules'));
});

it('does not append NODE_PATH', function () {
process.env.NODE_PATH = '/some/dummy/path';
assert(this.env.getNpmPaths(true).indexOf(process.env.NODE_PATH) === -1);
});

it('does not append NVM_PATH', function () {
process.env.NVM_PATH = '/some/dummy/path';
assert(this.env.getNpmPaths(true).indexOf(path.join(path.dirname(process.env.NVM_PATH), 'node_modules')) === -1);
});

it('does not append best bet', function () {
assert(this.env.getNpmPaths(true).indexOf(this.bestBet) === -1);
});

it('does not append default NPM dir depending on your OS', function () {
if (process.platform === 'win32') {
assert(this.env.getNpmPaths(true).indexOf(path.join(process.env.APPDATA, 'npm/node_modules')) === -1);
} else {
assert(this.env.getNpmPaths(true).indexOf('/usr/lib/node_modules') === -1);
}
});
});
});
});