From 245f7a62d5ae03a56fa28aa2d610b152a6a37ff2 Mon Sep 17 00:00:00 2001 From: Roman Seidelsohn Date: Wed, 2 Feb 2022 13:33:52 +0100 Subject: [PATCH 1/5] chore: Replace deprecated function --- bin/license-checker-rseidelsohn | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/license-checker-rseidelsohn b/bin/license-checker-rseidelsohn index c15b4f3..181ea12 100644 --- a/bin/license-checker-rseidelsohn +++ b/bin/license-checker-rseidelsohn @@ -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]; From 33a8cb391b45b7bdf305e8ed14b8647898281e97 Mon Sep 17 00:00:00 2001 From: Roman Seidelsohn Date: Wed, 2 Feb 2022 13:34:39 +0100 Subject: [PATCH 2/5] feat: Add new helper function for removing unwanted dependencies --- lib/index.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/lib/index.js b/lib/index.js index d73917d..e266010 100644 --- a/lib/index.js +++ b/lib/index.js @@ -278,6 +278,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, '\\'); From b4f1785a309d52bf3571ad50268a8b9eb7f01325 Mon Sep 17 00:00:00 2001 From: Roman Seidelsohn Date: Wed, 2 Feb 2022 13:35:13 +0100 Subject: [PATCH 3/5] feat: Use new filter function --- lib/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/index.js b/lib/index.js index e266010..1fd4a69 100644 --- a/lib/index.js +++ b/lib/index.js @@ -357,6 +357,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, From 5b8c7e5e8c089e8df69dc7b12c199abe3af50a7a Mon Sep 17 00:00:00 2001 From: Roman Seidelsohn Date: Wed, 2 Feb 2022 13:35:49 +0100 Subject: [PATCH 4/5] fix: Respect 'direct' option correctly --- lib/index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 1fd4a69..8fe11ca 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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]) { @@ -258,6 +258,7 @@ const flatten = function flatten(options) { development: options.development, production: options.production, unknown, + depth: options.depth + 1, }); }); } @@ -369,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 = {}; From f0f86424641c8c410a5c8e904c6384e58f1adfcf Mon Sep 17 00:00:00 2001 From: Roman Seidelsohn Date: Wed, 2 Feb 2022 13:46:06 +0100 Subject: [PATCH 5/5] chore: Bump fix version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d56163a..b8f60ff 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "license-checker-rseidelsohn", "description": "Feature enhanced version of the original license-checker v25.0.1", "author": "Roman Seidelsohn ", - "version": "3.0.0", + "version": "3.0.1", "license": "BSD-3-Clause", "contributors": [ "Adam Weber ",