Skip to content

Commit

Permalink
Updates Upgrade command to check if local version matches latest befo…
Browse files Browse the repository at this point in the history
…re downloading/installing
  • Loading branch information
JohnPhamous committed Jan 6, 2025
1 parent 60b6b13 commit 481c230
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/lib/upgrade.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
import type { Command } from "commander";
import ora from "ora";

/**
* Returns true if the current version is the same as the latest release version.
*/
const getIsOnLatestVersion = async (currentVersion: string | undefined) => {
if (!currentVersion) {
return false;
}

const latestVersionUrl =
"https://api.github.com/repos/sfcompute/cli/releases/latest";
const latestVersionResponse = await fetch(latestVersionUrl);

if (latestVersionResponse.ok) {
const latestVersionData = await latestVersionResponse.json();
const latestVersion = latestVersionData.tag_name;

return latestVersion === currentVersion;
}

return false;
};

export function registerUpgrade(program: Command) {
return program
.command("upgrade")
.argument("[version]", "The version to upgrade to")
.description("Upgrade to the latest version or a specific version")
.action(async version => {
const spinner = ora();
const currentVersion = program.version();

if (version) {
spinner.start(`Checking if version ${version} exists`);
Expand All @@ -21,6 +44,20 @@ export function registerUpgrade(program: Command) {
spinner.succeed();
}

// Check if user has already installed latest version.
if (version === currentVersion) {
spinner.succeed(`You are already on version ${currentVersion}.`);
process.exit(0);
}

const isOnLatestVersion = await getIsOnLatestVersion(currentVersion);
if (isOnLatestVersion) {
spinner.succeed(
`You are already on the latest version (${currentVersion}).`
);
process.exit(0);
}

// Fetch the install script
spinner.start("Downloading install script");
const scriptResponse = await fetch(
Expand Down

0 comments on commit 481c230

Please sign in to comment.