diff --git a/.github/workflows/licenses.yml b/.github/workflows/licenses.yml new file mode 100644 index 0000000..9d065e6 --- /dev/null +++ b/.github/workflows/licenses.yml @@ -0,0 +1,36 @@ +name: License checks + +on: + push: + branches: [main] + pull_request: + +permissions: + contents: read + +jobs: + npm-licenses: + name: npm dependency licenses + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: "24" + - run: npm ci --ignore-scripts + - name: Check all lockfile package licenses + run: | + node --input-type=module <<'NODE' + import assert from "node:assert/strict"; + import { readFileSync } from "node:fs"; + const allowed = new Set(["MIT", "Apache-2.0", "MIT OR Apache-2.0", "Apache-2.0 OR MIT"]); + function check(lock) { + if (lock?.lockfileVersion !== 3 || !lock.packages || typeof lock.packages !== "object" || Array.isArray(lock.packages) || !lock.packages[""]) throw new Error("Invalid npm lockfile"); + return Object.entries(lock.packages).filter(([path, pkg]) => path !== "" && !allowed.has(pkg?.license)).map(([path, pkg]) => `${path}: ${JSON.stringify(pkg?.license)}`); + } + assert.equal(check({ lockfileVersion: 3, packages: { "": {}, x: { license: "MIT AND GPL-3.0-only" } } }).length, 1); + assert.equal(check({ lockfileVersion: 3, packages: { "": {}, x: {} } }).length, 1); + assert.equal(check({ lockfileVersion: 3, packages: { "": {}, x: { license: "MIT" } } }).length, 0); + const failures = check(JSON.parse(readFileSync("package-lock.json", "utf8"))); + if (failures.length) { console.error(failures.join("\n")); process.exitCode = 1; } + NODE