licenses.mjs 6.2 KB

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