Skip to content

Commit

Permalink
fix: parse direct also inside init to make it work in programmatic usage
Browse files Browse the repository at this point in the history
  • Loading branch information
beawar committed Feb 27, 2023
1 parent d95e43e commit d61bc3b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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*/
Expand Down Expand Up @@ -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, '\\');

Expand Down
30 changes: 30 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit d61bc3b

Please sign in to comment.