From 443ba1d33ac56d17c4b6930e4e2e2841b2f119ee Mon Sep 17 00:00:00 2001 From: rhy3h Date: Tue, 25 Feb 2025 14:00:34 +0800 Subject: [PATCH 1/7] feat: support not to auto check update & manual check update --- src/index.ts | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index c378898..c2ef158 100644 --- a/src/index.ts +++ b/src/index.ts @@ -95,6 +95,11 @@ export interface IUpdateElectronAppOptions { * Minimum allowed interval is `5 minutes`. */ readonly updateInterval?: string; + /** + * @param {Boolean} autoCheck Decides whether to automatically check for updates + * Defaults to `true`. + */ + readonly autoCheck?: boolean; /** * @param {Object} logger A custom logger object that defines a `log` function. * Defaults to `console`. See electron-log, a module @@ -152,7 +157,7 @@ export function updateElectronApp(opts: IUpdateElectronAppOptions = {}) { } function initUpdater(opts: ReturnType) { - const { updateSource, updateInterval, logger } = opts; + const { updateSource, updateInterval, autoCheck, logger } = opts; // exit early on unsupported platforms, e.g. `linux` if (!supportedPlatforms.includes(process?.platform)) { @@ -241,11 +246,27 @@ function initUpdater(opts: ReturnType) { ); } - // check for updates right away and keep checking later - autoUpdater.checkForUpdates(); - setInterval(() => { + if (autoCheck) { + // check for updates right away and keep checking later + autoUpdater.checkForUpdates(); + setInterval(() => { + autoUpdater.checkForUpdates(); + }, ms(updateInterval)); + } +} + +export function checkForUpdates() { + return new Promise((resolve) => { autoUpdater.checkForUpdates(); - }, ms(updateInterval)); + + autoUpdater.once('update-available', () => { + resolve(true); + }); + + autoUpdater.once('update-not-available', () => { + resolve(false); + }); + }); } /** @@ -296,11 +317,12 @@ function validateInput(opts: IUpdateElectronAppOptions) { const defaults = { host: 'https://update.electronjs.org', updateInterval: '10 minutes', + autoCheck: true, logger: console, notifyUser: true, }; - const { host, updateInterval, logger, notifyUser, onNotifyUser } = Object.assign( + const { host, updateInterval, autoCheck, logger, notifyUser, onNotifyUser } = Object.assign( {}, defaults, opts, @@ -348,5 +370,5 @@ function validateInput(opts: IUpdateElectronAppOptions) { assert(logger && typeof logger.log, 'function'); - return { updateSource, updateInterval, logger, notifyUser, onNotifyUser }; + return { updateSource, updateInterval, autoCheck, logger, notifyUser, onNotifyUser }; } From 6248aa1162e9c80b15a5b28b2808263ad91bdf81 Mon Sep 17 00:00:00 2001 From: rhy3h Date: Tue, 25 Feb 2025 14:01:20 +0800 Subject: [PATCH 2/7] test: make sure checkForUpdates() have been called --- test/index.test.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test/index.test.ts b/test/index.test.ts index f45de9b..2405ac1 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -132,3 +132,29 @@ describe('makeUserNotifier', () => { ); }); }); + +describe('autoCheck', () => { + it('make sure checkForUpdates is called', () => { + const checkForUpdatesSpy = jest + .spyOn(autoUpdater, 'checkForUpdates') + .mockImplementation(() => Promise.resolve()); + + updateElectronApp({ repo: 'foo/bar' }); + + expect(checkForUpdatesSpy).toHaveBeenCalled(); + + checkForUpdatesSpy.mockRestore(); + }); + + it('make sure checkForUpdates is not called', () => { + const checkForUpdatesSpy = jest + .spyOn(autoUpdater, 'checkForUpdates') + .mockImplementation(() => Promise.resolve()); + + updateElectronApp({ repo: 'foo/bar', autoCheck: false }); + + expect(checkForUpdatesSpy).not.toHaveBeenCalled(); + + checkForUpdatesSpy.mockRestore(); + }); +}); From 90977b09989ade1d3e1fe16694b0122317485d45 Mon Sep 17 00:00:00 2001 From: rhy3h Date: Tue, 25 Feb 2025 14:05:08 +0800 Subject: [PATCH 3/7] docs: update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 12989cc..f477064 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,7 @@ Once you've called `updateElectronApp` as documented above, that's it! Here's wh Additional Options: - `updateInterval` String (optional) - How frequently to check for updates. Defaults to `10 minutes`. Minimum allowed interval is `5 minutes`. This is a human readable interval supported by the [`ms`](https://github.com/vercel/ms#readme) module +- `autoCheck` Boolean (optional) - Decides whether to automatically check for updates. Defaults to `true`. - `logger` Object (optional) - A custom logger object that defines a `log` function. Defaults to `console`. See [electron-log](https://github.com/megahertz/electron-log), a module that aggregates logs from main and renderer processes into a single file. - `notifyUser` Boolean (optional) - Defaults to `true`. When enabled the user will be prompted to apply the update immediately after download. From 0bf8f7c2dc151ebb68e62d38e4d95b33d3706be0 Mon Sep 17 00:00:00 2001 From: rhy3h Date: Tue, 13 May 2025 16:08:41 +0800 Subject: [PATCH 4/7] test: linux do not support auto update, skip it --- test/index.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/index.test.ts b/test/index.test.ts index 2405ac1..940ade3 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -133,7 +133,7 @@ describe('makeUserNotifier', () => { }); }); -describe('autoCheck', () => { +(process.platform === 'linux' ? describe.skip : describe)('autoCheck', () => { it('make sure checkForUpdates is called', () => { const checkForUpdatesSpy = jest .spyOn(autoUpdater, 'checkForUpdates') From 701979399a780de42665774432cd19145c781ad4 Mon Sep 17 00:00:00 2001 From: rhy3h Date: Wed, 14 May 2025 09:47:15 +0800 Subject: [PATCH 5/7] feat: add comment for checkForUpdates() and add return type --- src/index.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index c2ef158..db92c03 100644 --- a/src/index.ts +++ b/src/index.ts @@ -255,7 +255,12 @@ function initUpdater(opts: ReturnType) { } } -export function checkForUpdates() { +/** + * Checks for available updates for the application. + * - `true` if an update is available + * - `false` if no update is available + */ +export function checkForUpdates(): Promise { return new Promise((resolve) => { autoUpdater.checkForUpdates(); From 29231e28c260e63b4935b63629df4805d4f11576 Mon Sep 17 00:00:00 2001 From: rhy3h Date: Wed, 14 May 2025 10:04:40 +0800 Subject: [PATCH 6/7] test: check if current platform is supported --- test/index.test.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/index.test.ts b/test/index.test.ts index 940ade3..c7f1838 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -133,7 +133,10 @@ describe('makeUserNotifier', () => { }); }); -(process.platform === 'linux' ? describe.skip : describe)('autoCheck', () => { +const supportedPlatforms = ['darwin', 'win32']; +const isSupportedPlatform = supportedPlatforms.includes(process?.platform); + +(!isSupportedPlatform ? describe.skip : describe)('autoCheck', () => { it('make sure checkForUpdates is called', () => { const checkForUpdatesSpy = jest .spyOn(autoUpdater, 'checkForUpdates') From 83bd1ec500b5ceef7eaf6ec06823787773f2842c Mon Sep 17 00:00:00 2001 From: rhy3h Date: Wed, 21 May 2025 17:42:09 +0800 Subject: [PATCH 7/7] feat: remove checkForUpdates() --- src/index.ts | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/src/index.ts b/src/index.ts index db92c03..6525c0a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -255,25 +255,6 @@ function initUpdater(opts: ReturnType) { } } -/** - * Checks for available updates for the application. - * - `true` if an update is available - * - `false` if no update is available - */ -export function checkForUpdates(): Promise { - return new Promise((resolve) => { - autoUpdater.checkForUpdates(); - - autoUpdater.once('update-available', () => { - resolve(true); - }); - - autoUpdater.once('update-not-available', () => { - resolve(false); - }); - }); -} - /** * Helper function that generates a callback for use with {@link IUpdateElectronAppOptions.onNotifyUser}. *