diff --git a/package-lock.json b/package-lock.json index 45cf8dc..ca819c4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "nice-node", - "version": "5.0.0-alpha", + "version": "5.1.2-alpha", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "nice-node", - "version": "5.0.0-alpha", + "version": "5.1.2-alpha", "license": "MIT", "dependencies": { "@reduxjs/toolkit": "^1.9.3", diff --git a/package.json b/package.json index ddf39f0..079f024 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nice-node", - "version": "5.1.2-alpha", + "version": "5.2.0-alpha", "description": "Run a node at home, the easy way.", "homepage": "https://nicenode.xyz", "productName": "NiceNode", diff --git a/src/main/basicUpdater.ts b/src/main/basicUpdater.ts new file mode 100644 index 0000000..71929d9 --- /dev/null +++ b/src/main/basicUpdater.ts @@ -0,0 +1,134 @@ +// import sleep from 'await-sleep'; +// import { type BrowserWindow, autoUpdater, dialog, FeedURLOptions } from 'electron'; +import type { BrowserWindow } from 'electron'; +// // const { updateElectronApp } = require('update-electron-app') +import { type IUpdateElectronAppOptions, updateElectronApp, UpdateSourceType } from 'update-electron-app'; +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', () => { +// logger.info('autoUpdater:::::::::update-downloaded'); +// 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; +}; + +// const feedUrl = `https://update.electronjs.org/NiceNode/test-nice-node-updater/darwin-arm64/5.1.2-alpha` +export const initialize = (mainWindow: BrowserWindow) => { + updateLogger.info('initialize updater'); + + const options: IUpdateElectronAppOptions = { + updateSource: { + type: UpdateSourceType.ElectronPublicUpdateService, + // repo: 'NiceNode/nice-node', + repo: 'NiceNode/test-nice-node-updater', + host: 'https://update.electronjs.org', + }, + updateInterval: '5 minutes', // testing + logger: updateLogger + } + + updateLogger.info('updater options: ', options); + updateElectronApp(options); +} diff --git a/src/main/main.ts b/src/main/main.ts index 3310a3b..bcd5991 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -84,7 +84,7 @@ const RESOURCES_PATH = app.isPackaged : path.join(__dirname, '..', '..', 'assets'); // starting point: .vite/build/main.js const getAssetPath = (...paths: string[]): string => { - logger.log('RESOURCES_PATH: ', RESOURCES_PATH); + logger.info('RESOURCES_PATH: ', RESOURCES_PATH); return path.join(RESOURCES_PATH, ...paths); }; @@ -158,7 +158,12 @@ export const createWindow = async () => { // App auto updates updater.initialize(mainWindow); - // updater.checkForUpdates(false); + // disabled in dev env + if (!isDevelopment) { + updater.checkForUpdates(false); + } else { + logger.info('updater.checkForUpdates() skipped. Disabled in development env'); + } menuBuilder = new MenuBuilder(mainWindow); menuBuilder.buildMenu(); diff --git a/src/main/updater.ts b/src/main/updater.ts index f2be115..60c1df0 100644 --- a/src/main/updater.ts +++ b/src/main/updater.ts @@ -1,92 +1,90 @@ -// import sleep from 'await-sleep'; +import sleep from 'await-sleep'; // import { type BrowserWindow, autoUpdater, dialog, FeedURLOptions } from 'electron'; -import type { BrowserWindow } from 'electron'; -// // const { updateElectronApp } = require('update-electron-app') -import { type IUpdateElectronAppOptions, updateElectronApp, UpdateSourceType } from 'update-electron-app'; +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 { reportEvent } from './events'; +import i18nMain from './i18nMain'; +// import logger, { autoUpdateLogger } from './logger'; // import logger from './logger'; -// import { getSetIsPreReleaseUpdatesEnabled } from './state/settings'; +import { getSetIsPreReleaseUpdatesEnabled } from './state/settings'; -// let notifyUserIfNoUpdateAvailable: boolean; +let notifyUserIfNoUpdateAvailable: boolean; -// const t = i18nMain.getFixedT(null, 'updater'); +const t = i18nMain.getFixedT(null, 'updater'); -// const intiUpdateHandlers = (browserWindow: BrowserWindow) => { -// autoUpdater.on('error', (error) => { -// logger.error('autoUpdater:::::::::error', error); -// }); +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('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-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', () => { -// logger.info('autoUpdater:::::::::update-downloaded'); -// 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'); -// } -// }); -// }; + 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; @@ -104,8 +102,8 @@ const updateLogger = log.scope('updater'); export const checkForUpdates = (notifyIfNoUpdateAvailable: boolean) => { logger.info(`updater.checkForUpdates set to: ${notifyIfNoUpdateAvailable}`); - // notifyUserIfNoUpdateAvailable = notifyIfNoUpdateAvailable; - // autoUpdater.checkForUpdates(); + notifyUserIfNoUpdateAvailable = notifyIfNoUpdateAvailable; + autoUpdater.checkForUpdates(); }; export const setAllowPrerelease = (isAllowPrerelease: boolean) => { @@ -116,17 +114,11 @@ export const setAllowPrerelease = (isAllowPrerelease: boolean) => { export const initialize = (mainWindow: BrowserWindow) => { updateLogger.info('initialize updater'); - - const options: IUpdateElectronAppOptions = { - updateSource: { - type: UpdateSourceType.ElectronPublicUpdateService, - repo: 'NiceNode/nice-node', - host: 'https://update.electronjs.org', - }, - updateInterval: '5 minutes', // testing - logger: updateLogger - } - - updateLogger.info('updater options: ', options); - updateElectronApp(options); + 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); }