From 3c27d5fd202abf91279c7df5842112ef0373f77a Mon Sep 17 00:00:00 2001 From: "Rohith.Jidagam" Date: Thu, 22 Feb 2018 13:56:36 -0800 Subject: [PATCH] Refactored Code to avoid licenses getting from readMe (#13) * refactored code and removed bluebird and added time measure * Refactored Code. Removed bluebird in NPM. Modified Search Process * Removed ReadMe reading * Removed ReadMe reading * Formatted Code * Added Time Measure * Added filter while outputting text --- index.js | 27 +++++++++++++++++---------- package.json | 3 ++- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 6bdf2ac..e1fc796 100644 --- a/index.js +++ b/index.js @@ -32,6 +32,7 @@ var path = require('path'); var jetpack = require('fs-jetpack'); var cp = require('child_process'); var os = require('os'); +var taim = require('taim'); // const var licenseCheckerCustomFormat = { @@ -108,6 +109,7 @@ function getNpmLicenses() { } return merged; }).then((result) => { + // we want to exclude the top-level project from being included var dir = result[Object.keys(result)[0]]['dir']; var topLevelProjectInfo = jetpack.read(path.join(dir, 'package.json'), 'json'); @@ -117,6 +119,7 @@ function getNpmLicenses() { return bluebird.map(keys, (key) => { console.log('processing', key); + var package = result[key]; var defaultPackagePath = `${package['dir']}/node_modules/${package.name}/package.json` return jetpack.existsAsync(defaultPackagePath) @@ -149,7 +152,13 @@ function getNpmLicenses() { return getAttributionForAuthor(m); }).join(', ')); - props.licenseText = package.licenseFile && jetpack.exists(package.licenseFile) ? jetpack.read(package.licenseFile) : ''; + var licenseFile = package.licenseFile; + if (licenseFile && jetpack.exists(licenseFile) && path.basename(licenseFile).match(/license/i)) { + props.licenseText = jetpack.read(licenseFile); + } else { + props.licenseText = ''; + } + return props; }) .catch(e => { @@ -292,10 +301,10 @@ for (var i = 0; i < yargs.argv.baseDir.length; i++) { } -bluebird.all([ - getNpmLicenses(), +taim('Total Processing', bluebird.all([ + taim('Npm Licenses', getNpmLicenses()), getBowerLicenses() -]) +])) .catch((err) => { console.log(err); process.exit(1); @@ -324,16 +333,14 @@ bluebird.all([ }) .then((licenseInfos) => { var attribution = _.filter(licenseInfos, licenseInfo => { - return !licenseInfo.ignore; + return !licenseInfo.ignore && licenseInfo.name != undefined; }).map(licenseInfo => { - return [ - licenseInfo.name, - `${licenseInfo.version} <${licenseInfo.url}>`, - licenseInfo.licenseText || `license: ${licenseInfo.license}${os.EOL}authors: ${licenseInfo.authors}` - ].join(os.EOL); + return [licenseInfo.name,`${licenseInfo.version} <${licenseInfo.url}>`, + licenseInfo.licenseText || `license: ${licenseInfo.license}${os.EOL}authors: ${licenseInfo.authors}`].join(os.EOL); }).join(`${os.EOL}${os.EOL}******************************${os.EOL}${os.EOL}`); var headerPath = path.join(options.outputDir, 'header.txt'); + if (jetpack.exists(headerPath)) { var template = jetpack.read(headerPath); console.log('using template', template); diff --git a/package.json b/package.json index c916d25..9433d4c 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,8 @@ "license-checker": "^13.0.3", "lodash": "^4.17.4", "spdx-licenses": "^0.0.3", - "yargs": "^7.0.2" + "yargs": "^7.0.2", + "taim": "^1.0.2" }, "name": "oss-attribution-generator", "description": "utility to parse bower and npm packages used in a project and generate an attribution file to include in your product",