-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
Comment on lines
+17
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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`); | ||
|
@@ -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( | ||
|
There was a problem hiding this comment.
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