Files
pulse/scripts/ci/licenses.js
T

51 lines
2.0 KiB
JavaScript
Raw Permalink Normal View History

#!/usr/bin/env node
'use strict';
// Licence review for production dependencies (#45): every package that ships with Pulse
// must use a licence on the allowlist. Run after `npm ci`. The lockfile records most
// licences; for the rest, the installed package's own package.json is read.
// An SPDX "A OR B" passes when either side is allowed; "A AND B" needs both.
const fs = require('fs');
const path = require('path');
const ALLOWED = new Set([
'MIT', 'ISC', 'BSD-2-Clause', 'BSD-3-Clause', 'Apache-2.0', '0BSD',
'BlueOak-1.0.0', 'CC0-1.0', 'Unlicense', 'Python-2.0',
]);
const root = path.join(__dirname, '..', '..');
const lock = JSON.parse(fs.readFileSync(path.join(root, 'package-lock.json'), 'utf8'));
function licenceOf(key, entry) {
if (entry.license) return entry.license;
try {
const pkg = JSON.parse(fs.readFileSync(path.join(root, key, 'package.json'), 'utf8'));
if (typeof pkg.license === 'string') return pkg.license;
if (pkg.license && pkg.license.type) return pkg.license.type;
if (Array.isArray(pkg.licenses)) return pkg.licenses.map(l => l.type || l).join(' OR ');
} catch (e) { /* not installed or unreadable: reported below */ }
return null;
}
function allowed(expr) {
const clean = expr.replace(/[()]/g, ' ').trim();
return clean.split(/\s+OR\s+/).some(alt => alt.split(/\s+AND\s+/).every(id => ALLOWED.has(id.trim())));
}
const problems = [];
let checked = 0;
for (const [key, entry] of Object.entries(lock.packages || {})) {
if (!key || entry.dev || entry.link) continue; // the root, dev-only and workspace links
checked++;
const lic = licenceOf(key, entry);
if (!lic) problems.push(`${key}: no licence found`);
else if (!allowed(lic)) problems.push(`${key}: ${lic}`);
}
if (problems.length) {
console.error(`Licences not on the allowlist (${[...ALLOWED].join(', ')}):`);
for (const p of problems) console.error(' ' + p);
process.exit(1);
}
console.log(`${checked} production packages, all with allowed licences.`);