import { app , autoUpdater } from 'electron' ; import package_ from '../../../package.json' ; import { logger } from '../../shared/utils/logger' ; import { availableUpdate , downloadedUpdate , downloadingUpdate , } from '../state/actions' ; import { dispatch } from '../state/store' ; import { pickerWindow , prefsWindow } from '../windows' ; import { getUpdateUrl } from './get-update-url' ; import { isUpdateAvailable } from './is-update-available' ; /**
 * Auto update check on production
 */ export async function initUpdateChecker ( ) { if ( app . isPackaged ) { autoUpdater . setFeedURL ( { url : getUpdateUrl ( ) , headers : { 'User-Agent' : `${ package_ . name }/${ package_ . version } (darwin: ${ process . arch })` } } ) ; autoUpdater . on ( 'before-quit-for-update' , ( ) => { pickerWindow ?. destroy ( ) ; prefsWindow ?. destroy ( ) ; } ) ; autoUpdater . on ( 'update-available' , ( ) => { dispatch ( downloadingUpdate ( ) ) ; } ) ; autoUpdater . on ( 'update-downloaded' , ( ) => { dispatch ( downloadedUpdate ( ) ) ; } ) ; autoUpdater . on ( 'error' , ( ) => { logger ( 'AutoUpdater' , 'An error has occurred' ) ; } ) ; if ( await isUpdateAvailable ( ) ) { dispatch ( availableUpdate ( ) ) ; } const ONE_DAY_MS = 86400000 ; setInterval ( async ( ) => { if ( await isUpdateAvailable ( ) ) { dispatch ( availableUpdate ( ) ) ; } } , ONE_DAY_MS ) ; } }