|
| 1 | +#!/usr/bin/env bun |
| 2 | +// Path-gated releases (workit AR-16): a releasable commit counts only when it |
| 3 | +// touches package payload. CI/docs/tests/scripts-only merges cut no npm publish. |
| 4 | +import { execFileSync } from "node:child_process"; |
| 5 | +import { resolve } from "node:path"; |
| 6 | + |
| 7 | +const PRODUCT_FILES = new Set([ |
| 8 | + "plugin.ts", |
| 9 | + "index.ts", |
| 10 | + "models.json", |
| 11 | + "manifest.json", |
| 12 | + "_version.txt", |
| 13 | +]); |
| 14 | + |
| 15 | +export function isProductPath(file: string): boolean { |
| 16 | + return PRODUCT_FILES.has(file) || file.startsWith("src/"); |
| 17 | +} |
| 18 | + |
| 19 | +const g = (root: string, args: string[]): string => |
| 20 | + execFileSync("git", args, { cwd: root, encoding: "utf8" }).trim(); |
| 21 | + |
| 22 | +const SEMVER_TAG = /^v\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?$/; |
| 23 | + |
| 24 | +export function latestTag(root = process.cwd()): string | null { |
| 25 | + const out = g(root, ["tag", "--list", "v*", "--sort=-v:refname"]) |
| 26 | + .split("\n") |
| 27 | + .map((l) => l.trim()) |
| 28 | + .filter((l) => SEMVER_TAG.test(l)); |
| 29 | + return out[0] ?? null; |
| 30 | +} |
| 31 | + |
| 32 | +type Level = "major" | "minor" | "patch"; |
| 33 | +const LEVEL_RANK: Record<Level, number> = { patch: 1, minor: 2, major: 3 }; |
| 34 | +const TYPE_LEVEL: Record<string, Level> = { fix: "patch", perf: "patch", feat: "minor" }; |
| 35 | + |
| 36 | +const subjectLevel = (commit: string): Level | null => { |
| 37 | + const firstLine = commit.split("\n")[0] ?? ""; |
| 38 | + const m = /^(?:fix|perf|feat)(?:\([^)]*\))?!?:/.exec(firstLine); |
| 39 | + if (!m) return null; |
| 40 | + if (m[0].includes("!")) return "major"; |
| 41 | + const body = commit.split("\n").slice(1).join("\n"); |
| 42 | + const type = m[0].replace(/\(.*$/, "").replace(/!$/, "").replace(/:$/, ""); |
| 43 | + return /BREAKING[- ]CHANGE:/.test(body) ? "major" : (TYPE_LEVEL[type] ?? null); |
| 44 | +}; |
| 45 | + |
| 46 | +const commitsSince = (root: string, from: string): { message: string; files: string[] }[] => { |
| 47 | + const hashes = g(root, ["log", "--reverse", "--format=%H", `${from}..HEAD`]) |
| 48 | + .split("\n") |
| 49 | + .filter(Boolean); |
| 50 | + return hashes.map((h) => ({ |
| 51 | + message: g(root, ["show", "-s", "--format=%B", h]), |
| 52 | + files: g(root, ["diff-tree", "--no-commit-id", "--name-only", "-r", "-m", "--root", "-z", h]) |
| 53 | + .split("\0") |
| 54 | + .filter(Boolean), |
| 55 | + })); |
| 56 | +}; |
| 57 | + |
| 58 | +export function analyzeReleaseScope(root = process.cwd()): { level: Level | null } { |
| 59 | + const from = latestTag(root); |
| 60 | + if (from === null) return { level: "minor" }; |
| 61 | + const levels: Level[] = []; |
| 62 | + for (const { message, files } of commitsSince(root, from)) { |
| 63 | + if (!files.some(isProductPath)) continue; |
| 64 | + const lvl = subjectLevel(message); |
| 65 | + if (lvl) levels.push(lvl); |
| 66 | + } |
| 67 | + if (levels.length === 0) return { level: null }; |
| 68 | + const level = levels.reduce<Level>( |
| 69 | + (best, l) => (LEVEL_RANK[l] > LEVEL_RANK[best] ? l : best), |
| 70 | + "patch", |
| 71 | + ); |
| 72 | + return { level }; |
| 73 | +} |
| 74 | + |
| 75 | +if (import.meta.main) { |
| 76 | + const root = process.argv[2] ? resolve(process.argv[2]) : process.cwd(); |
| 77 | + const { level } = analyzeReleaseScope(root); |
| 78 | + if (level) process.stdout.write(`${level}\n`); |
| 79 | +} |
0 commit comments