|
| 1 | +/** |
| 2 | + * License utilities: file reading, SPDX detection, normalization, compatibility. |
| 3 | + * This module has NO dependencies on providers or backend to avoid circular dependencies. |
| 4 | + */ |
| 5 | + |
| 6 | +import fs from 'node:fs'; |
| 7 | +import path from 'node:path'; |
| 8 | + |
| 9 | +const LICENSE_FILES = ['LICENSE', 'LICENSE.md', 'LICENSE.txt']; |
| 10 | + |
| 11 | +/** |
| 12 | + * Find LICENSE file path in the same directory as the manifest. |
| 13 | + * @param {string} manifestPath |
| 14 | + * @returns {string|null} - path to LICENSE file or null if not found |
| 15 | + */ |
| 16 | +export function findLicenseFilePath(manifestPath) { |
| 17 | + const manifestDir = path.dirname(path.resolve(manifestPath)); |
| 18 | + |
| 19 | + for (const name of LICENSE_FILES) { |
| 20 | + const filePath = path.join(manifestDir, name); |
| 21 | + try { |
| 22 | + if (fs.statSync(filePath).isFile()) { |
| 23 | + return filePath; |
| 24 | + } |
| 25 | + } catch { |
| 26 | + // skip |
| 27 | + } |
| 28 | + } |
| 29 | + return null; |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Very simple SPDX detection from common license text (first ~500 chars). |
| 34 | + * @param {string} text |
| 35 | + * @returns {string|null} |
| 36 | + */ |
| 37 | +export function detectSpdxFromText(text) { |
| 38 | + const head = text.slice(0, 500); |
| 39 | + if (/Apache License,?\s*Version 2\.0/i.test(head)) { return 'Apache-2.0'; } |
| 40 | + if (/MIT License/i.test(head) && /Permission is hereby granted/i.test(head)) { return 'MIT'; } |
| 41 | + if (/GNU AFFERO GENERAL PUBLIC LICENSE\s+Version 3/i.test(head)) { return 'AGPL-3.0-only'; } |
| 42 | + if (/GNU LESSER GENERAL PUBLIC LICENSE\s+Version 3/i.test(head)) { return 'LGPL-3.0-only'; } |
| 43 | + if (/GNU LESSER GENERAL PUBLIC LICENSE\s+Version 2\.1/i.test(head)) { return 'LGPL-2.1-only'; } |
| 44 | + if (/GNU GENERAL PUBLIC LICENSE\s+Version 2/i.test(head)) { return 'GPL-2.0-only'; } |
| 45 | + if (/GNU GENERAL PUBLIC LICENSE\s+Version 3/i.test(head)) { return 'GPL-3.0-only'; } |
| 46 | + if (/BSD 2-Clause/i.test(head)) { return 'BSD-2-Clause'; } |
| 47 | + if (/BSD 3-Clause/i.test(head)) { return 'BSD-3-Clause'; } |
| 48 | + return null; |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Read LICENSE file and detect SPDX identifier. |
| 53 | + * @param {string} manifestPath - path to manifest |
| 54 | + * @returns {string|null} - SPDX identifier from LICENSE file or null |
| 55 | + */ |
| 56 | +export function readLicenseFile(manifestPath) { |
| 57 | + const licenseFilePath = findLicenseFilePath(manifestPath); |
| 58 | + if (!licenseFilePath) { return null; } |
| 59 | + |
| 60 | + try { |
| 61 | + const content = fs.readFileSync(licenseFilePath, 'utf-8'); |
| 62 | + return detectSpdxFromText(content) || content.split('\n')[0]?.trim() || null; |
| 63 | + } catch { |
| 64 | + return null; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Get project license from manifest or LICENSE file. |
| 70 | + * Returns manifestLicense if provided, otherwise tries LICENSE file. |
| 71 | + * @param {string|null} manifestLicense - license from manifest (or null) |
| 72 | + * @param {string} manifestPath - path to manifest |
| 73 | + * @returns {string|null} - SPDX identifier or null |
| 74 | + */ |
| 75 | +export function getLicense(manifestLicense, manifestPath) { |
| 76 | + return manifestLicense || readLicenseFile(manifestPath) || null; |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Normalize SPDX identifier for comparison (lowercase, strip common suffixes). |
| 81 | + * @param {string} spdxOrName |
| 82 | + * @returns {string} |
| 83 | + */ |
| 84 | +export function normalizeSpdx(spdxOrName) { |
| 85 | + const s = String(spdxOrName).trim().toLowerCase(); |
| 86 | + if (s.endsWith(' license')) { return s.slice(0, -8); } |
| 87 | + return s; |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Check if a dependency's license is compatible with the project license based on backend categories. |
| 92 | + * |
| 93 | + * @param {string} [projectCategory] - backend category for project license: PERMISSIVE | WEAK_COPYLEFT | STRONG_COPYLEFT | UNKNOWN |
| 94 | + * @param {string} [dependencyCategory] - backend category for dependency license: PERMISSIVE | WEAK_COPYLEFT | STRONG_COPYLEFT | UNKNOWN |
| 95 | + * @returns {'compatible'|'incompatible'|'unknown'} |
| 96 | + */ |
| 97 | +export function getCompatibility(projectCategory, dependencyCategory) { |
| 98 | + if (!projectCategory || !dependencyCategory) { |
| 99 | + return 'unknown'; |
| 100 | + } |
| 101 | + |
| 102 | + const proj = projectCategory.toUpperCase(); |
| 103 | + const dep = dependencyCategory.toUpperCase(); |
| 104 | + |
| 105 | + if (proj === 'UNKNOWN' || dep === 'UNKNOWN') { |
| 106 | + return 'unknown'; |
| 107 | + } |
| 108 | + |
| 109 | + const restrictiveness = { |
| 110 | + 'PERMISSIVE': 1, |
| 111 | + 'WEAK_COPYLEFT': 2, |
| 112 | + 'STRONG_COPYLEFT': 3 |
| 113 | + }; |
| 114 | + |
| 115 | + const projLevel = restrictiveness[proj]; |
| 116 | + const depLevel = restrictiveness[dep]; |
| 117 | + |
| 118 | + if (projLevel === undefined || depLevel === undefined) { |
| 119 | + return 'unknown'; |
| 120 | + } |
| 121 | + |
| 122 | + if (depLevel > projLevel) { |
| 123 | + return 'incompatible'; |
| 124 | + } |
| 125 | + |
| 126 | + return 'compatible'; |
| 127 | +} |
0 commit comments