1
- import { dirname , join , resolve } from 'node:path'
1
+ import { dirname , join , relative , resolve } from 'node:path'
2
2
import * as fs from 'node:fs'
3
3
import process from 'node:process'
4
4
import { fileURLToPath } from 'node:url'
5
5
import { ensurePrefix , slash } from '@antfu/utils'
6
- import isInstalledGlobally from 'is-installed-globally'
7
6
import { resolveGlobal } from 'resolve-global'
8
- import { findDepPkgJsonPath } from 'vitefu'
7
+ import { findClosestPkgJsonPath , findDepPkgJsonPath } from 'vitefu'
9
8
import { resolvePath } from 'mlly'
10
9
import globalDirs from 'global-directory'
11
10
import prompts from 'prompts'
@@ -15,6 +14,8 @@ import type { RootsInfo } from '@slidev/types'
15
14
16
15
const cliRoot = fileURLToPath ( new URL ( '..' , import . meta. url ) )
17
16
17
+ export const isInstalledGlobally : { value ?: boolean } = { }
18
+
18
19
/**
19
20
* Resolve path for import url on Vite client side
20
21
*/
@@ -39,7 +40,7 @@ export async function resolveImportPath(importName: string, ensure = false) {
39
40
}
40
41
catch { }
41
42
42
- if ( isInstalledGlobally ) {
43
+ if ( isInstalledGlobally . value ) {
43
44
try {
44
45
return resolveGlobal ( importName )
45
46
}
@@ -57,7 +58,7 @@ export async function findPkgRoot(dep: string, parent: string, ensure: true): Pr
57
58
export async function findPkgRoot ( dep : string , parent : string , ensure ?: boolean ) : Promise < string | undefined >
58
59
export async function findPkgRoot ( dep : string , parent : string , ensure = false ) {
59
60
const pkgJsonPath = await findDepPkgJsonPath ( dep , parent )
60
- const path = pkgJsonPath ? dirname ( pkgJsonPath ) : isInstalledGlobally ? await findGlobalPkgRoot ( dep , false ) : undefined
61
+ const path = pkgJsonPath ? dirname ( pkgJsonPath ) : isInstalledGlobally . value ? await findGlobalPkgRoot ( dep , false ) : undefined
61
62
if ( ensure && ! path )
62
63
throw new Error ( `Failed to resolve package "${ dep } "` )
63
64
return path
@@ -104,13 +105,13 @@ export function createResolver(type: 'theme' | 'addon', officials: Record<string
104
105
name : 'confirm' ,
105
106
initial : 'Y' ,
106
107
type : 'confirm' ,
107
- message : `The ${ type } "${ pkgName } " was not found ${ underline ( isInstalledGlobally ? 'globally' : 'in your project' ) } , do you want to install it now?` ,
108
+ message : `The ${ type } "${ pkgName } " was not found ${ underline ( isInstalledGlobally . value ? 'globally' : 'in your project' ) } , do you want to install it now?` ,
108
109
} )
109
110
110
111
if ( ! confirm )
111
112
process . exit ( 1 )
112
113
113
- if ( isInstalledGlobally )
114
+ if ( isInstalledGlobally . value )
114
115
await run ( parseNi , [ '-g' , pkgName ] )
115
116
else
116
117
await run ( parseNi , [ pkgName ] )
@@ -227,10 +228,14 @@ export async function getRoots(entry?: string): Promise<RootsInfo> {
227
228
return rootsInfo
228
229
if ( ! entry )
229
230
throw new Error ( '[slidev] Cannot find roots without entry' )
230
- const clientRoot = await findPkgRoot ( '@slidev/client' , cliRoot , true )
231
231
const userRoot = dirname ( entry )
232
- const userPkgJson = getUserPkgJson ( userRoot )
233
- const userWorkspaceRoot = searchForWorkspaceRoot ( userRoot )
232
+ isInstalledGlobally . value
233
+ = ! / ^ ( \. \. \/ ) * n o d e _ m o d u l e s \/ / i. test ( slash ( relative ( userRoot , cliRoot ) ) )
234
+ || ( await import ( 'is-installed-globally' ) ) . default
235
+ const clientRoot = await findPkgRoot ( '@slidev/client' , cliRoot , true )
236
+ const closestPkgRoot = dirname ( await findClosestPkgJsonPath ( userRoot ) || userRoot )
237
+ const userPkgJson = getUserPkgJson ( closestPkgRoot )
238
+ const userWorkspaceRoot = searchForWorkspaceRoot ( closestPkgRoot )
234
239
rootsInfo = {
235
240
cliRoot,
236
241
clientRoot,
0 commit comments