Skip to content
This repository has been archived by the owner on Jan 31, 2019. It is now read-only.

Commit

Permalink
Merge pull request #395 from thriqon/pretty-print-dependencies-315
Browse files Browse the repository at this point in the history
Issue #315: Nice dependency listings for packages
  • Loading branch information
mandric committed Jul 2, 2013
2 parents cbd979c + 350a40b commit 4d53d85
Showing 1 changed file with 57 additions and 3 deletions.
60 changes: 57 additions & 3 deletions lib/commands/ls.js
Expand Up @@ -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';
Expand All @@ -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';

Expand Down Expand Up @@ -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'},
Expand All @@ -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] || '.');
Expand Down Expand Up @@ -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();
}
});
});
};
Expand Down

0 comments on commit 4d53d85

Please sign in to comment.