2023-04-07 16:02:47 +00:00
|
|
|
import { existsSync, promises as fsp } from 'node:fs'
|
2022-04-15 15:19:05 +00:00
|
|
|
import { fileURLToPath } from 'node:url'
|
2023-04-07 16:02:47 +00:00
|
|
|
import { basename, dirname, isAbsolute, join, normalize, resolve } from 'pathe'
|
2022-01-13 18:21:49 +00:00
|
|
|
import { globby } from 'globby'
|
2023-05-10 12:11:13 +00:00
|
|
|
import { resolvePath as _resolvePath } from 'mlly'
|
2022-10-25 08:24:09 +00:00
|
|
|
import { resolveAlias as _resolveAlias } from 'pathe/utils'
|
2022-10-26 13:55:11 +00:00
|
|
|
import { tryUseNuxt } from './context'
|
2022-02-28 16:11:46 +00:00
|
|
|
import { isIgnored } from './ignore'
|
2023-12-23 14:22:58 +00:00
|
|
|
import { toArray } from './utils'
|
2020-07-02 13:02:35 +00:00
|
|
|
|
2022-02-07 21:00:20 +00:00
|
|
|
export interface ResolvePathOptions {
|
|
|
|
/** Base for resolving paths from. Default is Nuxt rootDir. */
|
|
|
|
cwd?: string
|
|
|
|
|
|
|
|
/** An object of aliases. Default is Nuxt configured aliases. */
|
2021-04-02 11:47:01 +00:00
|
|
|
alias?: Record<string, string>
|
2022-02-07 21:00:20 +00:00
|
|
|
|
|
|
|
/** The file extensions to try. Default is Nuxt configured extensions. */
|
2021-04-02 11:47:01 +00:00
|
|
|
extensions?: string[]
|
2024-04-19 09:43:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether to resolve files that exist in the Nuxt VFS (for example, as a Nuxt template).
|
|
|
|
* @default false
|
|
|
|
*/
|
|
|
|
virtual?: boolean
|
2024-06-10 16:18:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether to fallback to the original path if the resolved path does not exist instead of returning the normalized input path.
|
|
|
|
*
|
|
|
|
* @default false
|
|
|
|
*/
|
|
|
|
fallbackToOriginal?: boolean
|
2020-07-02 13:02:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 21:00:20 +00:00
|
|
|
/**
|
|
|
|
* Resolve full path to a file or directory respecting Nuxt alias and extensions options
|
|
|
|
*
|
|
|
|
* If path could not be resolved, normalized input path will be returned
|
|
|
|
*/
|
|
|
|
export async function resolvePath (path: string, opts: ResolvePathOptions = {}): Promise<string> {
|
|
|
|
// Always normalize input
|
2022-02-07 21:39:31 +00:00
|
|
|
const _path = path
|
2022-02-07 21:00:20 +00:00
|
|
|
path = normalize(path)
|
|
|
|
|
2021-04-15 18:49:29 +00:00
|
|
|
// Fast return if the path exists
|
2024-04-19 09:43:28 +00:00
|
|
|
if (isAbsolute(path)) {
|
|
|
|
if (opts?.virtual && existsInVFS(path)) {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
if (existsSync(path) && !(await isDirectory(path))) {
|
|
|
|
return path
|
|
|
|
}
|
2020-07-02 13:02:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 21:00:20 +00:00
|
|
|
// Use current nuxt options
|
2022-10-26 13:55:11 +00:00
|
|
|
const nuxt = tryUseNuxt()
|
2022-02-07 21:39:31 +00:00
|
|
|
const cwd = opts.cwd || (nuxt ? nuxt.options.rootDir : process.cwd())
|
|
|
|
const extensions = opts.extensions || (nuxt ? nuxt.options.extensions : ['.ts', '.mjs', '.cjs', '.json'])
|
|
|
|
const modulesDir = nuxt ? nuxt.options.modulesDir : []
|
2020-07-02 13:02:35 +00:00
|
|
|
|
2022-02-07 21:00:20 +00:00
|
|
|
// Resolve aliases
|
|
|
|
path = resolveAlias(path)
|
2020-07-02 13:02:35 +00:00
|
|
|
|
2022-02-07 21:00:20 +00:00
|
|
|
// Resolve relative to cwd
|
|
|
|
if (!isAbsolute(path)) {
|
|
|
|
path = resolve(cwd, path)
|
|
|
|
}
|
2021-06-30 13:55:11 +00:00
|
|
|
|
2021-04-02 11:47:01 +00:00
|
|
|
// Check if resolvedPath is a file
|
2024-04-19 09:43:28 +00:00
|
|
|
if (opts?.virtual && existsInVFS(path, nuxt)) {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2022-07-29 13:53:35 +00:00
|
|
|
let _isDir = false
|
2022-02-07 21:00:20 +00:00
|
|
|
if (existsSync(path)) {
|
2022-07-29 13:53:35 +00:00
|
|
|
_isDir = await isDirectory(path)
|
|
|
|
if (!_isDir) {
|
2022-02-07 21:00:20 +00:00
|
|
|
return path
|
2021-04-02 11:47:01 +00:00
|
|
|
}
|
2020-07-02 13:02:35 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 11:47:01 +00:00
|
|
|
// Check possible extensions
|
2022-02-07 21:00:20 +00:00
|
|
|
for (const ext of extensions) {
|
|
|
|
// path.[ext]
|
|
|
|
const pathWithExt = path + ext
|
2024-04-19 09:43:28 +00:00
|
|
|
if (opts?.virtual && existsInVFS(pathWithExt, nuxt)) {
|
|
|
|
return pathWithExt
|
|
|
|
}
|
2022-03-15 16:57:41 +00:00
|
|
|
if (existsSync(pathWithExt)) {
|
2022-02-07 21:00:20 +00:00
|
|
|
return pathWithExt
|
2021-04-02 11:47:01 +00:00
|
|
|
}
|
2022-02-07 21:00:20 +00:00
|
|
|
// path/index.[ext]
|
|
|
|
const pathWithIndex = join(path, 'index' + ext)
|
2024-04-19 09:43:28 +00:00
|
|
|
if (opts?.virtual && existsInVFS(pathWithIndex, nuxt)) {
|
|
|
|
return pathWithIndex
|
|
|
|
}
|
2022-07-29 13:53:35 +00:00
|
|
|
if (_isDir && existsSync(pathWithIndex)) {
|
2022-02-07 21:00:20 +00:00
|
|
|
return pathWithIndex
|
2020-07-02 13:02:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-07 21:00:20 +00:00
|
|
|
// Try to resolve as module id
|
2023-05-10 12:11:13 +00:00
|
|
|
const resolveModulePath = await _resolvePath(_path, { url: [cwd, ...modulesDir] }).catch(() => null)
|
2022-02-07 21:00:20 +00:00
|
|
|
if (resolveModulePath) {
|
|
|
|
return resolveModulePath
|
2020-07-02 13:02:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 21:00:20 +00:00
|
|
|
// Return normalized input
|
2024-06-10 16:18:00 +00:00
|
|
|
return opts.fallbackToOriginal ? _path : path
|
2020-07-02 13:02:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 21:00:20 +00:00
|
|
|
/**
|
|
|
|
* Try to resolve first existing file in paths
|
|
|
|
*/
|
2022-11-09 09:01:09 +00:00
|
|
|
export async function findPath (paths: string | string[], opts?: ResolvePathOptions, pathType: 'file' | 'dir' = 'file'): Promise<string | null> {
|
2024-04-19 09:43:28 +00:00
|
|
|
const nuxt = opts?.virtual ? tryUseNuxt() : undefined
|
|
|
|
|
2023-12-23 14:22:58 +00:00
|
|
|
for (const path of toArray(paths)) {
|
2022-02-07 21:00:20 +00:00
|
|
|
const rPath = await resolvePath(path, opts)
|
2024-04-19 09:43:28 +00:00
|
|
|
|
|
|
|
// Check VFS
|
|
|
|
if (opts?.virtual && existsInVFS(rPath, nuxt)) {
|
|
|
|
return rPath
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check file system
|
2022-02-07 21:00:20 +00:00
|
|
|
if (await existsSensitive(rPath)) {
|
2022-07-29 13:53:35 +00:00
|
|
|
const _isDir = await isDirectory(rPath)
|
|
|
|
if (!pathType || (pathType === 'file' && !_isDir) || (pathType === 'dir' && _isDir)) {
|
2022-03-15 16:57:41 +00:00
|
|
|
return rPath
|
|
|
|
}
|
2022-02-07 21:00:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null
|
2021-06-30 13:55:11 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 18:49:29 +00:00
|
|
|
/**
|
2022-02-07 21:00:20 +00:00
|
|
|
* Resolve path aliases respecting Nuxt alias options
|
2021-04-15 18:49:29 +00:00
|
|
|
*/
|
2022-02-07 21:00:20 +00:00
|
|
|
export function resolveAlias (path: string, alias?: Record<string, string>): string {
|
|
|
|
if (!alias) {
|
2022-04-04 09:41:48 +00:00
|
|
|
alias = tryUseNuxt()?.options.alias || {}
|
2022-02-07 21:00:20 +00:00
|
|
|
}
|
2022-10-25 08:24:09 +00:00
|
|
|
return _resolveAlias(path, alias)
|
2020-07-02 13:02:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 21:39:31 +00:00
|
|
|
export interface Resolver {
|
2022-11-09 09:01:09 +00:00
|
|
|
resolve (...path: string[]): string
|
|
|
|
resolvePath (path: string, opts?: ResolvePathOptions): Promise<string>
|
2022-02-07 21:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a relative resolver
|
|
|
|
*/
|
|
|
|
export function createResolver (base: string | URL): Resolver {
|
|
|
|
if (!base) {
|
|
|
|
throw new Error('`base` argument is missing for createResolver(base)!')
|
|
|
|
}
|
|
|
|
|
|
|
|
base = base.toString()
|
|
|
|
if (base.startsWith('file://')) {
|
|
|
|
base = dirname(fileURLToPath(base))
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
resolve: (...path) => resolve(base as string, ...path),
|
2024-04-05 18:08:32 +00:00
|
|
|
resolvePath: (path, opts) => resolvePath(path, { cwd: base as string, ...opts }),
|
2022-02-07 21:39:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-13 21:52:57 +00:00
|
|
|
export async function resolveNuxtModule (base: string, paths: string[]): Promise<string[]> {
|
|
|
|
const resolved: string[] = []
|
2023-09-22 07:25:52 +00:00
|
|
|
const resolver = createResolver(base)
|
|
|
|
|
|
|
|
for (const path of paths) {
|
|
|
|
if (path.startsWith(base)) {
|
|
|
|
resolved.push(path.split('/index.ts')[0])
|
|
|
|
} else {
|
|
|
|
const resolvedPath = await resolver.resolvePath(path)
|
|
|
|
resolved.push(resolvedPath.slice(0, resolvedPath.lastIndexOf(path) + path.length))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolved
|
|
|
|
}
|
|
|
|
|
2022-02-07 21:00:20 +00:00
|
|
|
// --- Internal ---
|
|
|
|
|
|
|
|
async function existsSensitive (path: string) {
|
|
|
|
if (!existsSync(path)) { return false }
|
|
|
|
const dirFiles = await fsp.readdir(dirname(path))
|
|
|
|
return dirFiles.includes(basename(path))
|
2020-07-02 13:02:35 +00:00
|
|
|
}
|
2021-05-20 11:42:41 +00:00
|
|
|
|
2023-01-19 13:20:53 +00:00
|
|
|
// Usage note: We assume path existence is already ensured
|
2022-07-29 13:53:35 +00:00
|
|
|
async function isDirectory (path: string) {
|
|
|
|
return (await fsp.lstat(path)).isDirectory()
|
|
|
|
}
|
|
|
|
|
2024-04-19 09:43:28 +00:00
|
|
|
function existsInVFS (path: string, nuxt = tryUseNuxt()) {
|
|
|
|
if (!nuxt) { return false }
|
|
|
|
|
|
|
|
if (path in nuxt.vfs) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
const templates = nuxt.apps.default?.templates ?? nuxt.options.build.templates
|
|
|
|
return templates.some(template => template.dst === path)
|
|
|
|
}
|
|
|
|
|
2022-08-02 12:25:35 +00:00
|
|
|
export async function resolveFiles (path: string, pattern: string | string[], opts: { followSymbolicLinks?: boolean } = {}) {
|
2024-06-13 21:52:57 +00:00
|
|
|
const files: string[] = []
|
|
|
|
for (const file of await globby(pattern, { cwd: path, followSymbolicLinks: opts.followSymbolicLinks ?? true })) {
|
|
|
|
const p = resolve(path, file)
|
|
|
|
if (!isIgnored(p)) {
|
|
|
|
files.push(p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return files.sort()
|
2021-05-20 11:42:41 +00:00
|
|
|
}
|