|
| 1 | +#!/usr/bin/env node |
| 2 | +// Generates THIRD_PARTY_LICENSES.txt covering every production dependency |
| 3 | +// bundled into the distributed container image, as required for binary |
| 4 | +// redistribution under most OSS licenses (MIT/BSD/Apache-2.0 etc.). |
| 5 | +import { execFileSync } from "node:child_process"; |
| 6 | +import { readdirSync, readFileSync, writeFileSync } from "node:fs"; |
| 7 | +import { join } from "node:path"; |
| 8 | + |
| 9 | +const LICENSE_FILE_RE = /^(license|licence|copying)(\.(md|txt))?$/i; |
| 10 | + |
| 11 | +function findLicenseText(dir) { |
| 12 | + let files; |
| 13 | + try { |
| 14 | + files = readdirSync(dir); |
| 15 | + } catch { |
| 16 | + return null; |
| 17 | + } |
| 18 | + const licenseFile = files.find((f) => LICENSE_FILE_RE.test(f)); |
| 19 | + if (!licenseFile) return null; |
| 20 | + return readFileSync(join(dir, licenseFile), "utf8").trim(); |
| 21 | +} |
| 22 | + |
| 23 | +const raw = execFileSync("pnpm", ["licenses", "list", "--json", "--prod"], { |
| 24 | + maxBuffer: 1024 * 1024 * 32, |
| 25 | +}).toString(); |
| 26 | +const byLicense = JSON.parse(raw); |
| 27 | + |
| 28 | +const packages = []; |
| 29 | +for (const [license, pkgs] of Object.entries(byLicense)) { |
| 30 | + for (const pkg of pkgs) { |
| 31 | + packages.push({ ...pkg, license }); |
| 32 | + } |
| 33 | +} |
| 34 | +packages.sort((a, b) => a.name.localeCompare(b.name)); |
| 35 | + |
| 36 | +const sections = packages.map((pkg) => { |
| 37 | + const header = [ |
| 38 | + `${pkg.name}@${pkg.versions.join(", ")}`, |
| 39 | + `License: ${pkg.license}`, |
| 40 | + pkg.homepage ? `Homepage: ${pkg.homepage}` : null, |
| 41 | + ] |
| 42 | + .filter(Boolean) |
| 43 | + .join("\n"); |
| 44 | + |
| 45 | + const text = findLicenseText(pkg.paths[0]); |
| 46 | + const body = |
| 47 | + text ?? "(license text not found in package; see homepage above)"; |
| 48 | + |
| 49 | + return `${header}\n\n${body}`; |
| 50 | +}); |
| 51 | + |
| 52 | +const banner = |
| 53 | + "This file lists the third-party open source dependencies bundled into\n" + |
| 54 | + "the clover-ui container image, along with their license texts, as\n" + |
| 55 | + "required for redistribution.\n"; |
| 56 | + |
| 57 | +const out = `${banner}\n${"=".repeat(80)}\n\n${sections.join(`\n\n${"=".repeat(80)}\n\n`)}\n`; |
| 58 | + |
| 59 | +writeFileSync("THIRD_PARTY_LICENSES.txt", out); |
| 60 | +console.log( |
| 61 | + `Wrote THIRD_PARTY_LICENSES.txt covering ${packages.length} packages.`, |
| 62 | +); |
0 commit comments