|
| 1 | +/* eslint-disable no-console */ |
| 2 | +import fs from "node:fs/promises"; |
| 3 | +import path from "node:path"; |
| 4 | +import { fileURLToPath } from "node:url"; |
| 5 | +import { simpleGit } from "simple-git"; |
| 6 | +import pkgJson from "../package.json" with { type: "json" }; |
| 7 | + |
| 8 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 9 | +const rootPath = path.join(__dirname, ".."); |
| 10 | +const git = simpleGit(rootPath); |
| 11 | + |
| 12 | +const VALID_BUMPS = new Set(["major", "minor", "patch"]); |
| 13 | +const FRONTMATTER_RE = /^---\r?\n([\s\S]*?)\r?\n---(?:\r?\n|$)/; |
| 14 | +const ENTRY_RE = /^"([^"]+)"\s*:\s*([a-zA-Z]+)\s*$/; |
| 15 | + |
| 16 | +const toLines = (output) => |
| 17 | + output |
| 18 | + .split(/\r?\n/) |
| 19 | + .map((line) => line.trim()) |
| 20 | + .filter(Boolean); |
| 21 | + |
| 22 | +const isChangeset = (filePath) => { |
| 23 | + const normalized = filePath.replaceAll("\\", "/"); |
| 24 | + return ( |
| 25 | + normalized.startsWith(".changeset/") && |
| 26 | + normalized.endsWith(".md") && |
| 27 | + normalized !== ".changeset/README.md" |
| 28 | + ); |
| 29 | +}; |
| 30 | + |
| 31 | +const gitDiff = async (more = []) => { |
| 32 | + const args = [ |
| 33 | + "diff", |
| 34 | + "--name-only", |
| 35 | + // cspell:ignore ACMR |
| 36 | + "--diff-filter=ACMR", |
| 37 | + ...more, |
| 38 | + "--", |
| 39 | + ".changeset/*.md", |
| 40 | + ].filter(Boolean); |
| 41 | + |
| 42 | + return toLines(await git.raw(args)); |
| 43 | +}; |
| 44 | + |
| 45 | +const getChangedFiles = async () => { |
| 46 | + const files = new Set(); |
| 47 | + const baseRef = process.env.GITHUB_BASE_REF; |
| 48 | + |
| 49 | + // GitHub Actions base diff |
| 50 | + if (baseRef) { |
| 51 | + for (const file of await gitDiff([`origin/${baseRef}...HEAD`])) { |
| 52 | + if (isChangeset(file)) files.add(file); |
| 53 | + } |
| 54 | + } |
| 55 | + // Local working tree changes |
| 56 | + else { |
| 57 | + const _files = [ |
| 58 | + // Unstaged changes |
| 59 | + ...(await gitDiff()), |
| 60 | + // Staged but uncommitted changes |
| 61 | + ...(await gitDiff(["--cached"])), |
| 62 | + // Untracked files |
| 63 | + ...(await git.status()).not_added, |
| 64 | + ]; |
| 65 | + for (const file of _files) { |
| 66 | + if (isChangeset(file)) files.add(file); |
| 67 | + } |
| 68 | + } |
| 69 | + return files; |
| 70 | +}; |
| 71 | + |
| 72 | +const validate = async (filePath) => { |
| 73 | + const absoluteFilePath = path.join(rootPath, filePath); |
| 74 | + const content = await fs.readFile(absoluteFilePath, "utf8"); |
| 75 | + const frontmatterMatch = content.match(FRONTMATTER_RE); |
| 76 | + const errors = []; |
| 77 | + |
| 78 | + if (!frontmatterMatch) { |
| 79 | + errors.push("missing YAML frontmatter block"); |
| 80 | + return errors; |
| 81 | + } |
| 82 | + |
| 83 | + const entries = frontmatterMatch[1] |
| 84 | + .split(/\r?\n/) |
| 85 | + .map((line) => line.trim()) |
| 86 | + .filter(Boolean); |
| 87 | + |
| 88 | + if (entries.length === 0) { |
| 89 | + errors.push("frontmatter does not contain package bump entries"); |
| 90 | + return errors; |
| 91 | + } |
| 92 | + |
| 93 | + for (const entry of entries) { |
| 94 | + const match = entry.match(ENTRY_RE); |
| 95 | + if (!match) { |
| 96 | + errors.push(`invalid frontmatter entry: ${entry}`); |
| 97 | + continue; |
| 98 | + } |
| 99 | + |
| 100 | + const [, pkgName, bumpType] = match; |
| 101 | + if (pkgName !== pkgJson.name) { |
| 102 | + errors.push( |
| 103 | + `invalid package name "${pkgName}", expected "${pkgJson.name}"`, |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + if (!VALID_BUMPS.has(bumpType)) { |
| 108 | + errors.push( |
| 109 | + `invalid bump type "${bumpType}", expected one of: major, minor, patch`, |
| 110 | + ); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return errors; |
| 115 | +}; |
| 116 | + |
| 117 | +const changedFiles = await getChangedFiles(); |
| 118 | + |
| 119 | +if (changedFiles.size === 0) { |
| 120 | + console.log("No changed changeset files found."); |
| 121 | +} else { |
| 122 | + const failures = []; |
| 123 | + for (const filePath of changedFiles) { |
| 124 | + const errors = await validate(filePath); |
| 125 | + for (const error of errors) { |
| 126 | + failures.push(`${filePath}: ${error}`); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + if (failures.length > 0) { |
| 131 | + console.error("Changeset validation failed:"); |
| 132 | + for (const failure of failures) { |
| 133 | + console.error(`- ${failure}`); |
| 134 | + } |
| 135 | + process.exitCode = 1; |
| 136 | + } |
| 137 | +} |
0 commit comments