Skip to content

Commit

Permalink
AMD: Upgrade cabinet and fix test config
Browse files Browse the repository at this point in the history
* No longer supports finding roots of multiple apps since it's
not possible to resolve a file where both requirejs and webpack configs
are supplied.
  • Loading branch information
mrjoelkemp committed Jun 2, 2016
1 parent d07e911 commit e1a32b5
Show file tree
Hide file tree
Showing 19 changed files with 66 additions and 45 deletions.
3 changes: 3 additions & 0 deletions .jscsrc
@@ -0,0 +1,3 @@
{
"preset": "mrjoelkemp"
}
42 changes: 26 additions & 16 deletions index.js
Expand Up @@ -3,8 +3,8 @@ var path = require('path');
var q = require('q');
var dir = require('node-dir');
var gmt = require('module-definition');
var lookup = require('module-lookup-amd');
var resolveDep = require('resolve-dependency-path');
var cabinet = require('filing-cabinet');
var debug = require('debug')('app-root');

/**
* Calls the given callback with a list of candidate root filenames
Expand All @@ -14,6 +14,7 @@ var resolveDep = require('resolve-dependency-path');
* @param {Function} options.success - Executed with the list of roots
*
* @param {String} [options.config] - Module loader configuration for aliased path resolution
* @param {String} [options.webpackConfig] - Module loader configuration for aliased path resolution
*
* @param {String[]} [options.ignoreDirectories] - List of directory names to ignore in the root search
* @param {String[]} [options.ignoreFiles] - List of filenames to ignore in the root search
Expand All @@ -28,24 +29,28 @@ module.exports = function(options) {
options.directory = path.resolve(options.directory);
options.includeNoDependencyModules = !!options.includeNoDependencyModules;

debug('given directory' + options.directory);
debug('include no dep modules? ' + options.includeNoDependencyModules);

getAllFiles(options)
.then(function(files) {
return files
// Types are used to determine if lookups are necessary
.map(function(file) {
debug('grabbed ' + files.length + ' files to sift through');

var actualModules = files.map(function(file) {
return fileObj = {
path: file,
type: path.extname(file) === '.js' ? gmt.sync(file) : ''
};
})
// Remove non-modules
.filter(function(fileObj) {
return fileObj.type !== 'none';
});

debug('number of actual modules to process: ' + actualModules.length);
return actualModules;
})
.then(function(files) {
// Get all files that are not depended on
return getIndependentFiles(files, options);
return getFilesNotDependedOn(files, options);
})
.done(function(files) {
options.success(files);
Expand Down Expand Up @@ -95,7 +100,7 @@ function getAllFiles(options) {
* @param {String} options.directory
* @return {Promise} Resolves with the list of independent filenames
*/
function getIndependentFiles(files, options) {
function getFilesNotDependedOn(files, options) {
// A look up table of all files used as dependencies within the directory
var dependencies = {};

Expand All @@ -105,6 +110,7 @@ function getIndependentFiles(files, options) {
// If a file cannot be parsed, it shouldn't be considered a root
try {
deps = getNonCoreDependencies(file.path);
debug('deps for ' + file.path + ':\n', deps);
} catch (e) {
dependencies[file.path] = true;
return;
Expand All @@ -118,18 +124,21 @@ function getIndependentFiles(files, options) {
}

deps.forEach(function(dep) {
if (file.type === 'amd' && options.config) {
dep = lookup(options.config, dep, file.path);
}
dep = cabinet({
partial: dep,
filename: file.path,
directory: options.directory,
config: options.config,
webpackConfig: options.webpackConfig
});

dep = resolveDep(dep, file.path, options.directory);
dependencies[dep] = true;
});
});

// Files that haven't been depended on
return files
.filter(function(file) {
debug('used dependencies: \n', dependencies);

return files.filter(function(file) {
return typeof dependencies[file.path] === 'undefined';
})
.map(function(file) {
Expand All @@ -139,6 +148,7 @@ function getIndependentFiles(files, options) {

/**
* Get a list of non-core dependencies for the given file
*
* @param {String} file
* @return {String[]}
*/
Expand Down
12 changes: 7 additions & 5 deletions package.json
Expand Up @@ -4,7 +4,7 @@
"description": "Find the entry-point/root file of a front-end application",
"main": "index.js",
"scripts": {
"test": "jscs -p google index.js test/index.js bin/ && mocha test/index.js"
"test": "jscs index.js test/index.js bin/ && mocha test/index.js"
},
"repository": {
"type": "git",
Expand All @@ -28,15 +28,17 @@
},
"homepage": "https://github.com/mrjoelkemp/node-app-root",
"dependencies": {
"debug": "~2.2.0",
"filing-cabinet": "~1.2.3",
"module-definition": "^2.1.0",
"module-lookup-amd": "^2.0.0",
"node-dir": "~0.1.6",
"object-assign": "~4.1.0",
"precinct": "~2.5.1",
"q": "^1.0.1",
"resolve-dependency-path": "^1.0.1"
"q": "^1.0.1"
},
"devDependencies": {
"jscs": "~1.8.0",
"jscs": "~2.11.0",
"jscs-preset-mrjoelkemp": "~1.0.0",
"mocha": "~2.0.1"
}
}
7 changes: 7 additions & 0 deletions test/apps/amd/config.json
@@ -0,0 +1,7 @@
require.config({
"baseUrl": "js",

"paths": {
"backbone": "bower_components/backbone/backbone"
}
});
8 changes: 4 additions & 4 deletions test/apps/amd/a.js → test/apps/amd/js/a.js
Expand Up @@ -3,10 +3,10 @@ define(function (require) {

// Should be the root node unless require by another file
// Score should be 4 + score of each dependency
var b = require('./b'),
c = require('./c'),
v1 = require('./views/v1.js'),
m1 = require('./models/m1.js'),
var b = require('b'),
c = require('c'),
v1 = require('views/v1'),
m1 = require('models/m1'),

// These should not count toward this file's score
path = require('path'),
Expand Down
2 changes: 1 addition & 1 deletion test/apps/amd/a2.js → test/apps/amd/js/a2.js
Expand Up @@ -7,7 +7,7 @@ define(function (require) {
http = require('http'),

// Makes this the root
a = require('./a');
a = require('a');

console.log('Booyah');

Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions test/apps/amd/js/bower_components/backbone/backbone.js
@@ -0,0 +1 @@
// Sup?
Expand Up @@ -2,10 +2,10 @@ define(function (require) {

// Should be the root node unless require by another file
// Score should be 4 + score of each dependency
var b = require('./b'),
c = require('./c'),
v1 = require('./views/v1.js'),
m1 = require('./models/m1.js'),
var b = require('b'),
c = require('c'),
v1 = require('views/v1.js'),
m1 = require('models/m1.js'),

// These should not count toward this file's score
path = require('path'),
Expand Down
Expand Up @@ -6,7 +6,7 @@ define(function (require) {
http = require('http'),

// Makes this the root
a = require('./a');
a = require('a');

console.log('Booyah');

Expand Down
File renamed without changes.
@@ -1,4 +1,4 @@
define(function (require) {
var fs = require('fs');
var v1 = require('./views/v1');
var v1 = require('views/v1');
});
2 changes: 1 addition & 1 deletion test/apps/amd/c.js → test/apps/amd/js/c.js
@@ -1,5 +1,5 @@
// non-root
define(function (require) {
var fs = require('fs');
var v1 = require('./views/v1');
var v1 = require('views/v1');
});
File renamed without changes.
File renamed without changes.
File renamed without changes.
22 changes: 10 additions & 12 deletions test/index.js
@@ -1,11 +1,6 @@
var getAppRoot = require('../');
var assert = require('assert');

function extend(o, o2) {
for (var prop in o2) {
o[prop] = o2[prop];
}
}
var extend = require('object-assign');

describe('app-root', function() {
var options = {
Expand Down Expand Up @@ -89,12 +84,15 @@ describe('app-root', function() {
});
});

it('finds the roots of an entire directory of multiple apps', function(done) {
// There is confusion around which loader to use with files that
// have both a requirejs and webpack config.
it.skip('finds the roots of an entire directory of multiple apps', function(done) {
var opts = {
directory: __dirname + '/apps',
success: function(root) {
config: __dirname + '/apps/amd/config.json',
success: function(roots) {
// Equal to the number of apps within /apps/
assert(root.length === 4);
assert.equal(roots.length, 4);
done();
}
};
Expand All @@ -108,10 +106,9 @@ describe('app-root', function() {
it('finds the roots of a commonjs app', function(done) {
var opts = {
directory: __dirname + '/apps/commonjs',

success: function(root) {
assert(root.length === 1);
assert(root[0].indexOf('a2.js') !== -1);
assert.equal(root.length, 1);
assert.ok(root[0].indexOf('a2.js') !== -1);
done();
}
};
Expand All @@ -126,6 +123,7 @@ describe('app-root', function() {
it('finds the roots of an amd app', function(done) {
var opts = {
directory: __dirname + '/apps/amd',
config: __dirname + '/apps/amd/config.json',
success: function(root) {
assert(root.length === 1);
assert(root[0].indexOf('a2.js') !== -1);
Expand Down

0 comments on commit e1a32b5

Please sign in to comment.