Skip to content

Commit

Permalink
feat(cli): verify installed version
Browse files Browse the repository at this point in the history
  • Loading branch information
bjohansebas committed Jul 11, 2024
1 parent e0d8adc commit f97d107
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"ora": "8.0.1",
"picocolors": "1.0.1",
"typescript": "5.5.3",
"update-check": "1.5.4",
"vitest": "1.6.0"
},
"homepage": "https://github.com/bjohansebas/rapidbuild",
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import { green } from 'picocolors'

import packageJson from '../package.json'
import { scannerCommand } from './commands/scan'
import { checkUpdates } from './utils/checkUpdates'

export const program = new Command(packageJson.name)
.version(packageJson.version)
.usage(`${green('[command]')} ${green('[options]')}`)
.hook('postAction', async () => {
await checkUpdates()
})

// program
// .command('add')
Expand Down
39 changes: 39 additions & 0 deletions packages/cli/src/utils/checkUpdates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { bold, cyan, yellow } from 'picocolors'
import checkForUpdate from 'update-check'

import packageJson from '../../package.json'

export async function checkUpdates(): Promise<void> {
const userAgent = process.env.npm_config_user_agent || ''

let packageManager = 'npm'

if (userAgent.startsWith('yarn')) packageManager = 'yarn'

if (userAgent.startsWith('pnpm')) packageManager = 'pnpm'

if (userAgent.startsWith('bun')) packageManager = 'bun'

try {
const res = await checkForUpdate(packageJson).catch(() => null)

if (res?.latest) {
const updateMessage =
packageManager === 'yarn'
? 'yarn global add rapidbuild'
: packageManager === 'pnpm'
? 'pnpm add -g rapidbuild'
: packageManager === 'bun'
? 'bun add -g rapidbuild'
: 'npm i -g rapidbuild'

console.log(
`\n${yellow(bold('A new version of `rapidbuild` is available!'))}\n\nYou can update by running: ${cyan(updateMessage)}\n`,
)
}

process.exit()
} catch {
// ignore error
}
}
65 changes: 65 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f97d107

Please sign in to comment.