diff --git a/lib/index.js b/lib/index.js index fbe72a2..6fc3b02 100644 --- a/lib/index.js +++ b/lib/index.js @@ -185,7 +185,7 @@ const flatten = function flatten(options) { }); } - files.forEach(function(filename, index) { + files.forEach(function (filename, index) { licenseFile = path.join(module_path, filename); // Checking that the file is in fact a normal file and not a directory for example. /*istanbul ignore else*/ @@ -393,7 +393,18 @@ const removeUnwantedDependencies = (json, args) => { } }; -exports.init = function init(args, callback) { +function prepareArgs(args) { + const normalizedArgs = { ...args }; + if (args.direct === true) { + normalizedArgs.direct = 0; + } else if (typeof args.direct !== 'number') { + normalizedArgs.direct = Infinity; + } + return normalizedArgs; +} + +exports.init = function init(initArgs, callback) { + const args = prepareArgs(initArgs); // Fix path if on Windows: const workingDir = args.start.replace(/\\\\/g, '\\'); diff --git a/tests/test.js b/tests/test.js index fa7f561..c04d3e4 100644 --- a/tests/test.js +++ b/tests/test.js @@ -5,6 +5,8 @@ const checker = require('../lib/index'); let args = require('../lib/args'); const chalk = require('chalk'); const fs = require('fs'); +const pkgPath = path.join(__dirname, '../package.json'); +const pkgJson = require(pkgPath); describe('main tests', function () { it('should load init', function () { @@ -140,6 +142,34 @@ describe('main tests', function () { }); }); + describe('should parse direct dependencies only', function () { + let output; + + before(function (done) { + checker.init( + { + start: path.join(__dirname, '../'), + direct: true, + }, + function (err, sorted) { + output = sorted; + done(); + }, + ); + }); + + it('and give us results', function () { + const pkgDepsNumber = + Object.keys(pkgJson.dependencies || {}).length + + Object.keys(pkgJson.devDependencies || {}).length + + Object.keys(pkgJson.peerDependencies || {}).length; + // all and only the dependencies listed in the package.json should be included in the output, + // plus the main module itself + assert.ok(Object.keys(output).length === pkgDepsNumber + 1); + assert.equal(output['abbrev@1.0.9'], undefined); + }); + }); + function parseAndExclude(parsePath, licenses, result) { return function (done) { checker.init(