Skip to content

feat: support not to auto check update & manual check update #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 15 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ export interface IUpdateElectronAppOptions<L = ILogger> {
* Minimum allowed interval is `5 minutes`.
*/
readonly updateInterval?: string;
/**
* @param {Boolean} autoCheck Decides whether to automatically check for updates
* Defaults to `true`.
*/
Comment on lines +98 to +101
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* @param {Boolean} autoCheck Decides whether to automatically check for updates
* Defaults to `true`.
*/
/**
* @param {Boolean} autoCheck Decides whether to automatically check for updates
* @default true
*/

Copy link
Contributor Author

@rhy3h rhy3h May 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other comments

/**
* @param {Object} logger A custom logger object that defines a `log` function.
* Defaults to `console`. See electron-log, a module
* that aggregates logs from main and renderer processes into a single file.
*/
readonly logger?: L;
/**
* @param {Boolean} notifyUser Defaults to `true`. When enabled the user will be
* prompted to apply the update immediately after download.
*/

those writing style uses indentation
so I do that too
which one should I follow

readonly autoCheck?: boolean;
/**
* @param {Object} logger A custom logger object that defines a `log` function.
* Defaults to `console`. See electron-log, a module
Expand Down Expand Up @@ -152,7 +157,7 @@ export function updateElectronApp(opts: IUpdateElectronAppOptions = {}) {
}

function initUpdater(opts: ReturnType<typeof validateInput>) {
const { updateSource, updateInterval, logger } = opts;
const { updateSource, updateInterval, autoCheck, logger } = opts;

// exit early on unsupported platforms, e.g. `linux`
if (!supportedPlatforms.includes(process?.platform)) {
Expand Down Expand Up @@ -241,11 +246,13 @@ function initUpdater(opts: ReturnType<typeof validateInput>) {
);
}

// check for updates right away and keep checking later
autoUpdater.checkForUpdates();
setInterval(() => {
if (autoCheck) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (autoCheck) {
if (autoCheck === true) {

I wonder if we want to be stricter about actually passing true rather than something truthy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (opts.notifyUser) {

I follow this, so I didn't do strict equal
I agree with you, it might need stricter

// check for updates right away and keep checking later
autoUpdater.checkForUpdates();
}, ms(updateInterval));
setInterval(() => {
autoUpdater.checkForUpdates();
}, ms(updateInterval));
}
}

/**
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 };
}
29 changes: 29 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});