import { readFile } from 'node:fs/promises' import { join } from 'node:path' import plist from 'plist' import { z } from 'zod' import { $, sleep } from 'zx' import { ffetch } from '../../utils/fetch.ts' const latestVerInfo = await ffetch('https://api.github.com/repos/forkgram/tdesktop/releases/latest').parsedJson( z.object({ tag_name: z.string().transform(v => v.replace(/^v/, '')), assets: z.array(z.object({ name: z.string(), browser_download_url: z.string(), })), }), ) const INSTALL_PATH = '/Applications/Forkgram.app' console.log('latest version:', latestVerInfo.tag_name) const installedPlist = await readFile(join(INSTALL_PATH, 'Contents/Info.plist'), 'utf8') const installedPlistParsed = z.object({ CFBundleShortVersionString: z.string(), }).parse(plist.parse(installedPlist)) console.log('installed version:', installedPlistParsed.CFBundleShortVersionString) if (installedPlistParsed.CFBundleShortVersionString === latestVerInfo.tag_name) { console.log('✅ no update needed') process.exit(0) } const arm64Asset = latestVerInfo.assets.find(asset => asset.name === 'Forkgram.macOS.no.auto-update_arm64.zip') if (!arm64Asset) { console.error('❌ no arm64 asset found') process.exit(1) } console.log('installing new version...') await $`curl -L ${arm64Asset.browser_download_url} -o /tmp/forkgram.zip` await $`unzip -o /tmp/forkgram.zip -d /tmp/forkgram` const pid = await $`/usr/bin/pgrep -f /Applications/Forkgram.app/Contents/MacOS/Telegram`.text().catch(() => null) if (pid) { await $`kill -9 ${pid.trim()}` } await $`rm -rf ${INSTALL_PATH}` await $`mv /tmp/forkgram/Telegram.app ${INSTALL_PATH}` await $`rm -rf /tmp/forkgram` await $`xattr -cr ${INSTALL_PATH}` await sleep(1000) await $`open ${INSTALL_PATH}` console.log('✅ done')