|
| 1 | +/** |
| 2 | + * Generates THIRD-PARTY-LICENSES.md from the runtime dependency closure. |
| 3 | + * |
| 4 | + * The published CLIs bundle their runtime dependencies into `dist/`, which |
| 5 | + * strips the per-package license headers. To satisfy the (permissive) MIT / |
| 6 | + * ISC / BSD terms of those bundled packages, we collect each one's license |
| 7 | + * text into a single notices file shipped alongside the bundle. |
| 8 | + * |
| 9 | + * Roots are the packages actually imported by src/. Dev-only tooling |
| 10 | + * (biome, typescript, @types/*) is intentionally excluded — none of it is |
| 11 | + * bundled into dist/. |
| 12 | + */ |
| 13 | + |
| 14 | +import { readdirSync, readFileSync } from 'node:fs'; |
| 15 | +import { join } from 'node:path'; |
| 16 | + |
| 17 | +const ROOTS = ['node-pkware', 'command-line-args', 'winston', 'mdb-reader']; |
| 18 | +const NODE_MODULES = 'node_modules'; |
| 19 | + |
| 20 | +interface Pkg { |
| 21 | + name: string; |
| 22 | + version: string; |
| 23 | + license?: string; |
| 24 | + licenses?: { type: string }[]; |
| 25 | + author?: unknown; |
| 26 | + dependencies?: Record<string, string>; |
| 27 | + homepage?: string; |
| 28 | +} |
| 29 | + |
| 30 | +function readPkg(name: string): Pkg | undefined { |
| 31 | + try { |
| 32 | + return JSON.parse(readFileSync(join(NODE_MODULES, name, 'package.json'), 'utf8')) as Pkg; |
| 33 | + } catch { |
| 34 | + return undefined; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +function readLicenseText(name: string): string | undefined { |
| 39 | + const dir = join(NODE_MODULES, name); |
| 40 | + let entries: string[]; |
| 41 | + try { |
| 42 | + entries = readdirSync(dir); |
| 43 | + } catch { |
| 44 | + return undefined; |
| 45 | + } |
| 46 | + const file = entries.find(e => /^(licen[sc]e|copying)(\.|$)/i.test(e)); |
| 47 | + if (!file) return undefined; |
| 48 | + try { |
| 49 | + return readFileSync(join(dir, file), 'utf8').trim(); |
| 50 | + } catch { |
| 51 | + return undefined; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +function licenseId(pkg: Pkg): string { |
| 56 | + if (typeof pkg.license === 'string') return pkg.license; |
| 57 | + if (pkg.licenses?.length) return pkg.licenses.map(l => l.type).join(', '); |
| 58 | + return 'UNKNOWN'; |
| 59 | +} |
| 60 | + |
| 61 | +// Resolve the transitive closure of runtime dependencies. |
| 62 | +const seen = new Set<string>(); |
| 63 | +const queue = [...ROOTS]; |
| 64 | +while (queue.length > 0) { |
| 65 | + const name = queue.shift() as string; |
| 66 | + if (seen.has(name)) continue; |
| 67 | + seen.add(name); |
| 68 | + const pkg = readPkg(name); |
| 69 | + if (pkg?.dependencies) queue.push(...Object.keys(pkg.dependencies)); |
| 70 | +} |
| 71 | + |
| 72 | +const names = [...seen].sort(); |
| 73 | + |
| 74 | +let unknown = 0; |
| 75 | +const sections: string[] = []; |
| 76 | +for (const name of names) { |
| 77 | + const pkg = readPkg(name); |
| 78 | + if (!pkg) continue; |
| 79 | + const id = licenseId(pkg); |
| 80 | + if (id === 'UNKNOWN') unknown++; |
| 81 | + const text = readLicenseText(name); |
| 82 | + const header = `## ${name}@${pkg.version} (${id})`; |
| 83 | + const link = pkg.homepage ? `\n${pkg.homepage}\n` : ''; |
| 84 | + const body = text ? `\n\`\`\`\n${text}\n\`\`\`\n` : '\n_(no license file shipped in the package; see the SPDX id above)_\n'; |
| 85 | + sections.push(`${header}\n${link}${body}`); |
| 86 | +} |
| 87 | + |
| 88 | +const preamble = `# Third-party licenses |
| 89 | +
|
| 90 | +The \`ewd\` and \`ewe\` executables in \`dist/\` are bundled with the following |
| 91 | +third-party packages. Their license texts are reproduced below. This file is |
| 92 | +generated by \`scripts/collect-licenses.ts\`. |
| 93 | +
|
| 94 | +Bundled packages (${names.length}): ${names.join(', ')}. |
| 95 | +`; |
| 96 | + |
| 97 | +await Bun.write('THIRD-PARTY-LICENSES.md', `${preamble}\n${sections.join('\n')}`); |
| 98 | +console.log(`Wrote THIRD-PARTY-LICENSES.md for ${names.length} packages (${unknown} with no SPDX id).`); |
0 commit comments