From c5a35f36d8655bfd0ce07078b797c7741decd833 Mon Sep 17 00:00:00 2001 From: Roman Seidelsohn Date: Sat, 15 Apr 2023 10:20:24 +0200 Subject: [PATCH] refactor: Provide a better name for a variable --- lib/index.js | 70 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 24 deletions(-) diff --git a/lib/index.js b/lib/index.js index 91de5b7..ab1119c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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; @@ -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; } @@ -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) { @@ -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; @@ -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; @@ -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) => { @@ -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)) { @@ -291,7 +309,7 @@ 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; @@ -299,10 +317,12 @@ const recursivelyCollectAllDependencies = (options) => { }); /*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]) { @@ -326,7 +346,7 @@ const recursivelyCollectAllDependencies = (options) => { }); } - if (!json.name || !json.version) { + if (!currentExtendedPackageJson.name || !currentExtendedPackageJson.version) { delete data[currentPackageNameAndVersion]; } @@ -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], ); } });