Skip to content

Commit

Permalink
refactor: Provide a better name for a variable
Browse files Browse the repository at this point in the history
  • Loading branch information
RSeidelsohn committed Apr 15, 2023
1 parent ddf7cd7 commit c5a35f3
Showing 1 changed file with 46 additions and 24 deletions.
70 changes: 46 additions & 24 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ debugLog.log = console.log.bind(console);
// second iteration, it collects the data from all direct dependencies, then it collects their dependencies and so on.
const recursivelyCollectAllDependencies = (options) => {
const moduleInfo = { licenses: LICENSE_TITLE_UNKNOWN };
const { color: colorize, deps: json, unknown } = options;
const currentPackageNameAndVersion = `${json.name}@${json.version}`;
const { color: colorize, deps: currentExtendedPackageJson, unknown } = options;
const currentPackageNameAndVersion = `${currentExtendedPackageJson.name}@${currentExtendedPackageJson.version}`;
let licenseFilesInCurrentModuleDirectory = [];
let licenseData;
let licenseFile;
Expand All @@ -52,11 +52,14 @@ const recursivelyCollectAllDependencies = (options) => {
return data;
}

if ((options.production && json.extraneous) || (options.development && !json.extraneous && !json.root)) {
if (
(options.production && currentExtendedPackageJson.extraneous) ||
(options.development && !currentExtendedPackageJson.extraneous && !currentExtendedPackageJson.root)
) {
return data;
}

if (json.private) {
if (currentExtendedPackageJson.private) {
moduleInfo.private = true;
}

Expand All @@ -68,7 +71,7 @@ const recursivelyCollectAllDependencies = (options) => {
if (mustInclude('repository')) {
const repositoryUrl = helpers.getRepositoryUrl({
clarificationRepository: clarification?.respository,
jsonRepository: json?.respository,
jsonRepository: currentExtendedPackageJson?.respository,
});

if (repositoryUrl) {
Expand All @@ -77,17 +80,20 @@ const recursivelyCollectAllDependencies = (options) => {
}

if (mustInclude('url')) {
// TODO: Figure out where the check for json.url.web comes from. It's in the original license-checker,
// TODO: Figure out where the check for currentExtendedPackageJson.url.web comes from. It's in the original license-checker,
// but I can't find any documentation on it.
let url = helpers.getFirstNotUndefinedOrUndefined(clarification?.url, json?.url?.web);
let url = helpers.getFirstNotUndefinedOrUndefined(clarification?.url, currentExtendedPackageJson?.url?.web);
/*istanbul ignore next*/
if (url) {
moduleInfo.url = url;
}
}

if (typeof json.author === 'object') {
const { publisher, email, url } = helpers.getAuthorDetails({ clarification, author: json?.author });
if (typeof currentExtendedPackageJson.author === 'object') {
const { publisher, email, url } = helpers.getAuthorDetails({
clarification,
author: currentExtendedPackageJson?.author,
});

if (mustInclude('publisher') && publisher) {
moduleInfo.publisher = publisher;
Expand All @@ -97,7 +103,7 @@ const recursivelyCollectAllDependencies = (options) => {
moduleInfo.email = email;
}

// moduleInfo.url can for some reason already be set to json.url.web further up in the code,
// moduleInfo.url can for some reason already be set to currentExtendedPackageJson.url.web further up in the code,
// so we only set it if it's not already set.
if (typeof moduleInfo.url !== 'undefined' && mustInclude('url') && url) {
moduleInfo.url = url;
Expand All @@ -106,27 +112,37 @@ const recursivelyCollectAllDependencies = (options) => {

/*istanbul ignore next*/
if (unknown) {
moduleInfo.dependencyPath = json.path;
moduleInfo.dependencyPath = currentExtendedPackageJson.path;
}

const modulePath = helpers.getFirstNotUndefinedOrUndefined(clarification?.path, json?.path);
const modulePath = helpers.getFirstNotUndefinedOrUndefined(clarification?.path, currentExtendedPackageJson?.path);
if (mustInclude('path') && typeof modulePath === 'string') {
moduleInfo.path = modulePath;
}

licenseData = helpers.getFirstNotUndefinedOrUndefined(clarification?.licenses, json.license, json.licenses);

if (modulePath && (!json.readme || json.readme.toLowerCase().indexOf('no readme data found') > -1)) {
if (
modulePath &&
(!currentExtendedPackageJson.readme ||
currentExtendedPackageJson.readme.toLowerCase().indexOf('no readme data found') > -1)
) {
readmeFile = path.join(modulePath, 'README.md');
/*istanbul ignore if*/
if (fs.existsSync(readmeFile)) {
json.readme = fs.readFileSync(readmeFile, 'utf8').toString();
currentExtendedPackageJson.readme = fs.readFileSync(readmeFile, 'utf8').toString();
}
}

// console.log('licenseData: %s', licenseData);

// Try to get the license information from the clarification file or from the package.json file:
licenseData = helpers.getFirstNotUndefinedOrUndefined(
clarification?.licenses,
currentExtendedPackageJson.license,
currentExtendedPackageJson.licenses,
);

if (licenseData) {
// License information has been collected from either the clarifiation file or from the package.json file
/*istanbul ignore else*/
if (Array.isArray(licenseData) && licenseData.length > 0) {
moduleInfo.licenses = licenseData.map((moduleLicense) => {
Expand All @@ -149,8 +165,10 @@ const recursivelyCollectAllDependencies = (options) => {
} else if (typeof licenseData === 'string') {
moduleInfo.licenses = getLicenseTitle(licenseData);
}
} else if (getLicenseTitle(json.readme)) {
moduleInfo.licenses = getLicenseTitle(json.readme);
} else if (getLicenseTitle(currentExtendedPackageJson.readme)) {
// Try to get the license information from the README file if neither the clarification file nor the package.json
// file contained any license information:
moduleInfo.licenses = getLicenseTitle(currentExtendedPackageJson.readme);
}

if (Array.isArray(moduleInfo.licenses)) {
Expand Down Expand Up @@ -291,18 +309,20 @@ const recursivelyCollectAllDependencies = (options) => {

// TODO: How do clarifications interact with notice files?
noticeFiles.forEach((filename) => {
const file = path.join(json.path, filename);
const file = path.join(currentExtendedPackageJson.path, filename);
/*istanbul ignore else*/
if (fs.lstatSync(file).isFile()) {
moduleInfo.noticeFile = options.basePath ? path.relative(options.basePath, file) : file;
}
});

/*istanbul ignore else*/
if (json.dependencies) {
Object.keys(json.dependencies).forEach((dependencyName) => {
if (currentExtendedPackageJson.dependencies) {
Object.keys(currentExtendedPackageJson.dependencies).forEach((dependencyName) => {
const childDependency =
options.currentRecursionDepth > options._args.direct ? {} : json.dependencies[dependencyName];
options.currentRecursionDepth > options._args.direct
? {}
: currentExtendedPackageJson.dependencies[dependencyName];
const dependencyId = `${childDependency.name}@${childDependency.version}`;

if (data[dependencyId]) {
Expand All @@ -326,7 +346,7 @@ const recursivelyCollectAllDependencies = (options) => {
});
}

if (!json.name || !json.version) {
if (!currentExtendedPackageJson.name || !currentExtendedPackageJson.version) {
delete data[currentPackageNameAndVersion];
}

Expand All @@ -336,7 +356,9 @@ const recursivelyCollectAllDependencies = (options) => {
if (mustInclude(item) && moduleInfo[item] == null) {
moduleInfo[item] = helpers.getFirstNotUndefinedOrUndefined(
clarification?.[item],
typeof json[item] === 'string' ? json[item] : options.customFormat[item],
typeof currentExtendedPackageJson[item] === 'string'
? currentExtendedPackageJson[item]
: options.customFormat[item],
);
}
});
Expand Down

0 comments on commit c5a35f3

Please sign in to comment.