Skip to content

Commit

Permalink
feat: support stringified license data
Browse files Browse the repository at this point in the history
  • Loading branch information
gabidobo committed Jan 27, 2023
1 parent f60175b commit e1b8838
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/graph/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,32 +182,38 @@ const addDependencyGraphData = ({

if (currentPackageData) {
let license;
let licenseData = currentPackageData.license;

if (typeof currentPackageData.license === 'string') {
try {
licenseData = JSON.parse(license);
// eslint-disable-next-line no-empty
} catch (error) {}

if (typeof licenseData === 'string') {
// Standard SPDX field
license = currentPackageData.license;
} else if (Array.isArray(currentPackageData.licenses)) {
license = licenseData;
} else if (Array.isArray(licenseData)) {
// Some older packages use an array
// {
// "licenses" : [
// {"type": "MIT", "url": "..."},
// {"type": "Apache-2.0", "url": "..."}
// ]
// }
if (currentPackageData.licenses.length === 1) {
license = currentPackageData.licenses[0].type;
if (licenseData.length === 1) {
license = licenseData[0].type;
} else {
license = `(${currentPackageData.licenses.map(({type}) => type).join(' OR ')})`;
license = `(${licenseData.map(({type}) => type).join(' OR ')})`;
}
} else if (typeof currentPackageData.license === 'object') {
} else if (typeof licenseData === 'object') {
// Some older packages use an object
// {
// "license" : {
// "type" : "ISC",
// "url" : "..."
// }
// }
license = currentPackageData.license.type;
license = licenseData.type;
}

Object.assign(root, {
Expand Down

0 comments on commit e1b8838

Please sign in to comment.