Skip to content
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

Periodic update check #8

Merged
merged 1 commit into from
Feb 24, 2025
Merged
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
4 changes: 3 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
hasRecognizedProjectStructureAtWorkspaceRoot,
isKeyUsed,
isWindowsNotWSL,
startPeriodicChecks,
Keys,
Logger,
} from './utils';
Expand Down Expand Up @@ -35,7 +36,8 @@ export function activate(context: vscode.ExtensionContext) {
.then(() => registerEditorCommands(context))
.then(() => registerStatusBarItems(context))
.then(() => registerDataProviders(context))
.then(autoStartLspClientIfRequested);
.then(autoStartLspClientIfRequested)
.then(startPeriodicChecks);
}

async function autoStartLspClientIfRequested() {
Expand Down
2 changes: 2 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ensureAderynIsInstalled } from './install';
import { isKeyUsed, Keys } from './keys';
import { Logger } from './logger';
import { readAderynConfigTemplate } from './metadata';
import { startPeriodicChecks } from './update';

export {
findProjectRoot,
Expand All @@ -21,6 +22,7 @@ export {
executeCommand,
ensureAderynIsInstalled,
isKeyUsed,
startPeriodicChecks,
Keys,
Logger,
};
66 changes: 66 additions & 0 deletions src/utils/update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Run periodic checks to see if there's an update to aderyn available. If yes, invite the user to visit
// the welcome page. [Update] or [Remind Me Later]

import * as vscode from 'vscode';
import { Logger } from './logger';
import { isAderynAvailableOnPath } from './install/aderyn';
import {
latestAderynVersionOnGithub,
areAderynVersionsEqual,
getLocalAderynVersion,
} from './install/versions';
import { EditorCmd } from '../commands/variants';

async function updateIsAvailable(logger: Logger): Promise<boolean> {
const isOnPath = await isAderynAvailableOnPath(logger);
if (!isOnPath) {
return false;
}
try {
const latestAderynVersion = await latestAderynVersionOnGithub(logger);
const existingAderynVersion = await getLocalAderynVersion(logger);
return !areAderynVersionsEqual(latestAderynVersion, existingAderynVersion);
} catch (_) {
return false;
}
}

const enum UserAction {
YES = 'Yes',
LATER = 'Remind me later',
NO = 'Undefined', // When the user closes the notification
}

async function promptUserToUpdate(): Promise<UserAction> {
return vscode.window
.showInformationMessage(
"Aderyn's core tool has an update used by the extension. Update now?",
UserAction.YES,
UserAction.LATER,
)
.then((selectedAction) => {
if (!selectedAction) {
return UserAction.NO;
}
if (selectedAction === UserAction.YES) {
vscode.commands.executeCommand(EditorCmd.ShowOnboardPanel);
}
return selectedAction;
});
}

async function startPeriodicChecks() {
const action = async () => {
const can = await updateIsAvailable(new Logger());
if (can) {
const action = await promptUserToUpdate();
if (action == UserAction.YES || action == UserAction.NO) {
clearInterval(intervalId);
}
}
};
const TIME_PERIOD = 30 * 60 * 1000; // 30 minute
const intervalId = setInterval(action, TIME_PERIOD);
}

export { startPeriodicChecks };