返回 slidev
resolver.ts
根目录 / packages / slidev / node / resolver.ts
1 import type { RootsInfo } from '@slidev/types'
2 import { existsSync, lstatSync, readdirSync, readlinkSync } from 'node:fs'
3 import { copyFile, readFile } from 'node:fs/promises'
4 import process from 'node:process'
5 import { fileURLToPath, pathToFileURL } from 'node:url'
6 import { parseNi, run } from '@antfu/ni'
7 import { ensurePrefix, slash } from '@antfu/utils'
8 import { underline, yellow } from 'ansis'
9 import globalDirs from 'global-directory'
10 import { resolve as resolveModuleUrl, resolvePath } from 'mlly'
11 import { dirname, join, relative, resolve, sep } from 'pathe'
12 import prompts from 'prompts'
13 import { resolveGlobal } from 'resolve-global'
14 import { findClosestPkgJsonPath, findDepPkgJsonPath } from 'vitefu'
15
16 const RE_PATH_SEPARATOR = /[/\\]/
17 const RE_SAFE_PKG_NAME = /^(?:@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/
18
19 const cliRoot = fileURLToPath(new URL('..', import.meta.url))
20
21 /**
22 * Detect the `node_modules/` directory the running slidev binary was
23 * dispatched from. The simple `findDepPkgJsonPath(name, cliRoot)` walk fails
24 * in pnpm v11+ global installs because `cliRoot` is the realpath inside the
25 * content-addressable store, with no path back to the install directory. The
26 * invocation path (`process.argv[1]`), however, is one of:
27 *
28 * - `{PNPM_HOME}/v11/{hash}/node_modules/@slidev/cli/bin/slidev.mjs` — pnpm
29 * v11 globals dispatch through a shell shim in `{PNPM_HOME}/bin/` that exec's
30 * `node` with this argv[1].
31 * - `{globals}/node_modules/@slidev/cli/bin/slidev.mjs` — npm/yarn globals,
32 * where the bin is a fs symlink and the resolved script path keeps the
33 * `node_modules` segment.
34 * - `./node_modules/.bin/slidev` — local project, where Node resolves the
35 * `.bin` symlink before populating argv[1].
36 *
37 * Returns the trailing `node_modules` directory of that path, or `undefined`
38 * if `argv1` doesn't pass through one.
39 *
40 * Exported for tests.
41 */
42 export function findInvocationNodeModulesPath(argv1: string | undefined): string | undefined {
43 if (!argv1)
44 return undefined
45 // Direct hit on the literal path passed to Node.
46 const direct = resolve(argv1)
47 const segment = `${sep}node_modules${sep}`
48 const directIdx = direct.lastIndexOf(segment)
49 if (directIdx >= 0)
50 return direct.slice(0, directIdx + 1 + 'node_modules'.length)
51 // Fall back to following an fs symlink chain (covers the legacy `.bin`
52 // symlink layout where argv[1] is a path *to* the symlink itself).
53 let current = direct
54 for (let i = 0; i < 16; i++) {
55 let stat
56 try {
57 stat = lstatSync(current)
58 }
59 catch {
60 return undefined
61 }
62 if (!stat.isSymbolicLink())
63 return undefined
64 const target = readlinkSync(current)
65 // `readlinkSync` returns the target verbatim — possibly with the *wrong*
66 // separator on Windows (e.g. a target written with `/` by a cross-platform
67 // tool). Resolve through the symlink's directory in every case so the
68 // separator-sensitive `node_modules` scan below sees a platform-normalized
69 // path.
70 current = resolve(dirname(current), target)
71 const idx = current.lastIndexOf(segment)
72 if (idx >= 0)
73 return current.slice(0, idx + 1 + 'node_modules'.length)
74 }
75 return undefined
76 }
77
78 /**
79 * Candidate `node_modules` directories to search when slidev is installed
80 * globally. Always includes the invocation's own `node_modules` (the install
81 * group `@slidev/cli` lives in), plus every sibling install group's
82 * `node_modules` so cross-group lookups can succeed.
83 *
84 * pnpm v11 lays globals out as `{root}/v11/{hash}/node_modules/...`; each
85 * `pnpm add -g <pkg>` invocation creates its own `{hash}` directory even
86 * when multiple packages are listed in one call. Walking the parent of the
87 * cli's install group lets us discover packages that landed in sibling
88 * groups (e.g. a theme installed alongside `@slidev/cli`).
89 *
90 * Exported for tests.
91 */
92 export function computeInvocationSearchPaths(ownNodeModules: string | undefined): string[] {
93 if (!ownNodeModules)
94 return []
95 const paths = [ownNodeModules]
96 const installDir = dirname(ownNodeModules)
97 const globalRoot = dirname(installDir)
98 if (!globalRoot || globalRoot === installDir)
99 return paths
100 let siblings: string[]
101 try {
102 siblings = readdirSync(globalRoot)
103 }
104 catch {
105 return paths
106 }
107 for (const sib of siblings) {
108 const sibInstall = join(globalRoot, sib)
109 if (sibInstall === installDir)
110 continue
111 const sibNm = join(sibInstall, 'node_modules')
112 try {
113 if (lstatSync(sibNm).isDirectory())
114 paths.push(sibNm)
115 }
116 catch { }
117 }
118 return paths
119 }
120
121 const invocationNodeModules = findInvocationNodeModulesPath(process.argv[1])
122 const invocationSearchPaths = computeInvocationSearchPaths(invocationNodeModules)
123
124 export const isInstalledGlobally: { value?: boolean } = {}
125
126 /**
127 * Resolve path for import url on Vite client side
128 */
129 export async function resolveImportUrl(id: string) {
130 return toAtFS(await resolveImportPath(id, true))
131 }
132
133 export function toAtFS(path: string) {
134 return `/@fs${ensurePrefix('/', slash(path))}`
135 }
136
137 /**
138 * Find the actual path of the import. If Slidev is installed globally, it will also search globally.
139 */
140 export async function resolveImportPath(importName: string, ensure: true): Promise<string>
141 export async function resolveImportPath(importName: string, ensure?: boolean): Promise<string | undefined>
142 export async function resolveImportPath(importName: string, ensure = false) {
143 try {
144 return await resolvePath(importName, {
145 url: import.meta.url,
146 })
147 }
148 catch { }
149
150 if (isInstalledGlobally.value) {
151 for (const nm of invocationSearchPaths) {
152 try {
153 return await resolvePath(importName, {
154 url: pathToFileURL(`${nm}${sep}`),
155 })
156 }
157 catch { }
158 }
159 try {
160 return resolveGlobal(importName)
161 }
162 catch { }
163 }
164
165 if (ensure)
166 throw new Error(`Failed to resolve package "${importName}"`)
167 }
168
169 /**
170 * Import an optional dependency (typically an optional peer dependency such as
171 * `vite-plugin-pwa`) that the user may or may not have installed. Resolution is
172 * attempted, in order, from the user's project root, the workspace root, the
173 * global registry (when Slidev runs globally), and finally the cli's own
174 * dependencies. Returns `undefined` when the package can't be resolved anywhere.
175 */
176 export async function importOptionalDependency<T = any>(name: string): Promise<T | undefined> {
177 const roots: string[] = []
178 try {
179 const { userRoot, userWorkspaceRoot } = await getRoots()
180 roots.push(userRoot)
181 if (userWorkspaceRoot !== userRoot)
182 roots.push(userWorkspaceRoot)
183 }
184 catch { }
185
186 for (const root of roots) {
187 try {
188 return await import(await resolveModuleUrl(name, { url: pathToFileURL(`${root}${sep}`).href }))
189 }
190 catch { }
191 }
192
193 if (isInstalledGlobally.value) {
194 try {
195 return await import(resolveGlobal(name))
196 }
197 catch { }
198 }
199
200 try {
201 return await import(name)
202 }
203 catch { }
204
205 return undefined
206 }
207
208 /**
209 * Prompt the user to install a missing optional dependency, then install it
210 * with the detected package manager. Exits the process when the user declines
211 * or when stdin isn't interactive (so it can't prompt).
212 *
213 * `purpose` describes why the package is needed, e.g. `The "pwa" option`.
214 */
215 export async function promptForOptionalInstallation(pkgName: string, purpose: string): Promise<void> {
216 // Check if stdin is available for prompts (i.e., is a TTY)
217 if (!process.stdin.isTTY) {
218 console.error(
219 `${purpose} requires the "${pkgName}" package, which is not installed, and cannot prompt for installation. `
220 + `Install it with \`npm i -D ${pkgName}\` (or your package manager's equivalent) and try again.`,
221 )
222 process.exit(1)
223 }
224
225 const { confirm } = await prompts({
226 name: 'confirm',
227 initial: 'Y',
228 type: 'confirm',
229 message: `${purpose} requires the "${yellow(pkgName)}" package, which is not installed ${underline(isInstalledGlobally.value ? 'globally' : 'in your project')}. Install it now?`,
230 })
231
232 if (!confirm)
233 process.exit(1)
234
235 if (isInstalledGlobally.value)
236 await run(parseNi, ['-g', pkgName])
237 else
238 await run(parseNi, [pkgName])
239 }
240
241 /**
242 * Find the root of the package. If Slidev is installed globally, it will also search globally.
243 */
244 export async function findPkgRoot(dep: string, parent: string, ensure: true): Promise<string>
245 export async function findPkgRoot(dep: string, parent: string, ensure?: boolean): Promise<string | undefined>
246 export async function findPkgRoot(dep: string, parent: string, ensure = false) {
247 const pkgJsonPath = await findDepPkgJsonPath(dep, parent)
248 const path = pkgJsonPath
249 ? dirname(pkgJsonPath)
250 : isInstalledGlobally.value
251 ? await findGlobalPkgRoot(dep, false)
252 : undefined
253 if (ensure && !path)
254 throw new Error(`Failed to resolve package "${dep}"`)
255 return path
256 }
257
258 export async function findGlobalPkgRoot(name: string, ensure: true): Promise<string>
259 export async function findGlobalPkgRoot(name: string, ensure?: boolean): Promise<string | undefined>
260 export async function findGlobalPkgRoot(name: string, ensure = false) {
261 const localPath = await findDepPkgJsonPath(name, cliRoot)
262 if (localPath)
263 return dirname(localPath)
264 for (const nm of invocationSearchPaths) {
265 const direct = join(nm, ...name.split('/'), 'package.json')
266 if (existsSync(direct))
267 return dirname(direct)
268 const walked = await findDepPkgJsonPath(name, nm)
269 if (walked)
270 return dirname(walked)
271 }
272 const yarnPath = join(globalDirs.yarn.packages, name)
273 if (existsSync(`${yarnPath}/package.json`))
274 return yarnPath
275 const npmPath = join(globalDirs.npm.packages, name)
276 if (existsSync(`${npmPath}/package.json`))
277 return npmPath
278 if (ensure)
279 throw new Error(`Failed to resolve global package "${name}"`)
280 }
281
282 export async function resolveEntry(entryRaw: string) {
283 if (!existsSync(entryRaw) && !entryRaw.endsWith('.md') && !RE_PATH_SEPARATOR.test(entryRaw))
284 entryRaw += '.md'
285 const entry = resolve(entryRaw)
286 if (!existsSync(entry)) {
287 // Check if stdin is available for prompts (i.e., is a TTY)
288 if (!process.stdin.isTTY) {
289 console.error(`Entry file "${entry}" does not exist and cannot prompt for confirmation`)
290 process.exit(1)
291 }
292 const { create } = await prompts({
293 name: 'create',
294 type: 'confirm',
295 initial: 'Y',
296 message: `Entry file ${yellow(`"${entry}"`)} does not exist, do you want to create it?`,
297 })
298 if (create)
299 await copyFile(resolve(cliRoot, 'template.md'), entry)
300 else
301 process.exit(0)
302 }
303 return slash(entry)
304 }
305
306 /**
307 * Create a resolver for theme or addon
308 */
309 export function createResolver(type: 'theme' | 'addon', officials: Record<string, string>) {
310 async function promptForInstallation(pkgName: string) {
311 // Check if stdin is available for prompts (i.e., is a TTY)
312 if (!process.stdin.isTTY) {
313 console.error(`The ${type} "${pkgName}" was not found and cannot prompt for installation`)
314 process.exit(1)
315 }
316
317 const { confirm } = await prompts({
318 name: 'confirm',
319 initial: 'Y',
320 type: 'confirm',
321 message: `The ${type} "${pkgName}" was not found ${underline(isInstalledGlobally.value ? 'globally' : 'in your project')}, do you want to install it now?`,
322 })
323
324 if (!confirm)
325 process.exit(1)
326
327 if (isInstalledGlobally.value)
328 await run(parseNi, ['-g', pkgName])
329 else
330 await run(parseNi, [pkgName])
331 }
332
333 return async function (name: string, importer: string): Promise<[name: string, root: string | null]> {
334 const { userRoot } = await getRoots()
335
336 if (name === 'none')
337 return ['', null]
338
339 // local path
340 if (name[0] === '/')
341 return [name, name]
342 if (name.startsWith('@/')) {
343 return [
344 name,
345 resolve(userRoot, name.slice(2)),
346 ]
347 }
348 if (name[0] === '.' || (name[0] !== '@' && name.includes('/'))) {
349 return [
350 name,
351 resolve(dirname(importer), name),
352 ]
353 }
354
355 // Validate that the name is a safe npm package name before resolving
356 if (!RE_SAFE_PKG_NAME.test(name))
357 throw new Error(`Invalid ${type} name "${name}". Only valid npm package names are allowed.`)
358
359 // search for local packages first
360 {
361 const possiblePkgNames = [name]
362
363 if (!name.includes('/') && !name.startsWith('@')) {
364 possiblePkgNames.unshift(
365 `@slidev/${type}-${name}`,
366 `slidev-${type}-${name}`,
367 )
368 }
369
370 for (const pkgName of possiblePkgNames) {
371 const pkgRoot = await findPkgRoot(pkgName, importer)
372 if (pkgRoot)
373 return [pkgName, pkgRoot]
374 }
375 }
376
377 // fallback to prompt install
378 const pkgName = officials[name] ?? (name[0] === '@'
379 ? name
380 : `slidev-${type}-${name}`)
381 await promptForInstallation(pkgName)
382 return [pkgName, await findPkgRoot(pkgName, importer, true)]
383 }
384 }
385
386 async function getUserPkgJson(userRoot: string) {
387 const path = resolve(userRoot, 'package.json')
388 if (existsSync(path))
389 return JSON.parse(await readFile(path, 'utf-8')) as Record<string, any>
390 return {}
391 }
392
393 // npm: https://docs.npmjs.com/cli/v7/using-npm/workspaces#installing-workspaces
394 // yarn: https://classic.yarnpkg.com/en/docs/workspaces/#toc-how-to-use-it
395 async function hasWorkspacePackageJSON(root: string): Promise<boolean> {
396 const path = join(root, 'package.json')
397 if (!existsSync(path))
398 return false
399 const content = JSON.parse(await readFile(path, 'utf-8')) || {}
400 return !!content.workspaces
401 }
402
403 function hasRootFile(root: string): boolean {
404 // https://github.com/vitejs/vite/issues/2820#issuecomment-812495079
405 const ROOT_FILES = [
406 // '.git',
407
408 // https://pnpm.js.org/workspaces/
409 'pnpm-workspace.yaml',
410
411 // https://rushjs.io/pages/advanced/config_files/
412 // 'rush.json',
413
414 // https://nx.dev/latest/react/getting-started/nx-setup
415 // 'workspace.json',
416 // 'nx.json'
417 ]
418
419 return ROOT_FILES.some(file => existsSync(join(root, file)))
420 }
421
422 /**
423 * Search up for the nearest workspace root
424 */
425 async function searchForWorkspaceRoot(
426 current: string,
427 root = current,
428 ): Promise<string> {
429 if (hasRootFile(current))
430 return current
431 if (await hasWorkspacePackageJSON(current))
432 return current
433
434 const dir = dirname(current)
435 // reach the fs root
436 if (!dir || dir === current)
437 return root
438
439 return searchForWorkspaceRoot(dir, root)
440 }
441
442 let rootsInfo: RootsInfo | null = null
443
444 export async function getRoots(entry?: string): Promise<RootsInfo> {
445 if (rootsInfo)
446 return rootsInfo
447 if (!entry)
448 throw new Error('[slidev] Cannot find roots without entry')
449 const userRoot = dirname(entry)
450 isInstalledGlobally.value
451 = slash(relative(userRoot, process.argv[1])).includes('/.pnpm/')
452 // pnpm v11 isolated globals don't expose a `.pnpm/` segment in argv[1]
453 // and aren't detected by `is-installed-globally` (which only knows npm
454 // and yarn). The cli's bin is symlinked into an install-group
455 // `node_modules/` that's outside the user's workspace, so use that as
456 // the global-mode signal.
457 || (invocationNodeModules != null
458 && slash(relative(userRoot, invocationNodeModules)).startsWith('..'))
459 || (await import('is-installed-globally')).default
460 const clientRoot = await findPkgRoot('@slidev/client', cliRoot, true)
461 const closestPkgRoot = dirname(await findClosestPkgJsonPath(userRoot) || userRoot)
462 const userPkgJson = await getUserPkgJson(closestPkgRoot)
463 const userWorkspaceRoot = await searchForWorkspaceRoot(closestPkgRoot)
464 rootsInfo = {
465 cliRoot,
466 clientRoot,
467 userRoot,
468 userPkgJson,
469 userWorkspaceRoot,
470 }
471 return rootsInfo
472 }
473
474 export function resolveSourceFiles(
475 roots: string[],
476 subpath: string,
477 extensions = ['.mjs', '.js', '.mts', '.ts'], // The same order as https://vite.dev/config/shared-options#resolve-extensions
478 ) {
479 const results: string[] = []
480 for (const root of roots) {
481 for (const ext of extensions) {
482 const fullPath = join(root, subpath + ext)
483 if (existsSync(fullPath)) {
484 results.push(fullPath)
485 break
486 }
487 }
488 }
489 return results
490 }
491
491 lines TYPESCRIPT