feat: extract name from license file (#7530)

This commit is contained in:
btea
2024-01-27 06:05:20 +08:00
committed by GitHub
parent e6428d85d7
commit 10a0896551
3 changed files with 76 additions and 3 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/license-scanner": minor
"pnpm": minor
---
When the license field does not exist in `package.json` but a license file exists, try to match and extract the license name [#7530](https://github.com/pnpm/pnpm/pull/7530).

View File

@@ -39,6 +39,65 @@ const LICENSE_FILES = [
'MIT-LICENSE',
]
/**
* @const
* List common license names
* Refer https://github.com/pivotal/LicenseFinder/blob/master/lib/license_finder/license/definitions.rb
*/
const LICENSE_NAMES = [
'Apache1_1',
'Apache-1.1',
'Apache 1.1',
'Apache2',
'Apache-2.0',
'Apache 2.0',
'BSD',
'BSD-4-Clause',
'CC01',
'CC0-1.0',
'CC0-1.0',
'CDDL1',
'CDDL-1.0',
'Common Development and Distribution License 1.0',
'EPL1',
'EPL-1.0',
'Eclipse Public License 1.0',
'GPLv2',
'GPL-2.0-only',
'GPLv3',
'GPL-3.0-only',
'ISC',
'LGPL',
'LGPL-3.0-only',
'LGPL2_1',
'LGPL-2.1-only',
'MIT',
'MPL1_1',
'MPL-1.1',
'Mozilla Public License 1.1',
'MPL2',
'MPL-2.0',
'Mozilla Public License 2.0',
'NewBSD',
'BSD-3-Clause',
'New BSD',
'OFL',
'OFL-1.1',
'SIL OPEN FONT LICENSE Version 1.1',
'Python',
'PSF-2.0',
'Python Software Foundation License',
'Ruby',
'SimplifiedBSD',
'BSD-2-Clause',
'Simplified BSD',
'WTFPL',
'0BSD',
'BSD Zero Clause License',
'Zlib',
'zlib/libpng license',
]
export interface LicenseInfo {
name: string
licenseFile?: string
@@ -126,10 +185,18 @@ async function parseLicense (
} else {
licenseContents = await readLicenseFileFromCafs(opts.cafsDir, licensePackageFileInfo as PackageFileInfo)
}
const licenseContent = licenseContents?.toString('utf-8')
let name = 'Unknown'
if (licenseContent) {
const match = licenseContent.match(new RegExp(`\\b(${LICENSE_NAMES.join('|')})\\b`, 'igm'))
if (match) {
name = [...new Set(match)].join(' OR ')
}
}
return {
name: 'Unknown',
licenseFile: licenseContents?.toString('utf-8'),
name,
licenseFile: licenseContent,
}
}
}

View File

@@ -80,7 +80,7 @@ exports[`pnpm licenses: should correctly read LICENSE file with executable file
│ │ │ The Node.js \`util.deprecate()\` function with browser support │
│ │ │ https://github.com/TooTallNate/util-deprecate │
├──────────────────────────────┼─────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ varstream │ Unknown │ Nicolas Froidure │
│ varstream │ MIT │ Nicolas Froidure │
│ │ │ Stream variables beetween 2 JavaScript threads (client/server, ipc, worker/main thread). │
│ │ │ https://github.com/nfroidure/VarStream#readme │
└──────────────────────────────┴─────────┴────────────────────────────────────────────────────────────────────────────────────────────────────────┘