27 lines
1.1 KiB
TypeScript
27 lines
1.1 KiB
TypeScript
import { InstalledRuntime, RemoteRuntime } from '../types'
|
|
|
|
export function sameRuntimeId(a: string, b: string): boolean {
|
|
const [baseA, revA = ''] = a.split('-', 2)
|
|
const [baseB, revB = ''] = b.split('-', 2)
|
|
return revA === revB && (baseA.startsWith(baseB) || baseB.startsWith(baseA))
|
|
}
|
|
|
|
interface RuntimeStatus {
|
|
active?: InstalledRuntime
|
|
latestCommit: string
|
|
activeIsLatest: boolean
|
|
latestInstalled: boolean
|
|
updatable: boolean
|
|
switchRecommended: boolean
|
|
}
|
|
|
|
export function describeRuntimeStatus(installed: InstalledRuntime[], latest?: RemoteRuntime): RuntimeStatus {
|
|
const active = installed.find((i) => i.active) ?? installed[0]
|
|
const latestCommit = latest?.commit ?? ''
|
|
const activeIsLatest = !!active && !!latestCommit && sameRuntimeId(active.commit, latestCommit)
|
|
const latestInstalled = !!latestCommit && installed.some((i) => sameRuntimeId(i.commit, latestCommit))
|
|
const updatable = !!latestCommit && !latestInstalled
|
|
const switchRecommended = !!active && !!latestCommit && !activeIsLatest && latestInstalled
|
|
return { active, latestCommit, activeIsLatest, latestInstalled, updatable, switchRecommended }
|
|
}
|