Skip to content
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 .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
schedule:
- cron: '0 22 * * 3'
workflow_call:
workflow_dispatch:
Copy link
Member

Choose a reason for hiding this comment

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

so anyone that forks the repo can run the tests locally in the fork

Could you just have this in your fork and avoid upstreaming it? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I needed upstream this to run the tests easier locally here. This grants me the power to run anytime the tests, I only use Linux and my hardware can't run VMs. So the only way I can test it in other platforms is using Github Actions. If you want I can remove it, but only when the other needed changes were done


permissions:
contents: read
Expand Down
58 changes: 47 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ export interface IUpdateElectronAppOptions<L = ILogger> {
readonly onNotifyUser?: (info: IUpdateInfo) => void;
}

export interface IUpdater {
readonly isSupported: boolean;
readonly isLookingForUpdates: boolean;
readonly stopLookingForUpdates: () => void;
readonly startLookingForUpdates: () => void;
}

// eslint-disable-next-line @typescript-eslint/no-require-imports
const pkg = require('../package.json');
const userAgent = format('%s/%s (%s: %s)', pkg.name, pkg.version, os.platform(), os.arch());
Expand All @@ -128,7 +135,7 @@ const isHttpsUrl = (maybeURL: string) => {
}
};

export function updateElectronApp(opts: IUpdateElectronAppOptions = {}) {
export function updateElectronApp(opts: IUpdateElectronAppOptions = {}): IUpdater {
// check for bad input early, so it will be logged during development
const safeOpts = validateInput(opts);

Expand All @@ -141,25 +148,36 @@ export function updateElectronApp(opts: IUpdateElectronAppOptions = {}) {
} else {
console.log(message);
}
return;

//Return Updater but does not start
return makeUpdater(safeOpts);
}

const updater = makeUpdater(safeOpts);

if (app.isReady()) {
initUpdater(safeOpts);
updater.startLookingForUpdates();
} else {
app.on('ready', () => initUpdater(safeOpts));
app.on('ready', () => updater.startLookingForUpdates());
}

return updater;
}

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

// exit early on unsupported platforms, e.g. `linux`
if (!supportedPlatforms.includes(process?.platform)) {
log(
`Electron's autoUpdater does not support the '${process.platform}' platform. Ref: https://www.electronjs.org/docs/latest/api/auto-updater#platform-notices`,
);
return;
return {
isSupported: false,
isLookingForUpdates: false,
stopLookingForUpdates: () => {},
startLookingForUpdates: () => {},
};
}

let feedURL: string;
Expand Down Expand Up @@ -241,11 +259,29 @@ function initUpdater(opts: ReturnType<typeof validateInput>) {
);
}

// check for updates right away and keep checking later
autoUpdater.checkForUpdates();
setInterval(() => {
autoUpdater.checkForUpdates();
}, ms(updateInterval));
let intervalID: ReturnType<typeof setInterval>;
let isLookingForUpdates = false;

return {
isSupported: true,
get isLookingForUpdates() {
return isLookingForUpdates;
},
stopLookingForUpdates() {
if (isLookingForUpdates) {
clearInterval(intervalID);
isLookingForUpdates = false;
}
},
startLookingForUpdates() {
if (!isLookingForUpdates) {
intervalID = setInterval(() => {
autoUpdater.checkForUpdates();
}, ms(updateInterval));
isLookingForUpdates = true;
}
},
};
}

/**
Expand Down