Skip to content

Commit

Permalink
[ENHANCEMENT] Add nodeModulesPath property to Project and Addon
Browse files Browse the repository at this point in the history
This uses the cli utility helper in node-modules-path to
determine the nodeModulesPath.

Also modifies private method `AddonDiscovery.discoverFromDependencies`
to get the nodeModules path from the project (or addon if inside
AddonDiscovery.discoverChildAddons) as its second argument.

This property is intended to provide a way for 3rd-party addons (like
ember-cli-dependency-checker) to know the project's node modules
path without having to discover it in an ad hoc way.
  • Loading branch information
bantic authored and mixonic committed Apr 7, 2015
1 parent 1da3465 commit 096178b
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 12 deletions.
9 changes: 4 additions & 5 deletions lib/models/addon-discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var path = require('path');
var CoreObject = require('core-object');
var resolve = require('resolve');
var findup = require('findup');
var nodeModulesPath = require ('../cli/node-modules-path');

/**
AddonDiscovery is responsible for collecting information about all of the
Expand Down Expand Up @@ -42,7 +41,7 @@ AddonDiscovery.prototype.constructor = AddonDiscovery;
AddonDiscovery.prototype.discoverProjectAddons = function(project) {
var projectAsAddon = this.discoverFromProjectItself(project);
var internalAddons = this.discoverFromInternalProjectAddons(project);
var dependencyAddons = this.discoverFromDependencies(project.root, project.pkg, false);
var dependencyAddons = this.discoverFromDependencies(project.root, project.nodeModulesPath, project.pkg, false);
var inRepoAddons = this.discoverInRepoAddons(project.root, project.pkg);
var addons = projectAsAddon.concat(internalAddons, dependencyAddons, inRepoAddons);

Expand All @@ -61,7 +60,7 @@ AddonDiscovery.prototype.discoverProjectAddons = function(project) {
*/
AddonDiscovery.prototype.discoverChildAddons = function(addon) {
debug('discoverChildAddons: %s(%s)', addon.name, addon.root);
var dependencyAddons = this.discoverFromDependencies(addon.root, addon.pkg, true);
var dependencyAddons = this.discoverFromDependencies(addon.root, addon.nodeModulesPath, addon.pkg, true);
var inRepoAddons = this.discoverInRepoAddons(addon.root, addon.pkg);
var addons = dependencyAddons.concat(inRepoAddons);
return addons;
Expand Down Expand Up @@ -92,7 +91,7 @@ AddonDiscovery.prototype.discoverFromProjectItself = function(project) {
@private
@method discoverFromDependencies
*/
AddonDiscovery.prototype.discoverFromDependencies = function(root, pkg, excludeDevDeps) {
AddonDiscovery.prototype.discoverFromDependencies = function(root, nodeModulesPath, pkg, excludeDevDeps) {
var discovery = this;
var addons = Object.keys(this.dependencies(pkg, excludeDevDeps)).map(function(name) {
if (name !== 'ember-cli') {
Expand All @@ -104,7 +103,7 @@ AddonDiscovery.prototype.discoverFromDependencies = function(root, pkg, excludeD

// this supports packages that do not have a valid entry point
// script (aka `main` entry in `package.json` or `index.js`)
addonPath = path.join(nodeModulesPath(root), name);
addonPath = path.join(nodeModulesPath, name);
var addon = discovery.discoverAtPath(addonPath);
if (addon) {
var chalk = require('chalk');
Expand Down
6 changes: 6 additions & 0 deletions lib/models/addon.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var glob = require('glob');
var SilentError = require('../errors/silent');
var reexport = require('../utilities/reexport');
var debug = require('debug')('ember-cli:addon');
var nodeModulesPath = require('../cli/node-modules-path');

var p = require('../preprocessors');
var preprocessJs = p.preprocessJs;
Expand Down Expand Up @@ -62,6 +63,11 @@ function Addon(parent, project) {
this.registry = p.defaultRegistry(this);
this._didRequiredBuildPackages = false;

if (!this.root) {
throw new Error('Addon classes must be instantiated with the `root` property');
}
this.nodeModulesPath = nodeModulesPath(this.root);

this.treePaths = {
app: 'app',
styles: 'app/styles',
Expand Down
7 changes: 6 additions & 1 deletion lib/models/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function Project(root, pkg, ui) {
this.addons = [];
this.liveReloadFilterPatterns = [];
this.setupBowerDirectory();
this.setupNodeModulesPath();
this.addonDiscovery = new AddonDiscovery(this.ui);
this.addonsFactory = new AddonsFactory(this, this);
}
Expand Down Expand Up @@ -69,6 +70,10 @@ Project.prototype.setupBowerDirectory = function() {
debug('bowerDirectory: %s', this.bowerDirectory);
};

Project.prototype.setupNodeModulesPath = function(){
this.nodeModulesPath = nodeModulesPath(this.root);
};

var NULL_PROJECT = new Project(process.cwd(), {});

NULL_PROJECT.isEmberCLIProject = function() {
Expand Down Expand Up @@ -217,7 +222,7 @@ Project.prototype.require = function(file) {
if (/^\.\//.test(file)) { // Starts with ./
return require(path.join(this.root, file));
} else {
return require(path.join(nodeModulesPath(this.root), file));
return require(path.join(this.nodeModulesPath, file));
}
};

Expand Down
1 change: 1 addition & 0 deletions tests/helpers/mock-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ MockProject.prototype.discoverAddons = Project.prototype.discoverAddons;
MockProject.prototype.addIfAddon = Project.prototype.addIfAddon;
MockProject.prototype.supportedInternalAddonPaths = Project.prototype.supportedInternalAddonPaths;
MockProject.prototype.setupBowerDirectory = Project.prototype.setupBowerDirectory;
MockProject.prototype.setupNodeModulesPath = Project.prototype.setupNodeModulesPath;
MockProject.prototype.dependencies = function() {
return [];
};
Expand Down
12 changes: 8 additions & 4 deletions tests/unit/models/addon-discovery-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ describe('models/addon-discovery.js', function() {

it('can find a package without a main entry point [DEPRECATED]', function() {
var root = path.join(fixturePath, 'shared-package', 'base');
var addonNodeModulesPath = path.join(root, 'node_modules');
var actualPaths = [];
var discovery = new AddonDiscovery(ui);

Expand All @@ -194,7 +195,7 @@ describe('models/addon-discovery.js', function() {
return providedPath;
};

discovery.discoverFromDependencies(root, mockPkg, true);
discovery.discoverFromDependencies(root, addonNodeModulesPath, mockPkg, true);

var expectedPaths = [
path.join(root, 'node_modules', 'foo-bar'),
Expand All @@ -211,6 +212,7 @@ describe('models/addon-discovery.js', function() {

it('does not error when dependencies are not found', function() {
var root = path.join(fixturePath, 'shared-package', 'base');
var addonNodeModulesPath = path.join(root, 'node_modules');
var actualPaths = [];
var discovery = new AddonDiscovery(ui);

Expand All @@ -221,7 +223,7 @@ describe('models/addon-discovery.js', function() {
return providedPath;
};

discovery.discoverFromDependencies(root, mockPkg, true);
discovery.discoverFromDependencies(root, addonNodeModulesPath, mockPkg, true);

var expectedPaths = [
path.join(root, 'node_modules', 'foo-bar'),
Expand All @@ -234,6 +236,7 @@ describe('models/addon-discovery.js', function() {

it('calls discoverAtPath for each entry in dependencies', function() {
var root = path.join(fixturePath, 'shared-package', 'base');
var addonNodeModulesPath = path.join(root, 'node_modules');
var actualPaths = [];
var discovery = new AddonDiscovery(ui);

Expand All @@ -243,7 +246,7 @@ describe('models/addon-discovery.js', function() {
return providedPath;
};

discovery.discoverFromDependencies(root, mockPkg);
discovery.discoverFromDependencies(root, addonNodeModulesPath, mockPkg);

var expectedPaths = [
path.join(root, '..', 'node_modules', 'dev-foo-bar'),
Expand All @@ -256,6 +259,7 @@ describe('models/addon-discovery.js', function() {

it('excludes devDeps if `excludeDevDeps` is true', function() {
var root = path.join(fixturePath, 'shared-package', 'base');
var addonNodeModulesPath = path.join(root, 'node_modules');
var actualPaths = [];
var discovery = new AddonDiscovery(ui);

Expand All @@ -265,7 +269,7 @@ describe('models/addon-discovery.js', function() {
return providedPath;
};

discovery.discoverFromDependencies(root, mockPkg, true);
discovery.discoverFromDependencies(root, addonNodeModulesPath, mockPkg, true);

var expectedPaths = [
path.join(root, 'node_modules', 'foo-bar'),
Expand Down
17 changes: 15 additions & 2 deletions tests/unit/models/addon-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ var fixturePath = path.resolve(__dirname, '../../fixtures/addon');
describe('models/addon.js', function() {
var addon, project, projectPath;

describe('root property', function() {
it('is required', function() {
expect(function() {
var TheAddon = Addon.extend({root:undefined});
new TheAddon();
}).to.throw(/root/);
});
});

describe('treePaths and treeForMethods', function() {
var FirstAddon, SecondAddon;

Expand All @@ -31,6 +40,7 @@ describe('models/addon.js', function() {

FirstAddon = Addon.extend({
name: 'first',
root: projectPath,

init: function() {
this.treePaths.vendor = 'blazorz';
Expand All @@ -40,6 +50,7 @@ describe('models/addon.js', function() {

SecondAddon = Addon.extend({
name: 'first',
root: projectPath,

init: function() {
this.treePaths.vendor = 'blammo';
Expand Down Expand Up @@ -345,7 +356,8 @@ describe('models/addon.js', function() {

beforeEach(function() {
var MyAddon = Addon.extend({
name: 'test-project'
name: 'test-project',
root: 'foo'
});

var projectPath = path.resolve(fixturePath, 'simple');
Expand Down Expand Up @@ -507,7 +519,8 @@ describe('models/addon.js', function() {
project = new Project(projectPath, packageContents, ui);

var AddonTemp = Addon.extend({
name: 'temp'
name: 'temp',
root: 'foo'
});

addon = new AddonTemp(project, project);
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/models/project-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,4 +472,30 @@ describe('models/project.js', function() {
expect(project.bowerDirectory).to.equal('bower_components');
});
});

describe('nodeModulesPath', function() {
function makeProject() {
projectPath = path.resolve(__dirname, '../../fixtures/addon/simple');
project = new Project(projectPath, {}, new MockUI());
}

afterEach(function() {
delete process.env.EMBER_NODE_PATH;
});

it('should equal env.EMBER_NODE_PATH when it is set', function() {
var nodePath = '/my/path/node_modules';
process.env.EMBER_NODE_PATH = nodePath;

makeProject();

expect(project.nodeModulesPath).to.equal(nodePath);
});

it('should equal project.root joined with "node_modules" when EMBER_NODE_PATH is not set', function() {
makeProject();

expect(project.nodeModulesPath).to.equal(path.join(projectPath, 'node_modules'));
});
});
});

0 comments on commit 096178b

Please sign in to comment.