import { platform } from "os" ; import { app , ipcMain } from "electron" ; import { activeWindow } from "electron-util" ; import fetch from "node-fetch" ; import { lte } from "semver" ; import { CHECK_FOR_UPDATES , UPDATE_AVAILABLE } from "@/messages" ; import { isNil } from "@/utils" ; const timeBetweenUpdateChecks = 30 * 60 * 1000 ; let mainWindow = null ; let updateInterval = null ; export const initUpdateChecker = ( ) => { mainWindow = activeWindow ( ) ; ipcMain . on ( CHECK_FOR_UPDATES , ( ) => { initCheckForUpdates ( ) ; } ) ; } ; type GitHubRelease = { tag_name : string ; name : string ; url : string ; prerelease : boolean ; assets : Array < { name : string ; browser_download_url : string ; } > ; } ; const getExtension = ( ) => { switch ( platform ( ) ) { case "darwin" : return ".dmg" ; case "win32" : return ".exe" ; default : return null ; } } ; const defaultUrl = "https://github.com/beeequeue/yuna/releases" ; const getDownloadUrl = ( data ) => { const extension = getExtension ( ) ; if ( extension == null ) { return defaultUrl ; } const url = data . assets . find ( ( asset ) => asset . browser_download_url . endsWith ( extension ) ) ; return url ?. browser_download_url ?? defaultUrl ; } ; const checkForUpdates = async ( ) => { const response = await fetch ( "https://api.github.com/repos/beeequeue/yuna/releases/latest" ) ; const body = ( await response . json ( ) ) as GitHubRelease ; if ( ! response . ok && updateInterval != null ) { global . clearInterval ( updateInterval ) ; updateInterval = null ; return ; } const latestVersion = body . tag_name . match ( /(\d+\.\d+\.\d+)/ ) ?. [ 0 ] ; if ( isNil ( latestVersion ) || lte ( latestVersion , app . getVersion ( ) ) ) return ; sendMessage ( UPDATE_AVAILABLE , getDownloadUrl ( body ) ) ; } ; const initCheckForUpdates = ( ) => { checkForUpdates ( ) ; updateInterval = setInterval ( checkForUpdates , timeBetweenUpdateChecks ) ; } ; const sendMessage = ( message , arg ? ) => { if ( isNil ( mainWindow ) ) { setTimeout ( ( ) => sendMessage ( message , arg ) , 1000 ) ; return ; } mainWindow . webContents . send ( message , arg ) ; } ;