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

fix: handle versions list fetch failures #65

Merged
merged 1 commit into from
Jan 3, 2024
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
39 changes: 26 additions & 13 deletions src/downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { SemVer, parse, rcompare } from "semver";
import { fetch } from "undici";
import {
ExtensionContext,
OutputChannel,
ProgressLocation,
Uri,
commands,
Expand All @@ -13,6 +14,7 @@ import { Commands } from "./commands";

export const selectAndDownload = async (
context: ExtensionContext,
outputChannel?: OutputChannel,
): Promise<string | undefined> => {
const versions = await window.withProgress(
{
Expand All @@ -21,10 +23,14 @@ export const selectAndDownload = async (
cancellable: false,
},
async () => {
return await getVersions(context);
return await getVersions(context, outputChannel);
},
);

if (!versions) {
return;
}

const version = await askVersion(versions);

if (!version) {
Expand All @@ -46,15 +52,21 @@ export const selectAndDownload = async (
);
};

export const updateToLatest = async (context: ExtensionContext) => {
export const updateToLatest = async (
context: ExtensionContext,
outputChannel?: OutputChannel,
) => {
await window.withProgress(
{
location: ProgressLocation.Notification,
title: "Updating Biome version",
cancellable: false,
},
async () => {
const versions = await getVersions(context);
const versions = await getVersions(context, outputChannel);
if (!versions) {
return;
}
const version = versions[0];
await commands.executeCommand(Commands.StopServer);
await download(version, context);
Expand Down Expand Up @@ -151,6 +163,7 @@ const askVersion = async (versions: string[]): Promise<string | undefined> => {
*/
export const getVersions = async (
context: ExtensionContext,
outputChannel?: OutputChannel,
): Promise<string[] | undefined> => {
const cachedVersions = context.globalState.get<{
expires_at: Date;
Expand All @@ -163,20 +176,20 @@ export const getVersions = async (
}

let releases = undefined;
try {
releases = (await (
await fetch(
"https://api.github.com/repos/biomejs/biome/releases?per_page=100",
)
).json()) as { tag_name: string }[];
} catch (e) {
releases = undefined;
}

if (!releases) {
const response = await fetch(
"https://api.github.com/repos/biomejs/biome/releases?per_page=100",
);

if (!response.ok) {
outputChannel?.appendLine(
`Failed to fetch Biome versions from GitHub API: ${response.statusText}`,
);
return undefined;
}

releases = (await response.json()) as { tag_name: string }[];

type Release = {
date: Date;
version: SemVer;
Expand Down
10 changes: 5 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export async function activate(context: ExtensionContext) {
);

if (action === "Download Biome") {
if (!(await selectAndDownload(context))) {
if (!(await selectAndDownload(context, outputChannel))) {
return;
}
}
Expand All @@ -115,7 +115,7 @@ export async function activate(context: ExtensionContext) {
}
}

const statusBar = new StatusBar(context);
const statusBar = new StatusBar(context, outputChannel);
await statusBar.setUsingBundledBiome(server.bundled);

const documentSelector: DocumentFilter[] = [
Expand Down Expand Up @@ -228,14 +228,14 @@ export async function activate(context: ExtensionContext) {
);

if (result === "Update") {
await updateToLatest(context);
statusBar.checkForUpdates();
await updateToLatest(context, outputChannel);
statusBar.checkForUpdates(outputChannel);
}
});

commands.registerCommand(Commands.ChangeVersion, async () => {
await selectAndDownload(context);
statusBar.checkForUpdates();
statusBar.checkForUpdates(outputChannel);
});

session.registerCommand(Commands.SyntaxTree, syntaxTree(session));
Expand Down
14 changes: 9 additions & 5 deletions src/statusBar.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { gt } from "semver";
import {
ExtensionContext,
OutputChannel,
StatusBarAlignment,
StatusBarItem,
ThemeColor,
Expand Down Expand Up @@ -37,7 +38,10 @@ export class StatusBar {
private isActive = false;
private serverVersion = "";

constructor(private readonly context: ExtensionContext) {
constructor(
private readonly context: ExtensionContext,
private readonly outputChannel?: OutputChannel,
) {
this.statusBarItem = window.createStatusBarItem(
"biome.status",
StatusBarAlignment.Right,
Expand All @@ -54,7 +58,7 @@ export class StatusBar {
);

this.update();
this.checkForUpdates();
this.checkForUpdates(outputChannel);
}

public setServerState(client: LanguageClient, state: State) {
Expand Down Expand Up @@ -164,17 +168,17 @@ export class StatusBar {

public async setUsingBundledBiome(usingBundledBiome: boolean) {
this.usingBundledBiome = usingBundledBiome;
await this.checkForUpdates();
await this.checkForUpdates(this.outputChannel);
}

public async checkForUpdates() {
public async checkForUpdates(outputChannel?: OutputChannel) {
// Only check for updates if we're using the bundled version
if (!this.usingBundledBiome) {
this.statusBarUpdateItem.hide();
return;
}

const latestVersion = (await getVersions(this.context))?.[0];
const latestVersion = (await getVersions(this.context, outputChannel))?.[0];

// If the latest version cannot be fetch, do not display the update
// status bar item.
Expand Down