Skip to content

Commit 9c6c683

Browse files
committed
feat(global): print versions includes global and local CLI
1 parent abd0919 commit 9c6c683

3 files changed

Lines changed: 44 additions & 1 deletion

File tree

packages/global/snap-tests/cli-helper-message/snap.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ Options:
3131
-V, --version Print version
3232

3333
> vp -V # show version
34-
vite-plus-global-cli <semver>
34+
Vite+ Version:
35+
- Local: Not found
36+
- Global: v<semver>
3537

3638
> vp install -h # show install help message
3739
Install all dependencies, or add packages if package names are provided

packages/global/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ if (command === 'gen' || command === 'g' || command === 'generate' || command ==
1111
import('./migration/bin.js');
1212
} else if (LOCAL_CLI_COMMANDS.includes(command)) {
1313
import('./local/bin.js');
14+
} else if (command === '--version' || command === '-V') {
15+
import('./version.js');
1416
} else {
1517
// Delegate to rust commands
1618
import('./global/bin.js');

packages/global/src/version.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { createRequire } from 'node:module';
2+
3+
import { detectPackageMetadata, VITE_PLUS_NAME } from './utils/index.js';
4+
5+
const require = createRequire(import.meta.url);
6+
7+
interface GlobalPackageJson {
8+
version: string;
9+
}
10+
11+
/**
12+
* Get the global CLI version from package.json
13+
*/
14+
export function getGlobalVersion(): string {
15+
const pkg: GlobalPackageJson = require('../package.json');
16+
return pkg.version;
17+
}
18+
19+
/**
20+
* Get the local CLI version if installed in the given directory
21+
*/
22+
export function getLocalVersion(cwd: string): string | null {
23+
const metadata = detectPackageMetadata(cwd, VITE_PLUS_NAME);
24+
return metadata?.version ?? null;
25+
}
26+
27+
/**
28+
* Print version information for both local and global CLI
29+
*/
30+
export function printVersion(cwd: string): void {
31+
const globalVersion = getGlobalVersion();
32+
const localVersion = getLocalVersion(cwd);
33+
34+
console.log('Vite+ Version:');
35+
console.log(`- Local: ${localVersion ? `v${localVersion}` : 'Not found'}`);
36+
console.log(`- Global: v${globalVersion}`);
37+
}
38+
39+
printVersion(process.cwd());

0 commit comments

Comments
 (0)