-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
512ee53
commit 690c326
Showing
6 changed files
with
70 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Command } from 'commander' | ||
import { intro, outro } from '@clack/prompts' | ||
import { isLatestVersion, installLatest, onCancel } from '../utils' | ||
|
||
export function upgradeCommand(program: Command): void { | ||
program | ||
.command('upgrade') | ||
.alias('u') | ||
.description('Upgrade the Shelve CLI to the latest version') | ||
.action(async () => { | ||
intro('Upgrading Shelve CLI to the latest version') | ||
|
||
try { | ||
const isLatest = await isLatestVersion() | ||
if (isLatest) { | ||
outro('Shelve CLI is already up to date') | ||
return | ||
} | ||
|
||
await installLatest() | ||
outro('Shelve CLI has been successfully updated') | ||
} catch (error) { | ||
console.error(error) | ||
onCancel('Failed to check for updates') | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,41 @@ | ||
import { cancel } from '@clack/prompts' | ||
import { cancel, spinner, note } from '@clack/prompts' | ||
import { addDependency } from 'nypm' | ||
import semver from 'semver' | ||
import npmFetch from 'npm-registry-fetch' | ||
import { version } from '../../package.json' | ||
|
||
const s = spinner() | ||
|
||
export function onCancel(message: string, output: number = 0): never { | ||
cancel(message) | ||
process.exit(output) | ||
} | ||
|
||
export async function isLatestVersion(): Promise<boolean> { | ||
s.start('Checking for updates') | ||
|
||
const packageInfo = await npmFetch.json('/@shelve/cli') as { | ||
'dist-tags': { | ||
latest: string | ||
} | ||
} | ||
|
||
s.stop('Checking for updates') | ||
|
||
const latestVersion = packageInfo['dist-tags'].latest | ||
const isUpdated = semver.gte(version, latestVersion) | ||
|
||
if (!isUpdated) | ||
note(`Shelve CLI ${version} is available (latest version is ${latestVersion})`, 'Update available') | ||
|
||
return isUpdated | ||
} | ||
|
||
export async function installLatest(): Promise<void> { | ||
s.start('Updating Shelve CLI') | ||
await addDependency('@shelve/cli', { | ||
silent: true, | ||
global: true | ||
}) | ||
s.stop('Updating Shelve CLI') | ||
} |