Something went wrong. Try again.
Personal OMP plugins
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145#!/usr/bin/env bun/** * Bump one plugin release and synchronize repository version metadata. * * Usage: bun run scripts/version-plugin.ts <plugin> <major|minor|patch> [--dry-run] */import { existsSync, readFileSync, writeFileSync } from "node:fs";import { join, resolve } from "node:path";
type BumpKind = "major" | "minor" | "patch";
export interface ParsedVersion { major: number; minor: number; patch: number; suffix: string;}
const ROOT = resolve(import.meta.dirname, "..");const VERSION_PATTERN = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-ezxomp\.(0|[1-9]\d*))?$/;const SLUG_PATTERN = /^[a-z0-9](?:[a-z0-9.-]*[a-z0-9])?$/;
function escapeRegExp(value: string): string { return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");}
function replaceExactly(text: string, pattern: RegExp, replacement: string, label: string): string { const matches = [...text.matchAll(new RegExp(pattern.source, "g"))]; if (matches.length !== 1) throw new Error(`Expected one ${label}, found ${matches.length}`); return text.replace(pattern, replacement);}
function replaceField(text: string, field: string, version: string, label: string): string { const pattern = new RegExp(`(\"${escapeRegExp(field)}\"\\s*:\\s*)\"[^\"]+\"`); return replaceExactly(text, pattern, `$1"${version}"`, label);}
export function parseVersion(value: string): ParsedVersion | null { const match = VERSION_PATTERN.exec(value); if (!match) return null; return { major: Number(match[1]), minor: Number(match[2]), patch: Number(match[3]), suffix: match[4] ?? "", };}
export function bumpVersion(current: string, kind: BumpKind): string { const parsed = parseVersion(current); if (!["major", "minor", "patch"].includes(kind)) throw new Error(`Invalid bump kind: ${kind}`); if (!parsed) throw new Error(`Unsupported plugin version: ${current}`); const versions = { major: `${parsed.major + 1}.0.0`, minor: `${parsed.major}.${parsed.minor + 1}.0`, patch: `${parsed.major}.${parsed.minor}.${parsed.patch + 1}`, }; return `${versions[kind]}-ezxomp.0`;}
function replaceCatalogVersion(text: string, slug: string, version: string): string { const nameIndex = text.indexOf(`"name": "${slug}"`); if (nameIndex < 0) throw new Error(`Marketplace entry not found: ${slug}`); const entryStart = text.lastIndexOf("\n {", nameIndex); const entryEnd = text.indexOf("\n }", nameIndex); if (entryStart < 0 || entryEnd < 0) throw new Error(`Malformed marketplace entry: ${slug}`); const entry = text.slice(entryStart, entryEnd); return text.slice(0, entryStart) + replaceField(entry, "version", version, `marketplace version for ${slug}`) + text.slice(entryEnd);}
function replaceMapVersion(text: string, mapName: string, slug: string, version: string): string { const start = text.indexOf(`const ${mapName}`); const end = text.indexOf("};", start); if (start < 0 || end < 0) { return text; } const key = escapeRegExp(slug); const pattern = new RegExp(`((?:\"${key}\"|${key})\\s*:\\s*)\"[^\"]+\"`); const body = text.slice(start, end); if (!pattern.test(body)) { return text; } return text.slice(0, start) + replaceExactly(body, pattern, `$1"${version}"`, `${mapName} version for ${slug}`) + text.slice(end);}
function replaceReadmeVersion(text: string, slug: string, version: string): string { const pattern = new RegExp(`(\\| \`${escapeRegExp(slug)}\` \\| )[^|]+( \\|)`); return pattern.test(text) ? replaceExactly(text, pattern, `$1${version}$2`, `README version for ${slug}`) : text;}
export function bumpPlugin(root: string, slug: string, kind: BumpKind, dryRun = false): string { if (!SLUG_PATTERN.test(slug)) throw new Error(`Invalid plugin slug: ${slug}`); const pluginDir = join(root, "plugins", slug); const packagePath = join(pluginDir, "package.json"); const catalogPath = join(root, ".omp-plugin", "marketplace.json"); const upstreamPath = join(pluginDir, "UPSTREAM.json"); const importPath = join(root, "scripts", "import-upstreams.ts"); const readmePath = join(root, "README.md");
const next = bumpVersion(packageVersion(packagePath, slug), kind); const packageText = replaceField(readFileSync(packagePath, "utf8"), "version", next, `package version for ${slug}`); const catalogText = replaceCatalogVersion(readFileSync(catalogPath, "utf8"), slug, next); const upstreamText = replaceField(readFileSync(upstreamPath, "utf8"), "localForkVersion", next, `localForkVersion for ${slug}`); const importText = replaceMapVersion(readFileSync(importPath, "utf8"), "FORK_VERSIONS", slug, next); const readmeText = existsSync(readmePath) ? replaceReadmeVersion(readFileSync(readmePath, "utf8"), slug, next) : null;
if (!dryRun) writeVersions([[packagePath, packageText], [catalogPath, catalogText], [upstreamPath, upstreamText], [importPath, importText], ...readmeUpdates(readmePath, readmeText)]); return next;}
function packageVersion(path: string, slug: string): string { let pkg: { version?: string }; try { pkg = JSON.parse(readFileSync(path, "utf8")); } catch { throw new Error(`Invalid package manifest: ${slug}`); } if (!pkg.version) throw new Error(`Plugin has no version: ${slug}`); return pkg.version;}
function readmeUpdates(path: string, text: string | null): [string, string][] { return text === null ? [] : [[path, text]];}
function writeVersions(updates: [string, string][]): void { for (const [path, text] of updates) writeFileSync(path, text);}
function usage(): never { console.error("Usage: bun run scripts/version-plugin.ts <plugin> <major|minor|patch> [--dry-run]"); process.exit(2);}
if (import.meta.main) { const args = process.argv.slice(2); const slug = args[0]; const kind = args[1] as BumpKind | undefined; if (!slug || !kind || !["major", "minor", "patch"].includes(kind)) usage(); try { const next = bumpPlugin(ROOT, slug, kind, args.includes("--dry-run")); console.log(`${slug}: ${next}${args.includes("--dry-run") ? " (dry run)" : ""}`); } catch (error) { console.error(`ERROR: ${(error as Error).message}`); process.exit(1); }}