#!/usr/bin/env node // Regenerates the "Every crate in Cargo.lock" table in docs/DEPENDENCIES.md from `cargo metadata` // and exits 1 when a crate's licence is outside the allowed set (or missing). // Usage: node scripts/deps.mjs [--check] (--check: do not rewrite the file, only verify it is current) import { execFileSync } from 'node:child_process'; import { readFileSync, writeFileSync } from 'node:fs'; import { dirname, join } from 'node:path'; import { fileURLToPath } from 'node:url'; const root = join(dirname(fileURLToPath(import.meta.url)), '..'); const docPath = join(root, 'docs', 'DEPENDENCIES.md'); export const ALLOWED = new Set([ 'MIT', 'MIT-0', 'Apache-2.0', 'Apache-2.0 WITH LLVM-exception', 'BSD-2-Clause', 'BSD-3-Clause', '0BSD', 'Zlib', 'MPL-2.0', 'ISC', 'Unicode-3.0', 'Unicode-DFS-2016', 'BSL-1.0', 'CC0-1.0', ]); // Minimal SPDX evaluator: OR / AND / WITH and parentheses. Old crates write "MIT/Apache-2.0" for OR. export function allowed(expr) { if (!expr) return false; const tokens = expr.replace(/\//g, ' OR ').replace(/\(/g, ' ( ').replace(/\)/g, ' ) ').split(/\s+/).filter(Boolean); let i = 0; function primary() { if (tokens[i] === '(') { i++; const v = orExpr(); i++; return v; } let id = tokens[i++]; if (tokens[i] === 'WITH') { id += ` WITH ${tokens[i + 1]}`; i += 2; } return ALLOWED.has(id); } function andExpr() { let v = primary(); while (tokens[i] === 'AND') { i++; v = primary() && v; } return v; } function orExpr() { let v = andExpr(); while (tokens[i] === 'OR') { i++; v = andExpr() || v; } return v; } return orExpr(); } function main() { // Every package in Cargo.lock (all platforms): a crate that is locked must be listed, whatever it is built for. const meta = JSON.parse(execFileSync('cargo', ['metadata', '--format-version', '1', '--locked'], { cwd: root, maxBuffer: 1 << 28, encoding: 'utf8', })); // resolve.nodes lists every package the lockfile resolves (all targets). const used = new Set(meta.resolve.nodes.map((n) => n.id)); const workspace = new Set(meta.workspace_members); const rows = meta.packages .filter((p) => used.has(p.id) && !workspace.has(p.id)) .map((p) => ({ name: p.name, version: p.version, licence: p.license ?? (p.license_file ? `file: ${p.license_file}` : '') })) .sort((a, b) => a.name.localeCompare(b.name) || a.version.localeCompare(b.version)); const bad = rows.filter((r) => !allowed(r.licence)); const table = [ `${rows.length} crates; ${bad.length} outside the allowed set.`, '', '| Crate | Version | Licence | Allowed |', '| :-- | :-- | :-- | :-- |', ...rows.map((r) => `| ${r.name} | ${r.version} | ${r.licence || '(none)'} | ${allowed(r.licence) ? 'yes' : '**NO**'} |`), ].join('\n'); const doc = readFileSync(docPath, 'utf8'); const next = doc.replace(/[\s\S]*/, `\n${table}\n`); if (process.argv.includes('--check')) { if (next !== doc) { console.error('docs/DEPENDENCIES.md is out of date: run node scripts/deps.mjs'); process.exit(1); } } else if (next !== doc) { writeFileSync(docPath, next); } for (const r of bad) console.error(`not allowed: ${r.name} ${r.version} (${r.licence || 'no licence field'})`); console.log(`${rows.length} crates, ${bad.length} not allowed`); process.exit(bad.length ? 1 : 0); } if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) main();