Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactor component-ls(1) to utilize `component.lookup()`

  • Loading branch information...
commit 200358ccdf3118d7ef24919a95c9bddffe178616 1 parent 201b5ec
@tj tj authored
Showing with 11 additions and 25 deletions.
  1. +10 −24 bin/component-ls
  2. +1 −1  lib/component.js
View
34 bin/component-ls
@@ -5,11 +5,11 @@
*/
var program = require('commander')
+ , component = require('..')
, archy = require('archy')
, path = require('path')
- , fs = require('fs')
- , exists = fs.existsSync
- , resolve = path.resolve;
+ , resolve = path.resolve
+ , lookup = component.lookup
// parse argv
@@ -24,15 +24,12 @@ console.log(indent(archy(tree)));
/**
* Return the dependency tree of the given config `file`.
*
- * TODO: all this should be in ./lib or the builder,
- * but the builder doesn't really make sense here
- *
* @param {String} file
- * @param {Array} dirs
+ * @param {Array} paths
* @return {Object}
*/
-function tree(file, dirs){
+function tree(file, paths){
var conf = require(resolve(file));
var deps = Object.keys(conf.dependencies || {});
var node = {};
@@ -40,8 +37,8 @@ function tree(file, dirs){
// local deps
if (conf.local) deps = deps.concat(conf.local);
- // lookup dirs
- dirs = (dirs || ['components']).concat(conf.paths);
+ // lookup paths
+ paths = (paths || ['components']).concat(conf.paths);
// label
node.label = '\033[36m' + conf.name + '\033[m';
@@ -50,27 +47,16 @@ function tree(file, dirs){
// dependencies
node.nodes = deps.map(function(dep){
var dep = dep.replace('/', '-');
- var file = lookup(dep, dirs);
+ var file = lookup(dep, paths);
if (!file) throw new Error('failed to resolve location of "' + dep + '"');
- return tree(file, dirs);
+ file = resolve(file, 'component.json');
+ return tree(file, paths);
});
return node;
}
/**
- * Lookup package `name` in `dirs`.
- */
-
-function lookup(name, dirs) {
- for (var i = 0; i < dirs.length; ++i) {
- var dir = dirs[i];
- var file = resolve(dir, name, 'component.json');
- if (exists(file)) return file;
- }
-}
-
-/**
* Indent `str`.
*/
View
2  lib/component.js
@@ -37,7 +37,7 @@ exports.utils = utils;
exports.lookup = function(pkg, paths){
debug('lookup %s', pkg);
- for (var i = 0, len = paths.length; i < len; ++i) {
+ for (var i = 0; i < paths.length; ++i) {
var path = join(paths[i], pkg);
debug('check %s', path);
if (exists(path)) {
Please sign in to comment.
Something went wrong with that request. Please try again.