Skip to content

Commit

Permalink
feat: Add upgrade command (#211)
Browse files Browse the repository at this point in the history
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
HugoRCD and autofix-ci[bot] authored Sep 20, 2024
1 parent 512ee53 commit 690c326
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 2 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Commands:
pull|pl [options] Pull variables for specified environment to .env file
push|ps [options] Push variables for specified environment to Shelve
generate|g Generate resources for a project
upgrade|u Upgrade the Shelve CLI to the latest version
config|cf Show the current configuration
help [command] display help for command
```
Expand Down
5 changes: 4 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shelve/cli",
"version": "2.2.1",
"version": "2.3.0",
"description": "The command-line interface for Shelve",
"homepage": "https://shelve.hrcd.fr",
"bugs": {
Expand Down Expand Up @@ -43,13 +43,16 @@
"c12": "^1.11.2",
"commander": "^12.1.0",
"consola": "^3.2.3",
"npm-registry-fetch": "^17.1.0",
"nypm": "^0.3.11",
"ofetch": "^1.3.4",
"semver": "^7.6.3",
"unbuild": "^2.0.0"
},
"devDependencies": {
"@shelve/types": "workspace:*",
"@types/bun": "^1.1.9",
"@types/npm-registry-fetch": "^8.0.7",
"@types/semver": "^7.5.8",
"eslint": "^9.10.0",
"release-it": "^17.6.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { pullCommand } from './pull'
import { pushCommand } from './push'
import { generateCommand } from './generate'
import { createCommand } from './create'
import { upgradeCommand } from './upgrade'

export function registerCommands(program: Command): void {
createCommand(program)
pullCommand(program)
pushCommand(program)
generateCommand(program)
upgradeCommand(program)
configCommand(program)
}
27 changes: 27 additions & 0 deletions packages/cli/src/commands/upgrade.ts
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')
}
})
}
37 changes: 36 additions & 1 deletion packages/cli/src/utils/index.ts
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')
}

0 comments on commit 690c326

Please sign in to comment.