Skip to content

Commit

Permalink
Merge pull request #31 from RSeidelsohn/feature/0006_fix-direct-option
Browse files Browse the repository at this point in the history
Fix `--direct` option
  • Loading branch information
RSeidelsohn authored Feb 2, 2022
2 parents ec35025 + f0f8642 commit 7428572
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
4 changes: 2 additions & 2 deletions bin/license-checker-rseidelsohn
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ function colorizeOutput(json) {
Object.keys(json).forEach((key) => {
const index = key.lastIndexOf('@');
const colorizedKey =
chalk.white.bgKeyword('darkslategrey')(key.substr(0, index)) +
chalk.white.bgKeyword('darkslategrey')(key.slice(0, index + 1)) +
chalk.dim('@') +
chalk.white.bgKeyword('green')(key.substr(index + 1));
chalk.white.bgKeyword('green')(key.slice(index + 1));
json[colorizedKey] = json[key];

delete json[key];
Expand Down
39 changes: 38 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ const flatten = function flatten(options) {
/*istanbul ignore else*/
if (json.dependencies) {
Object.keys(json.dependencies).forEach((name) => {
const childDependency = json.dependencies[name];
const childDependency = options.depth > options._args.direct ? {} : json.dependencies[name];
const dependencyId = `${childDependency.name}@${childDependency.version}`;

if (data[dependencyId]) {
Expand All @@ -258,6 +258,7 @@ const flatten = function flatten(options) {
development: options.development,
production: options.production,
unknown,
depth: options.depth + 1,
});
});
}
Expand All @@ -278,6 +279,38 @@ const flatten = function flatten(options) {
return data;
};

/**
* ! This function has a wanted sideeffect, as it modifies the json object that is passed by reference.
*
* The detph attribute set in the opts parameter here - which is defined by setting the `--direct` flag - is of
* no use with npm > 2, as the newer npm versions flatten all dependencies into one single directory. So in
* order to making `--direct` work with newer versions of npm, we need to filter out all non-dependencies from
* the json result.
*/
const removeUnwantedDependencies = (json, args) => {
if (args.direct === 0) {
const allDependencies = Object.keys(json.dependencies);
let wantedDependencies = [];

if (args.production && !args.development) {
const devDependencies = Object.keys(json.devDependencies);
wantedDependencies = Object.keys(json._dependencies).filter(
(directDependency) => !devDependencies.includes(directDependency),
);
} else if (!args.production && args.development) {
wantedDependencies = Object.keys(json.devDependencies);
} else {
wantedDependencies = Object.keys(json._dependencies);
}

allDependencies.forEach((currentDependency) => {
if (!wantedDependencies.includes(currentDependency)) {
delete json.dependencies[currentDependency];
}
});
}
};

exports.init = function init(args, callback) {
// Fix path if on Windows:
const workingDir = args.start.replace(/\\\\/g, '\\');
Expand Down Expand Up @@ -325,6 +358,8 @@ exports.init = function init(args, callback) {
}

read(args.start, opts, (err, json) => {
removeUnwantedDependencies(json, args);

const data = flatten({
_args: args,
basePath: args.relativeLicensePath ? json.path : null,
Expand All @@ -335,7 +370,9 @@ exports.init = function init(args, callback) {
development: args.development,
production: args.production,
unknown: args.unknown,
depth: 0,
});

const colorize = args.color;
const sorted = {};
let filtered = {};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "license-checker-rseidelsohn",
"description": "Feature enhanced version of the original license-checker v25.0.1",
"author": "Roman Seidelsohn <[email protected]>",
"version": "3.0.0",
"version": "3.0.1",
"license": "BSD-3-Clause",
"contributors": [
"Adam Weber <[email protected]>",
Expand Down

0 comments on commit 7428572

Please sign in to comment.