"use strict"; const fs = require("node:fs/promises"); const path = require("node:path"); const { execFileSync } = require("node:child_process"); const { app, safeStorage } = require("electron"); const root = path.resolve(__dirname, ".."); const configuredUserData = process.env.FORGEFLOW_USER_DATA || path.join(app.getPath("appData"), "forgeflow"); app.setPath("userData", path.resolve(configuredUserData)); async function api(baseUrl, token, pathname, options = {}) { const response = await fetch(`${baseUrl}/api/v1${pathname}`, { ...options, headers: { Accept: "application/json", Authorization: `token ${token}`, ...(options.headers || {}), }, signal: AbortSignal.timeout(options.timeout || 180_000), }); const text = await response.text(); let data = null; try { data = text ? JSON.parse(text) : null; } catch { data = text; } if (!response.ok) throw new Error( `Gitea returned HTTP ${response.status}: ${data?.message || text || response.statusText}`, ); return data; } app.whenReady().then(async () => { try { const manifest = JSON.parse( await fs.readFile(path.join(root, "package.json"), "utf8"), ); const config = JSON.parse( await fs.readFile( path.join(configuredUserData, "forgeflow-config.json"), "utf8", ), ); const token = safeStorage.decryptString( Buffer.from(config.gitea.encryptedToken, "base64"), ); const baseUrl = String(config.gitea.baseUrl).replace(/\/+$/, ""); const version = manifest.version; const tag = `v${version}`; const commit = execFileSync("git", ["rev-parse", "HEAD"], { cwd: root, encoding: "utf8", }).trim(); const remote = execFileSync( "git", ["ls-remote", "origin", "refs/heads/main"], { cwd: root, encoding: "utf8" }, ) .trim() .split(/\s+/)[0]; if (commit !== remote) throw new Error("Local HEAD is not the published origin/main commit."); const notesPath = path.join(root, "docs", `RELEASE_NOTES_${version}.md`); const body = await fs.readFile(notesPath, "utf8"); let release; try { release = await api( baseUrl, token, `/repos/Jens/ForgeFlow/releases/tags/${encodeURIComponent(tag)}`, ); } catch (error) { if (!/HTTP 404/.test(error.message)) throw error; release = await api(baseUrl, token, "/repos/Jens/ForgeFlow/releases", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ tag_name: tag, target_commitish: commit, name: `ForgeFlow ${version}`, body, draft: false, prerelease: false, }), }); } const binaries = [ path.join(root, "dist", `ForgeFlow-Setup-${version}-win-x64.exe`), path.join(root, "dist", `ForgeFlow-Portable-${version}-win-x64.exe`), ]; for (const binaryPath of binaries) { const binaryName = path.basename(binaryPath); const binary = await fs.readFile(binaryPath); const checksumPath = `${binaryPath}.sha256`; const checksumName = `${binaryName}.sha256`; const checksum = await fs.readFile(checksumPath); for (const [name, bytes, type] of [ [binaryName, binary, "application/vnd.microsoft.portable-executable"], [checksumName, checksum, "text/plain"], ]) { const existing = (release.assets || []).find( (asset) => asset.name === name, ); if (existing && Number(existing.size) === bytes.length) { console.log(`SKIP ${name} already published`); continue; } if (existing) { await api( baseUrl, token, `/repos/Jens/ForgeFlow/releases/${release.id}/assets/${existing.id}`, { method: "DELETE" }, ); } const form = new FormData(); form.append("attachment", new Blob([bytes], { type }), name); const uploaded = await api( baseUrl, token, `/repos/Jens/ForgeFlow/releases/${release.id}/assets?name=${encodeURIComponent(name)}`, { method: "POST", body: form, timeout: 300_000, }, ); release.assets = [ ...(release.assets || []).filter((asset) => asset.name !== name), uploaded, ]; console.log(`PASS published ${name}`); } } console.log( `PASS ForgeFlow ${version} binary release published for ${commit.slice(0, 7)}`, ); app.exit(0); } catch (error) { console.error(`FAIL ${error.message}`); app.exit(1); } });