From 10a08965515ed4e6db50d58ddcb88e355e515fbd Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Sat, 27 Jan 2024 06:05:20 +0800 Subject: [PATCH] feat: extract name from license file (#7530) --- .changeset/rich-grapes-whisper.md | 6 ++ reviewing/license-scanner/src/getPkgInfo.ts | 71 ++++++++++++++++++- .../test/__snapshots__/index.ts.snap | 2 +- 3 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 .changeset/rich-grapes-whisper.md diff --git a/.changeset/rich-grapes-whisper.md b/.changeset/rich-grapes-whisper.md new file mode 100644 index 0000000000..a7d515f7f3 --- /dev/null +++ b/.changeset/rich-grapes-whisper.md @@ -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). diff --git a/reviewing/license-scanner/src/getPkgInfo.ts b/reviewing/license-scanner/src/getPkgInfo.ts index 08252d040a..e8cdc0ac58 100644 --- a/reviewing/license-scanner/src/getPkgInfo.ts +++ b/reviewing/license-scanner/src/getPkgInfo.ts @@ -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, } } } diff --git a/reviewing/plugin-commands-licenses/test/__snapshots__/index.ts.snap b/reviewing/plugin-commands-licenses/test/__snapshots__/index.ts.snap index ed2813fbac..602ff24e00 100644 --- a/reviewing/plugin-commands-licenses/test/__snapshots__/index.ts.snap +++ b/reviewing/plugin-commands-licenses/test/__snapshots__/index.ts.snap @@ -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 │ └──────────────────────────────┴─────────┴────────────────────────────────────────────────────────────────────────────────────────────────────────┘