mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
69 lines
2.2 KiB
TypeScript
69 lines
2.2 KiB
TypeScript
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 =
|
|
/(?<![0-9A-Za-z._/@-])hyperframes@([0-9]+\.[0-9]+\.[0-9]+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?)(?=\s|$)/g;
|
|
|
|
export interface PinRewriteResult {
|
|
changed: boolean;
|
|
scripts: Record<string, string>;
|
|
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<string>();
|
|
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, string>): string[] {
|
|
const found = new Set<string>();
|
|
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<string, string>,
|
|
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<string>();
|
|
const next: Record<string, string> = {};
|
|
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(),
|
|
};
|
|
}
|