forked from componentjs/component
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponent-ls
executable file
·78 lines (60 loc) · 1.62 KB
/
component-ls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env node
/**
* Module dependencies.
*/
var program = require('commander');
var component = require('..');
var archy = require('archy');
var path = require('path');
var resolve = path.resolve;
var lookup = component.lookup;
var fs = require('fs');
var exists = fs.existsSync;
var utils = component.utils;
// parse argv
program
.option('-d, --depth <n>', 'set the depth to <n>', Infinity)
.parse(process.argv);
// component.json required
if (!exists('component.json')) utils.fatal('missing component.json');
// output tree
var tree = tree('component.json');
console.log();
console.log(indent(archy(tree)));
/**
* Return the dependency tree of the given config `file`.
*
* @param {String} file
* @param {Array} paths
* @return {Object}
*/
function tree(file, paths, depth){
depth = depth || 0;
var conf = require(resolve(file));
var deps = Object.keys(conf.dependencies || {});
var node = {};
// local deps
if (conf.local) deps = deps.concat(conf.local);
// lookup paths
paths = (paths || ['components']).concat(conf.paths);
// label
node.label = '\033[36m' + conf.name + '\033[m';
node.label += ' \033[90m' + (conf.version || '') + '\033[m';
// --depth n
if (++depth > program.depth) return node;
// dependencies
node.nodes = deps.map(function(dep){
var dep = dep.replace('/', '-');
var file = lookup(dep, paths);
if (!file) throw new Error('failed to resolve location of "' + dep + '"');
file = resolve(file, 'component.json');
return tree(file, paths, depth);
});
return node;
}
/**
* Indent `str`.
*/
function indent(str) {
return str.replace(/^/gm, ' ');
}