diff --git a/README.md b/README.md index b141410..ae447ff 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. diff --git a/src/index.ts b/src/index.ts index c378898..6525c0a 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,13 @@ 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(); - }, ms(updateInterval)); + setInterval(() => { + autoUpdater.checkForUpdates(); + }, ms(updateInterval)); + } } /** @@ -296,11 +303,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 +356,5 @@ function validateInput(opts: IUpdateElectronAppOptions) { assert(logger && typeof logger.log, 'function'); - return { updateSource, updateInterval, logger, notifyUser, onNotifyUser }; + return { updateSource, updateInterval, autoCheck, logger, notifyUser, onNotifyUser }; } diff --git a/test/index.test.ts b/test/index.test.ts index f45de9b..c7f1838 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -132,3 +132,32 @@ describe('makeUserNotifier', () => { ); }); }); + +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') + .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(); + }); +});