|
| 1 | +// server/typescript/packages/cli/src/commands/upgrade.ts |
| 2 | +// |
| 3 | +// `meta upgrade` — rewrite retired vocabulary in this project's metadata. |
| 4 | +// |
| 5 | +// DELIBERATELY NOT `meta migrate`. That command owns DATABASE SCHEMA (ADR-0015) and an |
| 6 | +// adopter reading `migrate` expects DDL. Overloading it with a metadata rewrite would make |
| 7 | +// the most destructive command in the toolchain ambiguous about what it touches. |
| 8 | +// |
| 9 | +// IT DOES NOT LOAD THE METADATA, and cannot. Once vocabulary is deregistered, metadata |
| 10 | +// carrying it fails the load — which is exactly the state this command exists to repair. So |
| 11 | +// it resolves the file SET through `resolveCollection` (the single authority on where |
| 12 | +// metadata lives) and hands each file's RAW TEXT to the rewriter. See |
| 13 | +// `metadata/src/vocabulary-rewrite.ts` for why that is the only workable shape. |
| 14 | +// |
| 15 | +// DRY-RUN BY DEFAULT, per this repo's convention for anything that edits committed files. |
| 16 | +// `--apply` writes. Refusals exit NON-ZERO even when every mechanical change succeeded, so |
| 17 | +// CI cannot mistake a partial upgrade for a finished one. |
| 18 | + |
| 19 | +import { readFile, writeFile } from "node:fs/promises"; |
| 20 | +import { relative } from "node:path"; |
| 21 | +import { resolveCollection } from "@metaobjectsdev/sdk"; |
| 22 | +import { rewriteDocument, type RewriteResult } from "@metaobjectsdev/metadata"; |
| 23 | +import { log } from "../lib/log.js"; |
| 24 | + |
| 25 | +interface UpgradeFlags { |
| 26 | + apply: boolean; |
| 27 | + maxVersion?: string; |
| 28 | + projectRoot?: string; |
| 29 | +} |
| 30 | + |
| 31 | +function parseArgs(argv: string[]): UpgradeFlags { |
| 32 | + const flags: UpgradeFlags = { apply: false }; |
| 33 | + for (let i = 0; i < argv.length; i++) { |
| 34 | + const a = argv[i] as string; |
| 35 | + if (a === "--apply") flags.apply = true; |
| 36 | + else if (a === "--to") { |
| 37 | + const v = argv[++i]; |
| 38 | + if (v === undefined) throw new Error("--to needs a version"); |
| 39 | + flags.maxVersion = v; |
| 40 | + } else if (a.startsWith("--to=")) flags.maxVersion = a.slice("--to=".length); |
| 41 | + else if (a === "--help" || a === "-h") throw new Error("__help__"); |
| 42 | + else if (!a.startsWith("-")) flags.projectRoot = a; |
| 43 | + else throw new Error(`unknown option: ${a}`); |
| 44 | + } |
| 45 | + return flags; |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * The node type a document's keys belong to. |
| 50 | + * |
| 51 | + * Retirements are TYPE-SCOPED — `@unique` is retired on `identity.secondary` and live on a |
| 52 | + * field — so the rewriter needs a scope per occurrence, not per file. A metadata file holds |
| 53 | + * many types, so we run the rewriter once per type key present in the text. Cheap, and it |
| 54 | + * keeps the scoping decision in one place (the map) rather than smeared across a parser we |
| 55 | + * deliberately do not have. |
| 56 | + */ |
| 57 | +function typeKeysIn(text: string): string[] { |
| 58 | + const keys = new Set<string>(); |
| 59 | + const re = /"([a-z]+)\.([A-Za-z*]+)"\s*:/g; |
| 60 | + let m: RegExpExecArray | null; |
| 61 | + while ((m = re.exec(text)) !== null) keys.add(`${m[1]}.${m[2]}`); |
| 62 | + return [...keys]; |
| 63 | +} |
| 64 | + |
| 65 | +/** Run every type scope present in the document, threading the text through each pass. */ |
| 66 | +function rewriteAllScopes(text: string, maxVersion: string | undefined): RewriteResult { |
| 67 | + const changes: RewriteResult["changes"][number][] = []; |
| 68 | + const refusals: RewriteResult["refusals"][number][] = []; |
| 69 | + let current = text; |
| 70 | + for (const typeKeyHint of typeKeysIn(text)) { |
| 71 | + const r = rewriteDocument(current, { |
| 72 | + typeKeyHint, |
| 73 | + ...(maxVersion !== undefined ? { maxVersion } : {}), |
| 74 | + }); |
| 75 | + current = r.text; |
| 76 | + changes.push(...r.changes); |
| 77 | + refusals.push(...r.refusals); |
| 78 | + } |
| 79 | + return { text: current, changes, refusals }; |
| 80 | +} |
| 81 | + |
| 82 | +export async function upgradeCommand(args: string[], cwd: string): Promise<number> { |
| 83 | + let flags: UpgradeFlags; |
| 84 | + try { |
| 85 | + flags = parseArgs(args); |
| 86 | + } catch (err) { |
| 87 | + if ((err as Error).message === "__help__") { |
| 88 | + log.info( |
| 89 | + "meta upgrade [<project>] [--to <version>] [--apply]\n\n" + |
| 90 | + " Rewrites retired metadata vocabulary. Previews by default; --apply writes.\n" + |
| 91 | + " Retirements needing a human decision are REFUSED and listed with their guide.", |
| 92 | + ); |
| 93 | + return 0; |
| 94 | + } |
| 95 | + log.error((err as Error).message); |
| 96 | + return 2; |
| 97 | + } |
| 98 | + |
| 99 | + const projectRoot = flags.projectRoot ?? cwd; |
| 100 | + |
| 101 | + let files: readonly string[]; |
| 102 | + try { |
| 103 | + files = (await resolveCollection(projectRoot)).files; |
| 104 | + } catch (err) { |
| 105 | + log.error((err as Error).message); |
| 106 | + return 1; |
| 107 | + } |
| 108 | + |
| 109 | + let totalChanges = 0; |
| 110 | + let totalRefusals = 0; |
| 111 | + let filesChanged = 0; |
| 112 | + |
| 113 | + for (const file of files) { |
| 114 | + const before = await readFile(file, "utf8"); |
| 115 | + const r = rewriteAllScopes(before, flags.maxVersion); |
| 116 | + if (r.changes.length === 0 && r.refusals.length === 0) continue; |
| 117 | + |
| 118 | + const rel = relative(projectRoot, file); |
| 119 | + log.info(`\n${rel}`); |
| 120 | + for (const c of r.changes) log.info(` ${c.line}: @${c.from} → ${c.to}`); |
| 121 | + for (const f of r.refusals) { |
| 122 | + log.warn( |
| 123 | + ` ${f.line}: @${f.attr}${f.value !== undefined ? `: ${f.value}` : ""} — needs a decision. ` + |
| 124 | + `Retired in ${f.since}. ${f.migration !== undefined ? `See ${f.migration}` : f.why}`, |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | + totalChanges += r.changes.length; |
| 129 | + totalRefusals += r.refusals.length; |
| 130 | + if (r.changes.length > 0) { |
| 131 | + filesChanged++; |
| 132 | + if (flags.apply) await writeFile(file, r.text, "utf8"); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + log.info(""); |
| 137 | + if (totalChanges === 0 && totalRefusals === 0) { |
| 138 | + log.info("meta upgrade — no retired vocabulary found."); |
| 139 | + return 0; |
| 140 | + } |
| 141 | + |
| 142 | + if (flags.apply) { |
| 143 | + log.info(`meta upgrade — rewrote ${totalChanges} declaration(s) across ${filesChanged} file(s).`); |
| 144 | + } else { |
| 145 | + log.info( |
| 146 | + `meta upgrade — ${totalChanges} declaration(s) in ${filesChanged} file(s) can be rewritten. ` + |
| 147 | + `Re-run with --apply to write.`, |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + // Non-zero while ANY refusal stands, applied or not. A partial upgrade that exited 0 would |
| 152 | + // let CI record the migration as done while metadata still fails to load. |
| 153 | + if (totalRefusals > 0) { |
| 154 | + log.error( |
| 155 | + `${totalRefusals} declaration(s) need a human decision and were left untouched — ` + |
| 156 | + `see the guides listed above.`, |
| 157 | + ); |
| 158 | + return 1; |
| 159 | + } |
| 160 | + return 0; |
| 161 | +} |
0 commit comments