Skip to content

Commit

Permalink
refactor: Improve readability
Browse files Browse the repository at this point in the history
By

- using more speaking variable names
- deleting the superfluous variable "filtered"
- using clearer type checking
  • Loading branch information
Roman Seidelsohn committed Apr 28, 2023
1 parent f3755fa commit edca92f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 37 deletions.
78 changes: 43 additions & 35 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,9 @@ exports.init = function init(args, callback) {
helpers.deleteNonDirectDependenciesFromAllDependencies(installedPackagesJson, args);
}

const data = recursivelyCollectAllDependencies({
// 'allWantedDepthDependenciesWithVersions' might be longer than 'installedPackagesJson.dependencies', as it appends the version numbers to each key (package name),
// e.g. 'grunt@1' instead of 'grunt', and this way contains all different installed versions of each package:
let allWantedDepthDependenciesWithVersions = recursivelyCollectAllDependencies({
_args: args,
basePath: args.relativeLicensePath ? installedPackagesJson.path : null,
color: args.color,
Expand All @@ -441,8 +443,8 @@ exports.init = function init(args, callback) {
});

const colorize = args.color;
const sorted = {};
let filtered = {};
const sorted = {}; // 'sorted' will store the same items as allWantedDepthDependenciesWithVersions, but sorted by package name and version
let resultJson = {};
const excludeLicenses =
args.excludeLicenses &&
args.excludeLicenses
Expand Down Expand Up @@ -602,107 +604,113 @@ exports.init = function init(args, callback) {
};

// This following block stores the licenses in the sorted object (before, the sorted object is the empty object):
Object.keys(data)
Object.keys(allWantedDepthDependenciesWithVersions)
.sort()
.forEach((item) => {
if (data[item].private) {
data[item].licenses = colorizeString(LICENSE_TITLE_UNLICENSED);
if (allWantedDepthDependenciesWithVersions[item].private) {
allWantedDepthDependenciesWithVersions[item].licenses = colorizeString(LICENSE_TITLE_UNLICENSED);
}

/*istanbul ignore next*/
if (!data[item].licenses) {
data[item].licenses = colorizeString(LICENSE_TITLE_UNKNOWN);
if (!allWantedDepthDependenciesWithVersions[item].licenses) {
allWantedDepthDependenciesWithVersions[item].licenses = colorizeString(LICENSE_TITLE_UNKNOWN);
}

if (
args.unknown &&
data[item].licenses &&
data[item].licenses !== LICENSE_TITLE_UNKNOWN &&
data[item].licenses.indexOf('*') > -1
allWantedDepthDependenciesWithVersions[item].licenses &&
allWantedDepthDependenciesWithVersions[item].licenses !== LICENSE_TITLE_UNKNOWN &&
allWantedDepthDependenciesWithVersions[item].licenses.indexOf('*') > -1
) {
/*istanbul ignore if*/
data[item].licenses = colorizeString(LICENSE_TITLE_UNKNOWN);
allWantedDepthDependenciesWithVersions[item].licenses = colorizeString(LICENSE_TITLE_UNKNOWN);
}
/*istanbul ignore else*/
if (data[item]) {
if (args.relativeModulePath && data[item].path != null) {
data[item].path = path.relative(args.start, data[item].path);
if (allWantedDepthDependenciesWithVersions[item]) {
if (args.relativeModulePath && allWantedDepthDependenciesWithVersions[item].path != null) {
allWantedDepthDependenciesWithVersions[item].path = path.relative(
args.start,
allWantedDepthDependenciesWithVersions[item].path,
);
}

if (args.onlyunknown) {
if (
data[item].licenses.indexOf('*') > -1 ||
data[item].licenses.indexOf(LICENSE_TITLE_UNKNOWN) > -1
allWantedDepthDependenciesWithVersions[item].licenses.indexOf('*') > -1 ||
allWantedDepthDependenciesWithVersions[item].licenses.indexOf(LICENSE_TITLE_UNKNOWN) > -1
) {
sorted[item] = data[item];
sorted[item] = allWantedDepthDependenciesWithVersions[item];
}
} else {
sorted[item] = data[item];
sorted[item] = allWantedDepthDependenciesWithVersions[item];
}
}
});

// 'allWantedDepthDependenciesWithVersions' is not needed anymore:
allWantedDepthDependenciesWithVersions = null;

if (!Object.keys(sorted).length) {
err = new Error('No packages found in this path...');
}

// This following block stores the licenses in the filtered object (before, the filtered object is the empty object):
if (excludeLicenses || includeLicenses) {
if (excludeLicenses) {
// This following block stores the entries from the 'sorted' object in the
// resultJson object (before, the resultJson object is the empty object):
if (
(!Array.isArray(excludeLicenses) || excludeLicenses.length === 0) &&
(!Array.isArray(includeLicenses) || includeLicenses.length === 0)
) {
resultJson = { ...sorted };
} else {
if (Array.isArray(excludeLicenses) && excludeLicenses.length > 0) {
Object.entries(sorted).forEach(([packageName, packageData]) => {
let { licenses } = packageData;

/*istanbul ignore if - just for protection*/
if (!licenses) {
filtered[packageName] = packageData;
resultJson[packageName] = packageData;
} else {
const licensesArr = Array.isArray(licenses) ? licenses : [licenses];
const licenseMatch = getLicenseMatch(
licensesArr,
filtered,
resultJson,
packageName,
packageData,
excludeLicenses,
);

if (!licenseMatch) {
filtered[packageName] = packageData;
resultJson[packageName] = packageData;
}
}
});
}

if (includeLicenses) {
if (Array.isArray(includeLicenses) && includeLicenses.length > 0) {
Object.entries(sorted).forEach(([packageName, packageData]) => {
let { licenses } = packageData;

/*istanbul ignore if - just for protection*/
if (!licenses) {
filtered[packageName] = packageData;
resultJson[packageName] = packageData;
} else {
const licensesArr = Array.isArray(licenses) ? licenses : [licenses];
const licenseMatch = getLicenseMatch(
licensesArr,
filtered,
resultJson,
packageName,
packageData,
includeLicenses,
);

if (licenseMatch) {
filtered[packageName] = packageData;
resultJson[packageName] = packageData;
}
}
});
}
} else {
filtered = { ...sorted };
}

// TODO: This is bullshit - the precedent block could as well just store the result in the resultJson variable
// directly rather than in the filtered variable and then copying the filtered variable to the resultJson:
let resultJson = { ...filtered };

// package whitelist
const whitelist = getOptionArray(args.includePackages);
if (whitelist) {
Expand Down
4 changes: 2 additions & 2 deletions lib/licenseCheckerHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ const filterJson = function filterJson(limitAttributes, json) {
return filteredJson;
};

const getFormattedOutput = function getFormattedOutput(json, args) {
let filteredJson = filterJson(args.limitAttributes, json);
const getFormattedOutput = function getFormattedOutput(modulesWithVersions, args) {
let filteredJson = filterJson(args.limitAttributes, modulesWithVersions);
const jsonCopy = cloneDeep(filteredJson);
filteredJson = null;

Expand Down

0 comments on commit edca92f

Please sign in to comment.