-
Notifications
You must be signed in to change notification settings - Fork 11
feat: fall back to LICENSE file #409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| /** | ||
| * License utilities: file reading, SPDX detection, normalization, compatibility. | ||
| * This module has NO dependencies on providers or backend to avoid circular dependencies. | ||
| */ | ||
|
|
||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
|
|
||
| const LICENSE_FILES = ['LICENSE', 'LICENSE.md', 'LICENSE.txt']; | ||
|
|
||
| /** | ||
| * Find LICENSE file path in the same directory as the manifest. | ||
| * @param {string} manifestPath | ||
| * @returns {string|null} - path to LICENSE file or null if not found | ||
| */ | ||
| export function findLicenseFilePath(manifestPath) { | ||
| const manifestDir = path.dirname(path.resolve(manifestPath)); | ||
|
|
||
| for (const name of LICENSE_FILES) { | ||
| const filePath = path.join(manifestDir, name); | ||
| try { | ||
| if (fs.statSync(filePath).isFile()) { | ||
| return filePath; | ||
| } | ||
| } catch { | ||
| // skip | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Very simple SPDX detection from common license text (first ~500 chars). | ||
| * @param {string} text | ||
| * @returns {string|null} | ||
| */ | ||
| export function detectSpdxFromText(text) { | ||
| const head = text.slice(0, 500); | ||
| if (/Apache License,?\s*Version 2\.0/i.test(head)) { return 'Apache-2.0'; } | ||
| if (/MIT License/i.test(head) && /Permission is hereby granted/i.test(head)) { return 'MIT'; } | ||
| if (/GNU AFFERO GENERAL PUBLIC LICENSE\s+Version 3/i.test(head)) { return 'AGPL-3.0-only'; } | ||
| if (/GNU LESSER GENERAL PUBLIC LICENSE\s+Version 3/i.test(head)) { return 'LGPL-3.0-only'; } | ||
| if (/GNU LESSER GENERAL PUBLIC LICENSE\s+Version 2\.1/i.test(head)) { return 'LGPL-2.1-only'; } | ||
| if (/GNU GENERAL PUBLIC LICENSE\s+Version 2/i.test(head)) { return 'GPL-2.0-only'; } | ||
| if (/GNU GENERAL PUBLIC LICENSE\s+Version 3/i.test(head)) { return 'GPL-3.0-only'; } | ||
| if (/BSD 2-Clause/i.test(head)) { return 'BSD-2-Clause'; } | ||
| if (/BSD 3-Clause/i.test(head)) { return 'BSD-3-Clause'; } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Read LICENSE file and detect SPDX identifier. | ||
| * @param {string} manifestPath - path to manifest | ||
| * @returns {string|null} - SPDX identifier from LICENSE file or null | ||
| */ | ||
| export function readLicenseFile(manifestPath) { | ||
| const licenseFilePath = findLicenseFilePath(manifestPath); | ||
| if (!licenseFilePath) { return null; } | ||
|
|
||
| try { | ||
| const content = fs.readFileSync(licenseFilePath, 'utf-8'); | ||
| return detectSpdxFromText(content) || content.split('\n')[0]?.trim() || null; | ||
| } catch { | ||
| return null; | ||
| } | ||
|
ruromero marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /** | ||
| * Get project license from manifest or LICENSE file. | ||
| * Returns manifestLicense if provided, otherwise tries LICENSE file. | ||
| * @param {string|null} manifestLicense - license from manifest (or null) | ||
| * @param {string} manifestPath - path to manifest | ||
| * @returns {string|null} - SPDX identifier or null | ||
| */ | ||
| export function getLicense(manifestLicense, manifestPath) { | ||
| return manifestLicense || readLicenseFile(manifestPath) || null; | ||
| } | ||
|
|
||
| /** | ||
| * Normalize SPDX identifier for comparison (lowercase, strip common suffixes). | ||
| * @param {string} spdxOrName | ||
| * @returns {string} | ||
| */ | ||
| export function normalizeSpdx(spdxOrName) { | ||
| const s = String(spdxOrName).trim().toLowerCase(); | ||
| if (s.endsWith(' license')) { return s.slice(0, -8); } | ||
| return s; | ||
| } | ||
|
|
||
| /** | ||
| * Check if a dependency's license is compatible with the project license based on backend categories. | ||
| * | ||
| * @param {string} [projectCategory] - backend category for project license: PERMISSIVE | WEAK_COPYLEFT | STRONG_COPYLEFT | UNKNOWN | ||
| * @param {string} [dependencyCategory] - backend category for dependency license: PERMISSIVE | WEAK_COPYLEFT | STRONG_COPYLEFT | UNKNOWN | ||
| * @returns {'compatible'|'incompatible'|'unknown'} | ||
| */ | ||
| export function getCompatibility(projectCategory, dependencyCategory) { | ||
| if (!projectCategory || !dependencyCategory) { | ||
| return 'unknown'; | ||
| } | ||
|
|
||
| const proj = projectCategory.toUpperCase(); | ||
| const dep = dependencyCategory.toUpperCase(); | ||
|
|
||
| if (proj === 'UNKNOWN' || dep === 'UNKNOWN') { | ||
| return 'unknown'; | ||
| } | ||
|
|
||
| const restrictiveness = { | ||
| 'PERMISSIVE': 1, | ||
| 'WEAK_COPYLEFT': 2, | ||
| 'STRONG_COPYLEFT': 3 | ||
| }; | ||
|
|
||
| const projLevel = restrictiveness[proj]; | ||
| const depLevel = restrictiveness[dep]; | ||
|
|
||
| if (projLevel === undefined || depLevel === undefined) { | ||
| return 'unknown'; | ||
| } | ||
|
|
||
| if (depLevel > projLevel) { | ||
| return 'incompatible'; | ||
| } | ||
|
|
||
| return 'compatible'; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.