|
| 1 | +#!/usr/bin/env node |
| 2 | +// Gate 6 (lint:pkg) enforcer — treats publint suggestions/warnings/errors as fatal. |
| 3 | +// |
| 4 | +// publint 0.3.18 CLI does not expose a flag to fail on suggestions (--strict |
| 5 | +// only promotes warnings → errors). This wrapper fills that gap: it runs |
| 6 | +// publint per workspace, captures stdout, and fails the gate if any package |
| 7 | +// emits a "Suggestions:", "Warnings:", or "Errors:" block. attw --pack runs |
| 8 | +// after publint per package and preserves its own exit code. |
| 9 | +// |
| 10 | +// See enforcement queue #33 and the PR #35 regression that motivated this |
| 11 | +// tightening: publint suggestions about the "git+" URL prefix silently |
| 12 | +// re-drifted across 10 packages because the gate tolerated them. |
| 13 | + |
| 14 | +import { spawnSync } from "node:child_process"; |
| 15 | +import { readdirSync, readFileSync, statSync } from "node:fs"; |
| 16 | +import { join } from "node:path"; |
| 17 | + |
| 18 | +const PACKAGES_DIR = "packages"; |
| 19 | +const PUBLINT_BLOCK_RE = /^(Suggestions|Warnings|Errors):$/m; |
| 20 | + |
| 21 | +function listPackageDirs() { |
| 22 | + return readdirSync(PACKAGES_DIR) |
| 23 | + .map((name) => join(PACKAGES_DIR, name)) |
| 24 | + .filter((dir) => { |
| 25 | + try { |
| 26 | + return statSync(dir).isDirectory() && statSync(join(dir, "package.json")).isFile(); |
| 27 | + } catch { |
| 28 | + return false; |
| 29 | + } |
| 30 | + }) |
| 31 | + .sort(); |
| 32 | +} |
| 33 | + |
| 34 | +function packageName(dir) { |
| 35 | + const pkg = JSON.parse(readFileSync(join(dir, "package.json"), "utf8")); |
| 36 | + return pkg.name ?? dir; |
| 37 | +} |
| 38 | + |
| 39 | +function runCaptured(cmd, args, cwd) { |
| 40 | + const result = spawnSync(cmd, args, { |
| 41 | + cwd, |
| 42 | + stdio: ["ignore", "pipe", "pipe"], |
| 43 | + encoding: "utf8", |
| 44 | + shell: false, |
| 45 | + }); |
| 46 | + const stdout = result.stdout ?? ""; |
| 47 | + const stderr = result.stderr ?? ""; |
| 48 | + process.stdout.write(stdout); |
| 49 | + process.stderr.write(stderr); |
| 50 | + return { stdout, stderr, status: result.status ?? 1 }; |
| 51 | +} |
| 52 | + |
| 53 | +function main() { |
| 54 | + const dirs = listPackageDirs(); |
| 55 | + const failures = []; |
| 56 | + |
| 57 | + for (const dir of dirs) { |
| 58 | + const name = packageName(dir); |
| 59 | + process.stdout.write(`\n--- lint:pkg ${name} (${dir}) ---\n`); |
| 60 | + |
| 61 | + const publint = runCaptured("npx", ["publint", "run"], dir); |
| 62 | + const publintBlock = PUBLINT_BLOCK_RE.exec(publint.stdout); |
| 63 | + if (publint.status !== 0) { |
| 64 | + failures.push(`${name}: publint exited ${publint.status}`); |
| 65 | + } else if (publintBlock) { |
| 66 | + failures.push( |
| 67 | + `${name}: publint emitted "${publintBlock[1]}:" block (fail-on-suggestion gate)`, |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + const attw = runCaptured("npx", ["attw", "--pack"], dir); |
| 72 | + if (attw.status !== 0) { |
| 73 | + failures.push(`${name}: attw exited ${attw.status}`); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + if (failures.length > 0) { |
| 78 | + process.stderr.write(`\n\nlint:pkg gate FAILED (${failures.length}):\n`); |
| 79 | + for (const f of failures) { |
| 80 | + process.stderr.write(` - ${f}\n`); |
| 81 | + } |
| 82 | + process.exit(1); |
| 83 | + } |
| 84 | + |
| 85 | + process.stdout.write( |
| 86 | + `\nlint:pkg gate PASS — ${dirs.length} packages clean (publint suggestions/warnings/errors all treated as fatal).\n`, |
| 87 | + ); |
| 88 | +} |
| 89 | + |
| 90 | +main(); |
0 commit comments