diff --git a/packages/desktop/scripts/publish.ts b/packages/desktop/scripts/publish.ts index 45aa120b930..4e187442fdb 100644 --- a/packages/desktop/scripts/publish.ts +++ b/packages/desktop/scripts/publish.ts @@ -4,20 +4,26 @@ import { Script } from "@opencode/script" import { UpdateArtifact } from "../../../script/update-artifact" const dryRun = process.argv.includes("--dry-run") + if (!Script.release) { console.log("skipped desktop publication without a release") process.exit(0) } + const directory = process.env.OPENCODE_DESKTOP_DIST + if (!directory) throw new Error("OPENCODE_DESKTOP_DIST is required") + const files = ( await Array.fromAsync( new Bun.Glob("*.{exe,blockmap,dmg,zip,AppImage,deb,rpm,app.tar.gz}").scan({ cwd: directory, absolute: true }), ) ).sort() + if (!files.length) throw new Error("No desktop release files found") const uploaded = await UpdateArtifact.upload({ version: Script.version, files, dryRun }) + const artifact = { channel: Script.channel, name: "desktop", @@ -25,7 +31,9 @@ const artifact = { version: Script.version, metadata: { files: uploaded, ...(await metadata(Script.version, uploaded)) }, } + if (dryRun) console.log(`dry-run artifact: ${JSON.stringify(artifact)}`) + if (!dryRun) await UpdateArtifact.publish(artifact) type DesktopFile = { @@ -37,7 +45,9 @@ type DesktopFile = { async function metadata(version: string, files: Record) { const directory = process.env.LATEST_YML_DIR + if (!directory) throw new Error("LATEST_YML_DIR is required") + const entries = await Promise.all( [ { @@ -64,54 +74,75 @@ async function metadata(version: string, files: Record) await Promise.all( item.sources.map(async ([subdirectory, source]) => { const file = Bun.file(`${directory}/${subdirectory}/${source}`) + if (!(await file.exists())) return undefined + return parse(await file.text(), version, files) }), ) ).filter((manifest) => manifest !== undefined) + if (manifests.length !== item.sources.length) return undefined + return [ item.name, { files: manifests.flatMap((manifest) => manifest.files), releaseDate: manifests[0]!.releaseDate }, ] as const }), ) + if (entries.some((entry) => entry === undefined)) throw new Error("Desktop update metadata is incomplete") const manifests = Object.fromEntries(entries.filter((entry) => entry !== undefined)) + return { manifests } } function parse(content: string, version: string, uploaded: Record) { const lines = content.split("\n") + const found = lines .find((line) => line.startsWith("version:")) ?.slice("version:".length) .trim() + if (found !== version) throw new Error(`Desktop metadata version mismatch: expected ${version}, got ${found}`) + const releaseDate = lines .find((line) => line.startsWith("releaseDate:")) ?.slice("releaseDate:".length) .trim() .replace(/^['"]|['"]$/g, "") + if (!releaseDate) throw new Error("Desktop metadata did not include a release date") const files: DesktopFile[] = [] lines.forEach((line) => { const value = line.trim() + if (value.startsWith("- url:")) { const name = value.slice("- url:".length).trim() const filename = name.startsWith("http") ? decodeURIComponent(new URL(name).pathname.split("/").pop()!) : name const url = uploaded[filename]?.url + if (!url) throw new Error(`Desktop update file was not uploaded: ${filename}`) files.push({ url, sha512: "", size: 0 }) + return } + const current = files.at(-1) - if (!current) return + + // electron-builder repeats the first file's sha512 as a top-level legacy field after the list. + if (!current || !line.startsWith(" ")) return + if (value.startsWith("sha512:")) current.sha512 = value.slice("sha512:".length).trim() + if (value.startsWith("size:")) current.size = Number(value.slice("size:".length).trim()) + if (value.startsWith("blockMapSize:")) current.blockMapSize = Number(value.slice("blockMapSize:".length).trim()) }) + if (!files.length || files.some((file) => !file.sha512 || !file.size)) throw new Error("Desktop metadata contained an incomplete file") + return { files, releaseDate } }