11import { autoUpdater , BrowserWindow } from 'electron' ;
22import log from 'electron-log/main' ;
33import packageJson from '../../package.json' ;
4+ import { mainStorage } from './storageService' ;
45
56export class AutoUpdateManager {
7+ public static versionDownloadedKey = 'versionDownloaded' ;
8+
69 private readonly channel = 'stable' ;
710
811 private newAvailableVersion : string | undefined = undefined ;
@@ -12,19 +15,37 @@ export class AutoUpdateManager {
1215 // private feedBaseUrl = 'https://update.electronjs.org';
1316 private feedBaseUrl = 'https://tonkeeper-web-updater-test.nkuznetsov.workers.dev' ;
1417
18+ public static async quitAndInstallIfFlagged ( ) {
19+ const flagged = await mainStorage . get ( AutoUpdateManager . versionDownloadedKey ) ;
20+ if ( flagged ) {
21+ return AutoUpdateManager . quitAndInstall ( ) ;
22+ }
23+ }
24+
25+ public static async quitAndInstall ( ) {
26+ await mainStorage . delete ( AutoUpdateManager . versionDownloadedKey ) ;
27+ setImmediate ( ( ) => autoUpdater . quitAndInstall ( ) ) ;
28+ return true ;
29+ }
30+
1531 constructor ( win : BrowserWindow ) {
1632 this . win = win ;
1733
1834 this . init ( ) ;
1935 }
2036
21- private init ( ) {
37+ private async init ( ) {
2238 const feedURL = `${ this . feedBaseUrl } /${ this . getRepoUrl ( ) } /${ process . platform } /${
2339 packageJson . version
2440 } /${ this . channel } `;
2541 autoUpdater . setFeedURL ( { url : feedURL } ) ;
2642 this . listenDownload ( ) ;
2743
44+ const exited = await AutoUpdateManager . quitAndInstallIfFlagged ( ) ;
45+ if ( exited ) {
46+ return ;
47+ }
48+
2849 autoUpdater . checkForUpdates ( ) ;
2950 setInterval ( ( ) => {
3051 autoUpdater . checkForUpdates ( ) ;
@@ -39,10 +60,6 @@ export class AutoUpdateManager {
3960 . trim ( ) ;
4061 }
4162
42- public quitAndInstall ( ) {
43- setImmediate ( ( ) => autoUpdater . quitAndInstall ( ) ) ;
44- }
45-
4663 public getNewVersionAvailable ( ) {
4764 return this . newAvailableVersion ;
4865 }
@@ -52,6 +69,7 @@ export class AutoUpdateManager {
5269 const version = process . platform === 'win32' ? releaseNotes : releaseName ;
5370 this . newAvailableVersion = version ;
5471 this . win . webContents . send ( 'app-update::ready' , { version } ) ;
72+ mainStorage . set ( AutoUpdateManager . versionDownloadedKey , version ) ;
5573 log . log ( '[AutoUpdate] updater new version fetched:' , version ) ;
5674 } ) ;
5775
0 commit comments