Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for dependency tree rendering #4

Merged
merged 1 commit into from Feb 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
"test": "grunt test"
},
"dependencies": {
"archy": "^1.0.0",
"bower": "^1.3.12",
"bower-config": "^0.5.2",
"chalk": "^0.5.1",
"inquirer": "^0.8.0",
"mout": "^0.11.0"
},
Expand Down
76 changes: 74 additions & 2 deletions tasks/lib/GruntLogListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

'use strict';

var inquirer = require('inquirer');
var archy = require('archy'),
chalk = require('chalk'),
inquirer = require('inquirer'),
mout = require('mout');

var GruntLogListener = function (grunt) {
this.grunt = grunt;
Expand All @@ -19,7 +22,12 @@ var GruntLogListener = function (grunt) {
this.grunt.event.on('bower.prompt', this.prompt.bind(this));
};

GruntLogListener.prototype.end = function (data) {
GruntLogListener.prototype.end = function (data, task) {
if (task === 'list') {
// print bower dependency tree if 'list' is the command
this.grunt.log.oklns("dependency tree:");
console.log(archy(tree2archy(data)));
}
this.grunt.log.oklns("Bower command finished.");
};

Expand Down Expand Up @@ -51,5 +59,69 @@ GruntLogListener.prototype.prompt = function (prompts, callback) {
inquirer.prompt(prompts, callback);
};

/**
* the _tree2archy function, taken directly from bower source
* https://github.com/bower/bower/blob/master/lib/renderers/StandardRenderer.js#L424-480
*
* bower renderers are not a separate module, or else we wouldn't need this,
* see: https://github.com/bower/bower/issues/874
*/
function tree2archy (node) {
var dependencies = mout.object.values(node.dependencies);
var version = !node.missing ? node.pkgMeta._release || node.pkgMeta.version : null;
var label = node.endpoint.name + (version ? '#' + version : '');
var update;

if (node.root) {
label += ' ' + node.canonicalDir;
}

// State labels
if (node.missing) {
label += chalk.red(' not installed');
return label;
}

if (node.different) {
label += chalk.red(' different');
}

if (node.linked) {
label += chalk.magenta(' linked');
}

if (node.incompatible) {
label += chalk.yellow(' incompatible') + ' with ' + node.endpoint.target;
} else if (node.extraneous) {
label += chalk.green(' extraneous');
}

// New versions
if (node.update) {
update = '';

if (node.update.target && node.pkgMeta.version !== node.update.target) {
update += node.update.target + ' available';
}

if (node.update.latest !== node.update.target) {
update += (update ? ', ' : '');
update += 'latest is ' + node.update.latest;
}

if (update) {
label += ' (' + chalk.cyan(update) + ')';
}
}

if (!dependencies.length) {
return label;
}

return {
label: label,
nodes: mout.object.values(dependencies).map(tree2archy)
};
}

module.exports = GruntLogListener;