@@ -5,14 +5,16 @@ import path from 'node:path';
55import { vitePlusHeader } from '../binding/index.js' ;
66import { VITE_PLUS_NAME } from './utils/constants.js' ;
77import { renderCliDoc } from './utils/help.js' ;
8- import { detectPackageMetadata } from './utils/package.js' ;
8+ import { detectPackageMetadata , hasVitePlusDependency } from './utils/package.js' ;
99import { accent , log } from './utils/terminal.js' ;
1010
1111const require = createRequire ( import . meta. url ) ;
1212
1313interface PackageJson {
14- version : string ;
14+ version ? : string ;
1515 bundledVersions ?: Record < string , string > ;
16+ dependencies ?: Record < string , string > ;
17+ devDependencies ?: Record < string , string > ;
1618}
1719
1820interface LocalPackageMetadata {
@@ -38,9 +40,29 @@ function getCliVersion(): string | null {
3840}
3941
4042function getLocalMetadata ( cwd : string ) : LocalPackageMetadata | null {
43+ if ( ! isVitePlusDeclaredInAncestors ( cwd ) ) {
44+ return null ;
45+ }
4146 return detectPackageMetadata ( cwd , VITE_PLUS_NAME ) ?? null ;
4247}
4348
49+ function isVitePlusDeclaredInAncestors ( cwd : string ) : boolean {
50+ let currentDir = path . resolve ( cwd ) ;
51+ while ( true ) {
52+ const packageJsonPath = path . join ( currentDir , 'package.json' ) ;
53+ const pkg = readPackageJsonFromPath ( packageJsonPath ) ;
54+ if ( pkg && hasVitePlusDependency ( pkg ) ) {
55+ return true ;
56+ }
57+ const parentDir = path . dirname ( currentDir ) ;
58+ if ( parentDir === currentDir ) {
59+ break ;
60+ }
61+ currentDir = parentDir ;
62+ }
63+ return false ;
64+ }
65+
4466function readPackageJsonFromPath ( packageJsonPath : string ) : PackageJson | null {
4567 try {
4668 return JSON . parse ( fs . readFileSync ( packageJsonPath , 'utf8' ) ) as PackageJson ;
@@ -158,18 +180,20 @@ export async function printVersion(cwd: string) {
158180 } ,
159181 ] ;
160182
161- const resolvedTools = tools . map ( ( tool ) => ( {
162- tool,
163- version : localMetadata ? resolveToolVersion ( tool , localMetadata . path ) : null ,
164- } ) ) ;
165-
166- sections . push ( {
167- title : 'Tools' ,
168- rows : resolvedTools . map ( ( { tool, version } ) => ( {
169- label : accent ( tool . displayName ) ,
170- description : version ? `v${ version } ` : 'Not found' ,
171- } ) ) ,
172- } ) ;
183+ if ( localMetadata ) {
184+ const resolvedTools = tools . map ( ( tool ) => ( {
185+ tool,
186+ version : resolveToolVersion ( tool , localMetadata . path ) ,
187+ } ) ) ;
188+
189+ sections . push ( {
190+ title : 'Tools' ,
191+ rows : resolvedTools . map ( ( { tool, version } ) => ( {
192+ label : accent ( tool . displayName ) ,
193+ description : version ? `v${ version } ` : 'Not found' ,
194+ } ) ) ,
195+ } ) ;
196+ }
173197
174198 log ( renderCliDoc ( { sections } ) ) ;
175199}
0 commit comments