#!/usr/bin/env node // Writes the license notices for everything the app ships to stdout: the // project's own LICENSE, then every production dependency of the client and // server workspaces with its license text, as installed in node_modules. // // node scripts/third-party-licenses.mjs > licenses.txt // // Run it from the repository root after `npm ci` (dev dependencies may be // installed too; only production dependencies are listed). Exits non-zero // when a package's license can't be determined. import { execFileSync } from "node:child_process"; import * as fs from "node:fs"; import * as path from "node:path"; const ROOT = process.cwd(); const LICENSE_FILE = /^(licen[cs]e|copying|notice)(\.|-|$)/i; const tree = JSON.parse( execFileSync("npm", ["ls", "--omit=dev", "--all", "--long", "--json"], { cwd: ROOT, encoding: "utf8", maxBuffer: 256 * 1024 * 1024, // npm ls exits non-zero on unmet optional peers; the JSON is still complete stdio: ["ignore", "pipe", "ignore"], }).toString() || "{}", ); const workspaces = new Set( ( JSON.parse(fs.readFileSync(path.join(ROOT, "package.json"), "utf8")) .workspaces ?? [] ).map((w) => path.resolve(ROOT, w)), ); /** name@version → { name, version, license, dir } */ const packages = new Map(); const walk = (node) => { for (const [name, dep] of Object.entries(node.dependencies ?? {})) { // Unmet optional peers have no path if (!dep.path) continue; // Workspace packages are our own code: list only their dependencies if (!workspaces.has(fs.realpathSync(dep.path))) { const key = `${name}@${dep.version}`; if (packages.has(key)) continue; packages.set(key, { name, version: dep.version, dir: dep.path }); } walk(dep); } }; walk(tree); const manifestOf = (pkg) => JSON.parse(fs.readFileSync(path.join(pkg.dir, "package.json"), "utf8")); const licenseOf = (pkg) => { const manifest = manifestOf(pkg); const field = manifest.license ?? manifest.licenses; if (typeof field === "string") return field; if (Array.isArray(field)) return field.map((l) => l.type ?? l).join(" OR "); if (field?.type) return field.type; return null; }; const licenseTexts = (pkg) => fs .readdirSync(pkg.dir) .filter( (f) => LICENSE_FILE.test(f) && fs.statSync(path.join(pkg.dir, f)).isFile(), ) .sort() .map((f) => fs.readFileSync(path.join(pkg.dir, f), "utf8").trim()); // For packages that ship no license file, record who to attribute and where // the full terms are const withoutLicenseFile = (pkg, license) => { const { author, repository, homepage } = manifestOf(pkg); const authorName = typeof author === "string" ? author : author?.name; const repo = typeof repository === "string" ? repository : repository?.url; return [ `The package ships no license file; its package.json declares ${license}.`, authorName && `Author: ${authorName}`, (repo || homepage) && `Source: ${repo || homepage}`, ] .filter(Boolean) .join("\n"); }; const missing = []; const sections = [...packages.values()] .sort( (a, b) => a.name.localeCompare(b.name) || a.version.localeCompare(b.version), ) .map((pkg) => { const license = licenseOf(pkg); const texts = licenseTexts(pkg); if (!license && texts.length === 0) missing.push(`${pkg.name}@${pkg.version}`); const body = texts.length > 0 ? texts.join("\n\n") : withoutLicenseFile(pkg, license); return `${pkg.name}@${pkg.version}\nLicense: ${license ?? "see below"}\n\n${body}`; }); if (missing.length > 0) { console.error(`No license information for: ${missing.join(", ")}`); process.exit(1); } const rule = "-".repeat(79); process.stdout.write( [ fs.readFileSync(path.join(ROOT, "LICENSE"), "utf8").trim(), `${"=".repeat(79)}\n\nThird-party software\n\nThis application includes the following third-party packages (${sections.length}),\nunder their own licenses:`, ...sections, ].join(`\n\n${rule}\n\n`) + "\n", );