From 7cfa9dc69bfdee22b279fbd41bd75cc7f1d12d55 Mon Sep 17 00:00:00 2001 From: MK Date: Tue, 16 Dec 2025 10:07:01 +0800 Subject: [PATCH] feat(global): print versions includes global and local CLI --- .../snap-tests/cli-helper-message/snap.txt | 4 +- packages/global/src/index.ts | 2 + packages/global/src/version.ts | 39 +++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 packages/global/src/version.ts diff --git a/packages/global/snap-tests/cli-helper-message/snap.txt b/packages/global/snap-tests/cli-helper-message/snap.txt index 04601146ec..c006b36947 100644 --- a/packages/global/snap-tests/cli-helper-message/snap.txt +++ b/packages/global/snap-tests/cli-helper-message/snap.txt @@ -31,7 +31,9 @@ Options: -V, --version Print version > vp -V # show version -vite-plus-global-cli +Vite+ Version: +- Local: Not found +- Global: v > vp install -h # show install help message Install all dependencies, or add packages if package names are provided diff --git a/packages/global/src/index.ts b/packages/global/src/index.ts index 76f70ae0a0..7650aeada5 100644 --- a/packages/global/src/index.ts +++ b/packages/global/src/index.ts @@ -11,6 +11,8 @@ if (command === 'gen' || command === 'g' || command === 'generate' || command == import('./migration/bin.js'); } else if (LOCAL_CLI_COMMANDS.includes(command)) { import('./local/bin.js'); +} else if (command === '--version' || command === '-V') { + import('./version.js'); } else { // Delegate to rust commands import('./global/bin.js'); diff --git a/packages/global/src/version.ts b/packages/global/src/version.ts new file mode 100644 index 0000000000..d37c32f1fd --- /dev/null +++ b/packages/global/src/version.ts @@ -0,0 +1,39 @@ +import { createRequire } from 'node:module'; + +import { detectPackageMetadata, VITE_PLUS_NAME } from './utils/index.js'; + +const require = createRequire(import.meta.url); + +interface GlobalPackageJson { + version: string; +} + +/** + * Get the global CLI version from package.json + */ +export function getGlobalVersion(): string { + const pkg: GlobalPackageJson = require('../package.json'); + return pkg.version; +} + +/** + * Get the local CLI version if installed in the given directory + */ +export function getLocalVersion(cwd: string): string | null { + const metadata = detectPackageMetadata(cwd, VITE_PLUS_NAME); + return metadata?.version ?? null; +} + +/** + * Print version information for both local and global CLI + */ +export function printVersion(cwd: string): void { + const globalVersion = getGlobalVersion(); + const localVersion = getLocalVersion(cwd); + + console.log('Vite+ Version:'); + console.log(`- Local: ${localVersion ? `v${localVersion}` : 'Not found'}`); + console.log(`- Global: v${globalVersion}`); +} + +printVersion(process.cwd());