licenses.mjs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/usr/bin/env node
  2. /* eslint-disable max-lines -- 三方 license 盘点/声明/门禁一体脚本,数据表与校验逻辑集中维护。 */
  3. // 三方 license 盘点/声明/门禁 一体脚本
  4. //
  5. // 用法:
  6. // node scripts/licenses.mjs notices 生成 THIRD-PARTY-NOTICES.md(精确版本、原始版权和许可文本)
  7. // node scripts/licenses.mjs check 手动门禁:出现 allowlist 之外或许可未知的包即退出码 1
  8. //
  9. // 数据口径:
  10. // - 实装清单 = 全 workspace node_modules 的递归集合,含嵌套版本与符号链接。
  11. // - 声明生成:third-party-npm.mjs 按生产依赖的精确版本收集真实许可文件。
  12. // - prod 判定按锁文件生产图中的精确版本。
  13. // - 商用/半开放检查作用于全量实装包(prod+dev)
  14. import { generateThirdPartyNotices } from "./generate-third-party-notices.mjs";
  15. import { readVerifiedNotices } from "./third-party-notices.mjs";
  16. import {
  17. isReviewedLegacyLicense,
  18. isReviewedBundledFontLicense,
  19. legacyLicenseIdentifier,
  20. } from "./license-identifiers.mjs";
  21. import { readFile } from "node:fs/promises";
  22. import {
  23. readWorkspaceProductionGraph,
  24. scanInstalledPackages,
  25. missingProductionPackages,
  26. } from "./third-party-npm.mjs";
  27. import path from "node:path";
  28. import { fileURLToPath } from "node:url";
  29. const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
  30. const command = process.argv[2] || "notices";
  31. if (command === "notices") {
  32. await generateThirdPartyNotices(ROOT);
  33. process.exit(0);
  34. }
  35. // 标识门禁与声明生成共用 workspace 图和递归扫描,不按包名猜测生产范围。
  36. const { required, projects } = await readWorkspaceProductionGraph(ROOT);
  37. const ownNames = new Set(projects.map((project) => project.name));
  38. const installed = new Map();
  39. const MANUAL_LICENSE = {
  40. "exif-parser": "MIT", // 包内 LICENSE.md
  41. khroma: "MIT", // 包内 license 文件
  42. semaphore: "MIT", // 包内 README License 段
  43. "css-value": "MIT", // 包内 Readme License 段
  44. "@fig/autocomplete-helpers": "MIT", // 包内 LICENSE
  45. };
  46. function normLicense(value) {
  47. if (typeof value === "string") return value.trim();
  48. if (Array.isArray(value)) return value.map(normLicense).join(" OR ");
  49. return value?.type?.trim() || "(missing)";
  50. }
  51. const scanned = await scanInstalledPackages(ROOT, projects);
  52. missingProductionPackages(required, scanned);
  53. for (const [key, { pkg }] of scanned) {
  54. if (ownNames.has(pkg.name)) continue;
  55. installed.set(key, {
  56. name: pkg.name,
  57. version: pkg.version,
  58. license: normLicense(
  59. pkg.license ?? pkg.licenses ?? legacyLicenseIdentifier(pkg) ?? MANUAL_LICENSE[pkg.name],
  60. ),
  61. isProd: required.has(key),
  62. });
  63. }
  64. // ---------- 分类 ----------
  65. const GREEN =
  66. /^(MIT|MIT-0|ISC|BSD-2-Clause|BSD-3-Clause|BSD-4-Clause|0BSD|Unlicense|Apache-2\.0|Zlib|WTFPL|Artistic-2\.0|BlueOak-1\.0\.0|CC0-1\.0|CC-BY-4\.0|CC-BY-3\.0|BSD|Python-2\.0)$/i;
  67. function classify(raw) {
  68. const s = raw.replace(/[()]/g, " ").trim();
  69. if (s === "(missing)" || s === "missing" || s === "") return "missing";
  70. if (/^UNLICENSED|SEE LICEN[CS]E IN|UNKNOWN|N\/A$/i.test(s)) return "unresolved";
  71. const or = s
  72. .split(/\s+OR\s+/i)
  73. .map((x) => x.trim())
  74. .filter(Boolean);
  75. if (or.length > 1 && or.some((x) => GREEN.test(x))) return "green";
  76. const and = s
  77. .split(/\s+AND\s+/i)
  78. .map((x) => x.trim())
  79. .filter(Boolean);
  80. if (and.length > 1 && and.every((x) => GREEN.test(x))) return "green";
  81. const t = or[0] || and[0] || s;
  82. if (/\bAGPL/i.test(t)) return "red-agpl";
  83. if (/\bLGPL/i.test(t)) return "yellow-lgpl";
  84. if (/\bGPL/i.test(t)) return "red-gpl";
  85. if (/^(MPL|EPL|CDDL)/i.test(t)) return "yellow-weak";
  86. if (/^(BUSL|BSL|Elastic|SSPL|PolyForm|FSL|CAL-1)/i.test(t)) return "red-semiopen";
  87. if (/^CC-BY-NC/i.test(t)) return "red-nc";
  88. if (/^CC-BY-(ND|SA)/i.test(t)) return "yellow-cc";
  89. return GREEN.test(t) ? "green" : "review";
  90. }
  91. for (const r of installed.values()) r.bucket = classify(r.license);
  92. // ---------- 构建工具许可标识复核(不是二进制发行义务豁免) ----------
  93. const WEAK_ALLOW = [[/^lightningcss/, "当前仅构建依赖;进入生产图时需重新核对 MPL 源码提供义务"]];
  94. // ---------- 生产依赖的逐项复核结论:按包名登记并绑定许可类别,许可变化后重新阻断 ----------
  95. const PROD_REVIEWED = [
  96. [
  97. /^@img\/sharp-(libvips-|win32-)/,
  98. "yellow-lgpl",
  99. "libvips 以独立、可替换的共享库随 sharp 分发,仅真实 Computer Use 截图使用;声明附 LGPL-3.0 原文",
  100. ],
  101. [
  102. /^elkjs$/,
  103. "yellow-weak",
  104. "仅内部开发文档站 apps/dev-docs 使用,不随产品分发、不部署;按 EPL-2.0 使用",
  105. ],
  106. ];
  107. function weakAllowReason(r) {
  108. if (r.isProd) {
  109. for (const [re, bucket, why] of PROD_REVIEWED)
  110. if (re.test(r.name) && r.bucket === bucket) return why;
  111. return null;
  112. }
  113. for (const [re, why] of WEAK_ALLOW) if (re.test(r.name)) return why;
  114. return null;
  115. }
  116. if (command === "check") {
  117. const bad = [];
  118. for (const r of installed.values()) {
  119. if (r.bucket === "green" || isReviewedLegacyLicense(r) || isReviewedBundledFontLicense(r))
  120. continue;
  121. if (r.bucket.startsWith("yellow") && weakAllowReason(r)) continue;
  122. bad.push(r);
  123. }
  124. if (bad.length) {
  125. console.error(`✗ license 检查失败:${bad.length} 个包超出 allowlist:`);
  126. for (const r of bad.sort((a, b) => a.name.localeCompare(b.name)))
  127. console.error(` [${r.bucket}] ${r.name}@${r.version} → ${r.license}`);
  128. console.error(
  129. "\n处置:替换依赖,或在 scripts/licenses.mjs 的 WEAK_ALLOW(构建依赖)/ PROD_REVIEWED(生产依赖)登记人工复核结论。",
  130. );
  131. process.exit(1);
  132. }
  133. await readVerifiedNotices(ROOT, { requireComplete: process.argv.includes("--strict") });
  134. const reviewRequired =
  135. JSON.parse(await readFile(path.join(ROOT, "third-party/inventory.json"), "utf8"))
  136. .reviewRequired ?? [];
  137. if (reviewRequired.length)
  138. console.warn(
  139. `待补齐/核验材料 ${reviewRequired.length} 项;发布前运行 node scripts/licenses.mjs check --strict,不得将基础检查通过视为合规完成。`,
  140. );
  141. const n = installed.size;
  142. console.log(
  143. `✓ 许可标识与声明新鲜度检查通过:${n} 个实装包(构建依赖复核 ${[...installed.values()].filter((r) => r.bucket.startsWith("yellow")).length} 项);材料限制见 third-party/README.md`,
  144. );
  145. } else {
  146. console.error("用法: node scripts/licenses.mjs [notices|check]");
  147. process.exit(2);
  148. }