// Shared by the release scripts: the Forgejo API (FORGEJO_TOKEN from the environment or // the ignored .env, never printed), release notes and the bundle folder. import fs from 'node:fs'; import path from 'node:path'; export const API = 'https://git.elijahkuntz.com/api/v1/repos/Elijah/Kaleido-Desktop'; export const root = path.resolve(import.meta.dirname, '..'); export const bundleDir = path.join(root, 'src-tauri/target/release/bundle'); /** FORGEJO_TOKEN; reads of a public repository work without it, uploads don't. */ export function token(required = false) { let t = process.env.FORGEJO_TOKEN; if (!t && fs.existsSync(path.join(root, '.env'))) { t = fs.readFileSync(path.join(root, '.env'), 'utf8').match(/^FORGEJO_TOKEN=(.*)$/m)?.[1]?.trim().replace(/^"|"$/g, ''); } if (!t && required) { console.error('FORGEJO_TOKEN is not set.'); process.exit(1); } return t; } const auth = () => (token() ? { Authorization: `token ${token()}` } : {}); export async function api(method, url, body) { const headers = { ...auth(), Accept: 'application/json' }; const r = await fetch(url.startsWith('http') ? url : API + url, { method, headers: body instanceof FormData ? headers : { ...headers, 'Content-Type': 'application/json' }, body: body === undefined ? undefined : body instanceof FormData ? body : JSON.stringify(body), }); const text = await r.text(); if (!r.ok) throw new Error(`${method} ${url} → ${r.status} ${text.slice(0, 300)}`); return text ? JSON.parse(text) : null; } /** Downloads a release asset's text (an updater signature). */ export async function download(url) { const r = await fetch(url, { headers: auth() }); if (!r.ok) throw new Error(`GET ${url} → ${r.status}`); return r.text(); } /** The version's section of CHANGELOG.md. */ export function releaseNotes(version) { const changelog = fs.readFileSync(path.join(root, 'CHANGELOG.md'), 'utf8'); const section = changelog.split(/^## /m).find((s) => s.startsWith(version)); return section ? section.slice(version.length).trim() : ''; }