import { isSafeVersion } from "./safeVersion.js"; // Package-name characters before the name belong to another package, including // scoped names: a word boundary also matches after their slash, hyphen or dot. // The version shape follows isSafeVersion; group 1 captures the current pin. export const HYPERFRAMES_PIN_RE = /(?; fromVersions: string[]; } export interface TextPinRewriteResult { changed: boolean; text: string; fromVersions: string[]; } export function rewritePinnedHyperframesText( text: string, targetVersion: string, ): TextPinRewriteResult { if (!isSafeVersion(targetVersion)) { return { changed: false, text, fromVersions: [] }; } const fromVersions = new Set(); const next = text.replace(HYPERFRAMES_PIN_RE, (_full, version: string) => { if (version !== targetVersion) fromVersions.add(version); return `hyperframes@${targetVersion}`; }); return { changed: fromVersions.size > 0, text: next, fromVersions: [...fromVersions].sort(), }; } export function readPinnedHyperframesVersions(scripts: Record): string[] { const found = new Set(); for (const cmd of Object.values(scripts ?? {})) { for (const m of cmd.matchAll(HYPERFRAMES_PIN_RE)) if (m[1]) found.add(m[1]); } return [...found].sort(); } export function rewriteProjectPinnedScripts( scripts: Record, targetVersion: string, ): PinRewriteResult { // Never emit an unverified version into a script the user (or npx) will run. if (!isSafeVersion(targetVersion)) { return { changed: false, scripts: { ...scripts }, fromVersions: [] }; } const fromVersions = new Set(); const next: Record = {}; for (const [name, cmd] of Object.entries(scripts ?? {})) { const rewrite = rewritePinnedHyperframesText(cmd, targetVersion); next[name] = rewrite.text; for (const version of rewrite.fromVersions) fromVersions.add(version); } return { changed: [...fromVersions].length > 0, scripts: next, fromVersions: [...fromVersions].sort(), }; }