-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdater.ts
More file actions
124 lines (113 loc) · 4.6 KB
/
updater.ts
File metadata and controls
124 lines (113 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import sleep from 'await-sleep';
// import { type BrowserWindow, autoUpdater, dialog, FeedURLOptions } from 'electron';
import { app, autoUpdater, dialog, type BrowserWindow } from 'electron';
import logger, { autoUpdateLogger } from './logger';
import log from 'electron-log/main';
const updateLogger = log.scope('updater');
import { reportEvent } from './events';
import i18nMain from './i18nMain';
// import logger, { autoUpdateLogger } from './logger';
// import logger from './logger';
import { getSetIsPreReleaseUpdatesEnabled } from './state/settings';
let notifyUserIfNoUpdateAvailable: boolean;
const t = i18nMain.getFixedT(null, 'updater');
const intiUpdateHandlers = (browserWindow: BrowserWindow) => {
autoUpdater.on('error', (error) => {
logger.error('autoUpdater:::::::::error', error);
});
autoUpdater.on('checking-for-update', () => {
logger.info('autoUpdater:::::::::checking-for-update');
});
autoUpdater.on('update-available', async (info: any) => {
logger.info('autoUpdater:::::::::update-available: ', info);
// Quick fix to wait for window load before showing update prompt
await sleep(5000);
dialog
.showMessageBox(browserWindow, {
type: 'info',
title: t('UpdateAvailable'),
message: `${t('UpdateNiceNode')} ${info.version}.`,
buttons: [t('Yes'), t('No')],
})
.then(async (buttonIndex) => {
if (buttonIndex.response === 0) {
console.log('update accepted by user');
console.log('starting download');
autoUpdater.quitAndInstall();
dialog.showMessageBox(browserWindow, {
type: 'info',
title: t('UpdateAvailable'),
message: t('DownloadingUpdate'),
});
} else {
console.log('update checkbox not checked');
}
})
.catch((err) => {
console.error('error in update available dialog: ', err);
});
});
autoUpdater.on('update-not-available', () => {
logger.info('autoUpdater:::::::::update-not-available');
if (notifyUserIfNoUpdateAvailable) {
dialog.showMessageBox(browserWindow, {
type: 'info',
title: t('NoUpdateAvailable'),
message: t('NoUpdateAvailable'),
});
notifyUserIfNoUpdateAvailable = false;
}
});
autoUpdater.on('update-downloaded', (...args) => {
logger.info('autoUpdater:::::::::update-downloaded args: ', args);
logger.info('Calling autoUpdater.quitAndInstall()');
reportEvent('UpdatedNiceNode');
try {
autoUpdater.quitAndInstall();
} catch (err) {
logger.error('Error in: autoUpdater.quitAndInstall()');
logger.error(err);
dialog.showErrorBox(
t('ErrorUpdating'),
t('UnableToInstallUpdate', {
downloadLink: 'https://www.nicenode.xyz/#download',
}),
);
// todo: send error details
reportEvent('ErrorUpdatingNiceNode');
}
});
};
// export const initialize = (mainWindow: BrowserWindow) => {
// // autoUpdater.logger = autoUpdateLogger;
// // autoUpdater.autoDownload = false;
// // autoUpdater.autoInstallOnAppQuit = false;
// const isPreReleaseUpdatesEnabled = getSetIsPreReleaseUpdatesEnabled();
// logger.info(`isPreReleaseUpdatesEnabled: ${isPreReleaseUpdatesEnabled}`);
// // const server = 'https://github.com/NiceNode/nice-node/releases/latest'
// // const url = `${server}/update/${process.platform}/${app.getVersion()}`
// // autoUpdater.setFeedURL({ url });
// // autoUpdater.allowPrerelease = isPreReleaseUpdatesEnabled;
// notifyUserIfNoUpdateAvailable = false;
// intiUpdateHandlers(mainWindow);
// };
export const checkForUpdates = (notifyIfNoUpdateAvailable: boolean) => {
logger.info(`updater.checkForUpdates set to: ${notifyIfNoUpdateAvailable}`);
notifyUserIfNoUpdateAvailable = notifyIfNoUpdateAvailable;
autoUpdater.checkForUpdates();
};
export const setAllowPrerelease = (isAllowPrerelease: boolean) => {
logger.info(`updater.allowPrerelease set to: ${isAllowPrerelease}`);
// pre-release: not available https://www.electronjs.org/docs/latest/api/auto-updater#event-update-available
// autoUpdater.allowPrerelease = isAllowPrerelease;
};
export const initialize = (mainWindow: BrowserWindow) => {
updateLogger.info('initialize updater');
const host = 'https://update.electronjs.org';
const publicRepo = 'NiceNode/test-nice-node-updater';
const currentAppVersion = app.getVersion(); // ex. 5.1.2-alpha
const feedUrl = `${host}/${publicRepo}/${process.platform}-${process.arch}/${currentAppVersion}`
autoUpdater.setFeedURL({ url: feedUrl });
notifyUserIfNoUpdateAvailable = false;
intiUpdateHandlers(mainWindow);
}