@@ -295,7 +295,7 @@ function checkForExistingDownload(): { exists: boolean; version?: string } {
295295 const fileName = ( info ?. fileName || info ?. path ) as string | undefined
296296 if ( fileName && fs . existsSync ( path . join ( cacheDir , fileName ) ) ) {
297297 mainLog . info ( `[auto-update] Found existing download via update-info.json: ${ fileName } ` )
298- return { exists : true , version : info ?. version as string }
298+ return { exists : true , version : ( info ?. version as string | undefined ) ?? inferVersionFromUpdateFileName ( fileName ) }
299299 }
300300 }
301301
@@ -309,7 +309,7 @@ function checkForExistingDownload(): { exists: boolean; version?: string } {
309309 )
310310 if ( downloadFile ) {
311311 mainLog . info ( `[auto-update] Found existing download file: ${ downloadFile } ` )
312- return { exists : true }
312+ return { exists : true , version : inferVersionFromUpdateFileName ( downloadFile ) }
313313 }
314314
315315 mainLog . info ( `[auto-update] No existing download found in cache` )
@@ -320,6 +320,41 @@ function checkForExistingDownload(): { exists: boolean; version?: string } {
320320 }
321321}
322322
323+ function inferVersionFromUpdateFileName ( fileName : string ) : string | undefined {
324+ const match = fileName . match ( / (?: ^ | [ - _ ] ) ( \d + \. \d + \. \d + ) (?: [ - _ . ] | $ ) / )
325+ return match ?. [ 1 ]
326+ }
327+
328+ async function refreshReadyUpdateState ( ) : Promise < boolean > {
329+ const internalState = checkElectronUpdaterState ( )
330+ if ( internalState . ready ) {
331+ updateInfo = {
332+ ...updateInfo ,
333+ available : true ,
334+ latestVersion : internalState . version ?? updateInfo . latestVersion ,
335+ downloadState : 'ready' ,
336+ downloadProgress : 100 ,
337+ }
338+ broadcastUpdateInfo ( )
339+ return true
340+ }
341+
342+ const existing = checkForExistingDownload ( )
343+ if ( existing . exists ) {
344+ updateInfo = {
345+ ...updateInfo ,
346+ available : true ,
347+ latestVersion : existing . version ?? updateInfo . latestVersion ,
348+ downloadState : 'ready' ,
349+ downloadProgress : 100 ,
350+ }
351+ broadcastUpdateInfo ( )
352+ return true
353+ }
354+
355+ return false
356+ }
357+
323358/**
324359 * Check for available updates.
325360 * Returns the current UpdateInfo state after check completes.
@@ -382,6 +417,16 @@ export async function checkForUpdates(options: CheckOptions = {}): Promise<Updat
382417 * Then relaunches the app automatically.
383418 */
384419export async function installUpdate ( ) : Promise < void > {
420+ if ( updateInfo . downloadState !== 'ready' ) {
421+ mainLog . warn ( `[auto-update] installUpdate called while state is ${ updateInfo . downloadState } ; refreshing update state` )
422+
423+ if ( ! await refreshReadyUpdateState ( ) ) {
424+ await checkForUpdates ( { autoDownload : true } )
425+ await new Promise ( resolve => setTimeout ( resolve , 500 ) )
426+ await refreshReadyUpdateState ( )
427+ }
428+ }
429+
385430 if ( updateInfo . downloadState !== 'ready' ) {
386431 throw new Error ( 'No update ready to install' )
387432 }
0 commit comments