diff --git a/lib/commands/ls.js b/lib/commands/ls.js index 623dac8..2299af3 100644 --- a/lib/commands/ls.js +++ b/lib/commands/ls.js @@ -4,7 +4,9 @@ var utils = require('../utils'), couchdb = require('../couchdb'), kansorc = require('../kansorc'), argParse = require('../args').parse, - path = require('path'); + path = require('path'), + tree = require('../tree'), + install = require('./install'); exports.summary = 'Builds a project and reports a list of its exports'; @@ -31,6 +33,7 @@ exports.usage = '' + ' --rewrites List rewrites (by "from" pattern)\n' + ' --validate Test whether validate_doc_update exists\n' + ' --size Calculate size of design doc including dependencies\n' + +' --dependencies List dependencies\n' + '\n' + 'If no options are specified, shows all information'; @@ -89,6 +92,32 @@ function deep_print(obj, ignores, path) { return str; } +/** + * Prints a hierarchy. Parameter 2 and 3 should not be passed by user code. + * @param obj an object with a structure like: {title: 'root', children: [{title: 'ch1', children: []}, {title: 'ch2', children: []}]} + */ +function hierarchy_print(obj, thislevellog, nextlevellog) { + var joinedprint = function () { + console.log(Array.prototype.slice.call(arguments).join('')); + }; + thislevellog = thislevellog || joinedprint; + nextlevellog = nextlevellog || joinedprint; + + if (typeof obj.title !== 'undefined') { + if (obj.children.length > 0) { + thislevellog(logger.yellow('┬─ ') + obj.title); + } else { + thislevellog(logger.yellow('── ') + obj.title); + } + } + for (var i = 0; i < obj.children.length - 1; i++) { + hierarchy_print(obj.children[i], nextlevellog.bind(null, logger.yellow('├─')), nextlevellog.bind(null, logger.yellow('│ '))); + } + if (obj.children.length > 0) { + hierarchy_print(obj.children[obj.children.length - 1], nextlevellog.bind(null, logger.yellow('└─')), nextlevellog.bind(null, ' ')); + } +}; + exports.run = function (settings, args) { var options = { name: {match: '--name'}, @@ -106,6 +135,7 @@ exports.run = function (settings, args) { rewrites: {match: '--rewrites'}, validate: {match: '--validate'}, size: {match: '--size'}, + dependencies: {match: '--dependencies'}, }; var a = argParse(args, options); var dir = utils.abspath(a.positional[0] || '.'); @@ -233,8 +263,32 @@ exports.run = function (settings, args) { ); } - //logger.clean_exit = true; - logger.end(); + if (!custom || a.options.dependencies) { + console.log(logger.bold('Dependencies')); + + tree.build({config: cfg, source: 'root'}, [install.dirSource(path.join(dir, 'packages'))], function (err, packages) { + var mypkgs = []; + var attach_deps = function (key, obj, ptree) { + for (var k in ptree) { + if (ptree.hasOwnProperty(k) && ptree[k].ranges && ptree[k].ranges.hasOwnProperty(key)) { + obj.push({ + "title" : logger.cyan(k) + (ptree[k].ranges[key] ? logger.cyan('(' + ptree[k].ranges[key] + ')') : '') + '@' + ptree[k].current_version, + "children": [] + }); + attach_deps(k, obj[obj.length -1 ].children, ptree); + } + } + }; + attach_deps(cfg.name, mypkgs, packages); + + hierarchy_print({children: mypkgs}); + + console.log(''); + logger.end(); + }); + } else { + logger.end(); + } }); }); };