|
| 1 | +import crypto from "node:crypto"; |
| 2 | +import fs from "node:fs"; |
| 3 | +import os from "node:os"; |
| 4 | +import path from "node:path"; |
| 5 | +import { execFileSync } from "node:child_process"; |
| 6 | +import { parse as parseYaml } from "yaml"; |
| 7 | +import { parseOciReference } from "../resolvers/oci"; |
| 8 | + |
| 9 | +type WriteLine = (line: string) => void; |
| 10 | + |
| 11 | +export type RunCheckCommandOptions = { |
| 12 | + cwd?: string; |
| 13 | + homeDir?: string; |
| 14 | + stdout?: WriteLine; |
| 15 | + stderr?: WriteLine; |
| 16 | +}; |
| 17 | + |
| 18 | +export type LockfileSourceEntry = { |
| 19 | + type: string; |
| 20 | + url: string; |
| 21 | + ref?: string; |
| 22 | + digest?: string; |
| 23 | + installMethod: string; |
| 24 | + skills: string[]; |
| 25 | + agents: string[]; |
| 26 | +}; |
| 27 | + |
| 28 | +export type CheckStatus = "up-to-date" | "outdated" | "unsupported" | "error"; |
| 29 | + |
| 30 | +export type CheckResult = { |
| 31 | + source: LockfileSourceEntry; |
| 32 | + skill: string; |
| 33 | + status: CheckStatus; |
| 34 | + currentDigest?: string; |
| 35 | + latestDigest?: string; |
| 36 | + message?: string; |
| 37 | +}; |
| 38 | + |
| 39 | +export async function runCheckCommand( |
| 40 | + args: string[], |
| 41 | + options: RunCheckCommandOptions = {} |
| 42 | +): Promise<number> { |
| 43 | + const stdout = options.stdout ?? ((line: string) => console.log(line)); |
| 44 | + const stderr = options.stderr ?? ((line: string) => console.error(line)); |
| 45 | + |
| 46 | + let parsed: { global: boolean }; |
| 47 | + try { |
| 48 | + parsed = parseCheckArgs(args); |
| 49 | + } catch (error) { |
| 50 | + stderr(error instanceof Error ? error.message : String(error)); |
| 51 | + return 1; |
| 52 | + } |
| 53 | + const cwd = path.resolve(options.cwd ?? process.cwd()); |
| 54 | + const homeDir = path.resolve(options.homeDir ?? os.homedir()); |
| 55 | + |
| 56 | + const lock = loadLockfile(parsed.global ? "global" : "project", cwd, homeDir); |
| 57 | + if (!lock) { |
| 58 | + stderr("No lockfile found. Run add first to generate skillet.lock.yaml."); |
| 59 | + return 1; |
| 60 | + } |
| 61 | + |
| 62 | + const results = await collectCheckResults(lock.sources); |
| 63 | + for (const result of results) { |
| 64 | + stdout(`${result.skill}\t${result.status}\t${result.source.url}`); |
| 65 | + } |
| 66 | + |
| 67 | + return 0; |
| 68 | +} |
| 69 | + |
| 70 | +export async function collectCheckResults(sources: LockfileSourceEntry[]): Promise<CheckResult[]> { |
| 71 | + const results: CheckResult[] = []; |
| 72 | + |
| 73 | + for (const source of sources) { |
| 74 | + const status = await checkSource(source); |
| 75 | + |
| 76 | + for (const skill of source.skills) { |
| 77 | + results.push({ |
| 78 | + source, |
| 79 | + skill, |
| 80 | + status: status.status, |
| 81 | + currentDigest: source.digest, |
| 82 | + latestDigest: status.latestDigest, |
| 83 | + message: status.message, |
| 84 | + }); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return results.sort((left, right) => left.skill.localeCompare(right.skill)); |
| 89 | +} |
| 90 | + |
| 91 | +async function checkSource(source: LockfileSourceEntry): Promise<{ |
| 92 | + status: CheckStatus; |
| 93 | + latestDigest?: string; |
| 94 | + message?: string; |
| 95 | +}> { |
| 96 | + if (source.type === "git") { |
| 97 | + try { |
| 98 | + const latestDigest = getLatestGitDigest(source.url, source.ref); |
| 99 | + if (source.digest && latestDigest !== source.digest) { |
| 100 | + return { status: "outdated", latestDigest }; |
| 101 | + } |
| 102 | + |
| 103 | + return { status: "up-to-date", latestDigest }; |
| 104 | + } catch (error) { |
| 105 | + return { |
| 106 | + status: "error", |
| 107 | + message: error instanceof Error ? error.message : String(error), |
| 108 | + }; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + if (source.type === "oci") { |
| 113 | + try { |
| 114 | + const latestDigest = await getLatestOciDigest(source.url); |
| 115 | + if (source.digest && latestDigest !== source.digest) { |
| 116 | + return { status: "outdated", latestDigest }; |
| 117 | + } |
| 118 | + |
| 119 | + return { status: "up-to-date", latestDigest }; |
| 120 | + } catch (error) { |
| 121 | + return { |
| 122 | + status: "error", |
| 123 | + message: error instanceof Error ? error.message : String(error), |
| 124 | + }; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return { |
| 129 | + status: "unsupported", |
| 130 | + message: `Source type '${source.type}' is not checked for updates`, |
| 131 | + }; |
| 132 | +} |
| 133 | + |
| 134 | +function parseCheckArgs(args: string[]): { global: boolean } { |
| 135 | + let global = false; |
| 136 | + |
| 137 | + for (const token of args) { |
| 138 | + if (token === "-g" || token === "--global") { |
| 139 | + global = true; |
| 140 | + continue; |
| 141 | + } |
| 142 | + |
| 143 | + if (token.startsWith("-")) { |
| 144 | + throw new Error(`Unknown option: ${token}`); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + return { global }; |
| 149 | +} |
| 150 | + |
| 151 | +export function loadLockfile( |
| 152 | + scope: "project" | "global", |
| 153 | + cwd: string, |
| 154 | + homeDir: string |
| 155 | +): { path: string; sources: LockfileSourceEntry[] } | undefined { |
| 156 | + const lockPath = |
| 157 | + scope === "project" ? path.join(cwd, "skillet.lock.yaml") : path.join(homeDir, ".skillet", "skillet.lock.yaml"); |
| 158 | + |
| 159 | + if (!fs.existsSync(lockPath)) { |
| 160 | + return undefined; |
| 161 | + } |
| 162 | + |
| 163 | + const parsed = parseYaml(fs.readFileSync(lockPath, "utf8")) as { |
| 164 | + sources?: LockfileSourceEntry[]; |
| 165 | + }; |
| 166 | + |
| 167 | + return { |
| 168 | + path: lockPath, |
| 169 | + sources: parsed.sources ?? [], |
| 170 | + }; |
| 171 | +} |
| 172 | + |
| 173 | +function getLatestGitDigest(url: string, ref?: string): string { |
| 174 | + const requestedRef = ref && ref.length > 0 ? ref : "HEAD"; |
| 175 | + const output = runGit(["ls-remote", url, requestedRef]); |
| 176 | + const firstLine = output.split(/\r?\n/).find((line) => line.trim().length > 0); |
| 177 | + |
| 178 | + if (!firstLine) { |
| 179 | + throw new Error(`Unable to resolve latest git digest for ${url} (${requestedRef})`); |
| 180 | + } |
| 181 | + |
| 182 | + const [digest] = firstLine.split(/\s+/, 2); |
| 183 | + if (!digest || digest.length < 40) { |
| 184 | + throw new Error(`Unexpected git ls-remote output for ${url}`); |
| 185 | + } |
| 186 | + |
| 187 | + return digest; |
| 188 | +} |
| 189 | + |
| 190 | +function runGit(args: string[]): string { |
| 191 | + try { |
| 192 | + return execFileSync("git", args, { |
| 193 | + encoding: "utf8", |
| 194 | + stdio: ["ignore", "pipe", "pipe"], |
| 195 | + }).trim(); |
| 196 | + } catch (error) { |
| 197 | + const stderr = |
| 198 | + error instanceof Error && "stderr" in error && typeof error.stderr === "string" |
| 199 | + ? error.stderr.trim() |
| 200 | + : ""; |
| 201 | + const details = stderr ? `: ${stderr}` : ""; |
| 202 | + throw new Error(`git ${args.join(" ")} failed${details}`); |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +async function getLatestOciDigest(source: string): Promise<string> { |
| 207 | + const parsed = parseOciReference(source); |
| 208 | + if (parsed.referenceType === "digest") { |
| 209 | + return parsed.reference; |
| 210 | + } |
| 211 | + |
| 212 | + const scheme = parsed.registry.startsWith("localhost") || parsed.registry.startsWith("127.0.0.1") ? "http" : "https"; |
| 213 | + const url = `${scheme}://${parsed.registry}/v2/${parsed.repository}/manifests/${parsed.reference}`; |
| 214 | + |
| 215 | + const response = await fetch(url, { |
| 216 | + headers: { |
| 217 | + Accept: "application/vnd.oci.image.manifest.v1+json, application/vnd.oci.artifact.manifest.v1+json", |
| 218 | + }, |
| 219 | + }); |
| 220 | + |
| 221 | + if (!response.ok) { |
| 222 | + throw new Error(`OCI manifest request failed: ${response.status} ${response.statusText}`); |
| 223 | + } |
| 224 | + |
| 225 | + const headerDigest = response.headers.get("docker-content-digest"); |
| 226 | + if (headerDigest) { |
| 227 | + return headerDigest; |
| 228 | + } |
| 229 | + |
| 230 | + const bodyText = await response.text(); |
| 231 | + return `sha256:${crypto.createHash("sha256").update(bodyText).digest("hex")}`; |
| 232 | +} |
0 commit comments