|
| 1 | +import { goldenTemplate as encodeHlsVideo001 } from './encode-hls-video/0.0.1.ts' |
| 2 | +import type { GoldenTemplate, GoldenTemplateDefinition } from './types.ts' |
| 3 | + |
| 4 | +export type { GoldenTemplate, GoldenTemplateDefinition } |
| 5 | + |
| 6 | +export const goldenTemplates = { |
| 7 | + [encodeHlsVideo001.slug]: encodeHlsVideo001, |
| 8 | +} satisfies Record<string, GoldenTemplateDefinition> |
| 9 | + |
| 10 | +const goldenTemplatesByKey: Record<string, GoldenTemplateDefinition> = goldenTemplates |
| 11 | + |
| 12 | +function parseSemver(version: string): [number, number, number] { |
| 13 | + const parts = version.split('.') |
| 14 | + const readPart = (index: number): number => { |
| 15 | + const part = parts[index] ?? '' |
| 16 | + const match = part.match(/^\d+/) |
| 17 | + return match ? Number(match[0]) : 0 |
| 18 | + } |
| 19 | + return [readPart(0), readPart(1), readPart(2)] |
| 20 | +} |
| 21 | + |
| 22 | +function compareSemver(a: string, b: string): number { |
| 23 | + const [aMajor, aMinor, aPatch] = parseSemver(a) |
| 24 | + const [bMajor, bMinor, bPatch] = parseSemver(b) |
| 25 | + if (aMajor !== bMajor) { |
| 26 | + return aMajor - bMajor |
| 27 | + } |
| 28 | + if (aMinor !== bMinor) { |
| 29 | + return aMinor - bMinor |
| 30 | + } |
| 31 | + if (aPatch !== bPatch) { |
| 32 | + return aPatch - bPatch |
| 33 | + } |
| 34 | + if (a === b) { |
| 35 | + return 0 |
| 36 | + } |
| 37 | + return a < b ? -1 : 1 |
| 38 | +} |
| 39 | + |
| 40 | +function versionFromKey(key: string): string { |
| 41 | + const [, version] = key.split('@') |
| 42 | + return version ?? '' |
| 43 | +} |
| 44 | + |
| 45 | +function compareGoldenKeys(a: string, b: string): number { |
| 46 | + return compareSemver(versionFromKey(a), versionFromKey(b)) |
| 47 | +} |
| 48 | + |
| 49 | +export function resolveGoldenTemplateKey(slug: string): string | null { |
| 50 | + if (slug.includes('@')) { |
| 51 | + return goldenTemplatesByKey[slug] ? slug : null |
| 52 | + } |
| 53 | + |
| 54 | + const matches = Object.keys(goldenTemplatesByKey).filter((key) => key.startsWith(`${slug}@`)) |
| 55 | + if (matches.length === 0) { |
| 56 | + return null |
| 57 | + } |
| 58 | + |
| 59 | + let latest = matches[0] |
| 60 | + for (const candidate of matches.slice(1)) { |
| 61 | + if (compareGoldenKeys(candidate, latest) > 0) { |
| 62 | + latest = candidate |
| 63 | + } |
| 64 | + } |
| 65 | + return latest |
| 66 | +} |
| 67 | + |
| 68 | +export function resolveGoldenTemplate(slug: string): GoldenTemplate | null { |
| 69 | + const key = resolveGoldenTemplateKey(slug) |
| 70 | + return key ? goldenTemplatesByKey[key] : null |
| 71 | +} |
| 72 | + |
| 73 | +export function listGoldenTemplateKeys(include: 'latest' | 'all' = 'latest'): string[] { |
| 74 | + const keys = Object.keys(goldenTemplatesByKey) |
| 75 | + if (include === 'all') { |
| 76 | + return keys |
| 77 | + } |
| 78 | + |
| 79 | + const latestBySlug = new Map<string, string>() |
| 80 | + for (const key of keys) { |
| 81 | + const base = key.split('@')[0] |
| 82 | + const existing = latestBySlug.get(base) |
| 83 | + if (!existing || compareGoldenKeys(key, existing) > 0) { |
| 84 | + latestBySlug.set(base, key) |
| 85 | + } |
| 86 | + } |
| 87 | + return [...latestBySlug.values()] |
| 88 | +} |
0 commit comments