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

Updates Upgrade command to check if local version matches latest before downloading/installing #50

Merged
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
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);
Copy link

Choose a reason for hiding this comment

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

logic: missing try/catch around fetch - network errors will cause uncaught exceptions


if (latestVersionResponse.ok) {
const latestVersionData = await latestVersionResponse.json();
const latestVersion = latestVersionData.tag_name;
Comment on lines +17 to +18
Copy link

Choose a reason for hiding this comment

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

logic: missing try/catch around json parsing and accessing tag_name - malformed response will throw


return latestVersion === currentVersion;
Copy link

Choose a reason for hiding this comment

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

style: version comparison should use semver library for proper version string comparison

}

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
Loading