Skip to content

Commit

Permalink
Merge e009ed6 into 62f8ba3
Browse files Browse the repository at this point in the history
  • Loading branch information
aleung committed Nov 25, 2015
2 parents 62f8ba3 + e009ed6 commit 57d7ab7
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 22 deletions.
4 changes: 3 additions & 1 deletion bin/nlf-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ var program = require('commander'),
program
.version(pjson.version)
.option('-d, --no-dev', 'exclude development dependencies')
.option('-s, --summary <mode>', 'summary (not available in csv format): off | simple (default) | detail', /^(off|simple|detail)$/i, 'simple')
.option('-c, --csv', 'output in csv format')
.option('-r, --reach [num]', 'package depth (reach)', parseInt, Infinity)
.parse(process.argv);

options.production = !program.dev;
options.depth = program.reach;
options.summaryMode = program.summary;

// select which formatter
if (program.csv) {
Expand All @@ -46,7 +48,7 @@ nlf.find(options, function (err, data) {
}

if (data && data.length > 0) {
format.render(data, function (err, output) {
format.render(data, options, function (err, output) {
if (err) {
console.error(err);
process.exit(1);
Expand Down
3 changes: 2 additions & 1 deletion lib/formatters/csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ function recordToCsv(moduleRecord) {
* Render the license data
*
* @param {Array} licenseData An array of module licence data
* @param {Object} options Options
* @param {Function} callback The callback (err, output string)
*/
function render(licenseData, callback) {
function render(licenseData, options, callback) {

if (typeof callback !== 'function') {
throw new Error('must have a callback');
Expand Down
49 changes: 41 additions & 8 deletions lib/formatters/standard.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,44 @@ function createModuleNode(moduleData) {
/**
* Create an archy formatted summary of the licenses found
*
* @param {Array} summaryData Array of licenses found
* @param {Object} summaryData Map: license -> modules
*/
function createSummary(summaryData) {
function createSummary(summmaryData) {
return archy({
label: 'LICENSES: ' + summaryData.sort().join(', ')
label: 'LICENSES: ' + Object.keys(summmaryData).sort().join(', ')
});
}


/**
* Create an archy output of the modules group by license
*
* @param {Object} summaryData Map: license -> modules
*/
function modulesByLicenses(summaryData) {
var output = {
label: 'LICENSES:',
nodes: []
};
for (var license in summaryData) {
if (summaryData.hasOwnProperty(license)) {
output.nodes.push({
label: license,
nodes: summaryData[license]
});
}
}
return archy(output);
}

/**
* Render the license data
*
* @param {Array} licenseData An array of module licence data
* @param {Object} options Options
* @param {Function} callback The callback (err, output string)
*/
function render(licenseData, callback) {
function render(licenseData, options, callback) {

if (typeof callback !== 'function') {
throw new Error('must have a callback');
Expand All @@ -76,7 +100,7 @@ function render(licenseData, callback) {
}

var output = [];
var summary = [];
var summary = {};

// go through all the modules, adding them to
// the output archy formatted data
Expand All @@ -85,14 +109,23 @@ function render(licenseData, callback) {
// add new licenses to the summary
var moduleLicenses = module.summary();
for (var index = 0; index < moduleLicenses.length; index++) {
if (summary.indexOf(moduleLicenses[index]) === -1) {
summary.push(moduleLicenses[index]);
var license = moduleLicenses[index];
if (!summary[license]) {
summary[license] = [];
}
summary[license].push(module.id);
}
});

// add the summary
output.push(createSummary(summary));
switch (options.summaryMode) {
case 'simple':
output.push(createSummary(summary));
break;
case 'detail':
output.push(modulesByLicenses(summary));
break;
}

callback(null, output.join('\n'));
}
Expand Down
10 changes: 10 additions & 0 deletions lib/nlf.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,14 @@ function convertToArray(object) {
return output;
}

function processOptionSummaryMode(options) {
options.summaryMode = options.summaryMode || 'simple';
if (typeof options.summaryMode !== 'string') {
throw new Error('options.summaryMode must be a string');
}
options.summaryMode = options.summaryMode.toLowerCase();
}

/**
* Process the options
*
Expand All @@ -404,6 +412,8 @@ function processOptions(options) {
if (typeof options.production !== 'boolean') {
throw new Error('options.production must be a boolean');
}

processOptionSummaryMode(options);

return options;
}
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ nlf can be used programmatically, or from the command line.
- `directory` (String) - where to look
- `production` (Boolean) (Default:false) - only traverse dependencies, no dev-dependencies
- `depth` (Number) (Default: Infinity) - how deep to traverse packages where 0 is the current package.json only
- `summaryMode` (String: off|simple|detail) (Default: simple)


### CLI
Expand Down
12 changes: 6 additions & 6 deletions test/unit/formatters/csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('csv formatter', function () {

it('should return an error', function () {

csvFormat.render(undefined, function (err) {
csvFormat.render(undefined, {}, function (err) {

err.should.be.an.object;

Expand All @@ -69,19 +69,19 @@ describe('csv formatter', function () {

it('should return an error', function () {

csvFormat.render(1, function (err) {
csvFormat.render(1, {}, function (err) {

err.should.be.an.object;

});

csvFormat.render(true, function (err) {
csvFormat.render(true, {}, function (err) {

err.should.be.an.object;

});

csvFormat.render('cats', function (err) {
csvFormat.render('cats', {}, function (err) {

err.should.be.an.object;

Expand All @@ -95,7 +95,7 @@ describe('csv formatter', function () {

it('should return an error', function () {

csvFormat.render([], function (err) {
csvFormat.render([], {}, function (err) {

err.should.be.an.object;

Expand All @@ -113,7 +113,7 @@ describe('csv formatter', function () {
throw err;
}

csvFormat.render(input, function (err, output) {
csvFormat.render(input, {}, function (err, output) {

if (err)
{
Expand Down
51 changes: 45 additions & 6 deletions test/unit/formatters/standard.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var standardFormat = require('../../..').standardFormatter,
input = [],
mod,
path = require('path'),
expectedWithDatailSummary,
expected;

require('should');
Expand All @@ -34,6 +35,16 @@ expected = 'test@1.0.0 [license(s): Apache, MIT]\n'
+ '└── readme files: MIT\n\n'
+ 'LICENSES: Apache, MIT\n';

expectedWithDatailSummary = 'test@1.0.0 [license(s): Apache, MIT]\n'
+ '├── package.json: Apache\n'
+ '├── license files: MIT\n'
+ '└── readme files: MIT\n\n'
+ 'LICENSES:\n'
+ '├─┬ Apache\n'
+ '│ └── test@1.0.0\n'
+ '└─┬ MIT\n'
+ ' └── test@1.0.0\n';

describe('standard formatter', function () {

describe('render method', function () {
Expand Down Expand Up @@ -66,7 +77,7 @@ describe('standard formatter', function () {

it('should return an error', function () {

standardFormat.render(undefined, function (err) {
standardFormat.render(undefined, {}, function (err) {

err.should.be.an.object;

Expand All @@ -80,19 +91,19 @@ describe('standard formatter', function () {

it('should return an error', function () {

standardFormat.render(1, function (err) {
standardFormat.render(1, {}, function (err) {

err.should.be.an.object;

});

standardFormat.render(true, function (err) {
standardFormat.render(true, {}, function (err) {

err.should.be.an.object;

});

standardFormat.render('cats', function (err) {
standardFormat.render('cats', {}, function (err) {

err.should.be.an.object;

Expand All @@ -106,7 +117,7 @@ describe('standard formatter', function () {

it('should return an error', function () {

standardFormat.render([], function (err) {
standardFormat.render([], {}, function (err) {

err.should.be.an.object;

Expand All @@ -127,7 +138,8 @@ describe('standard formatter', function () {
if (err) {
throw err;
}
standardFormat.render(input, function (err, output) {
standardFormat.render(input, {summaryMode: 'simple'},
function (err, output) {

if (err)
{
Expand All @@ -140,5 +152,32 @@ describe('standard formatter', function () {
});
});
});

it('should return detail summary', function (done) {

mod.licenseSources.license.sources[0].read(function (err) {
if (err) {
throw err;
}

mod.licenseSources.readme.sources[0].read(function (err) {
if (err) {
throw err;
}
standardFormat.render(input, {summaryMode: 'detail'},
function (err, output) {

if (err)
{
throw err;
}

output.should.be.equal(expectedWithDatailSummary);
done();
});
});
});
});

});
});

0 comments on commit 57d7ab7

Please sign in to comment.