fix(kit): make resolvePath case-sensitive (#291)

This commit is contained in:
Daniel Roe 2021-06-30 14:55:11 +01:00 committed by GitHub
parent 35e2b474d0
commit 6cd5f8816f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,5 @@
import { existsSync, lstatSync } from 'fs' import { existsSync, lstatSync, readdirSync } from 'fs'
import { resolve, join } from 'upath' import { basename, dirname, resolve, join } from 'upath'
import globby from 'globby' import globby from 'globby'
export interface ResolveOptions { export interface ResolveOptions {
@ -20,7 +20,7 @@ export interface ResolveOptions {
function resolvePath (path: string, opts: ResolveOptions = {}) { function resolvePath (path: string, opts: ResolveOptions = {}) {
// Fast return if the path exists // Fast return if the path exists
if (existsSync(path)) { if (existsSyncSensitive(path)) {
return path return path
} }
@ -34,9 +34,11 @@ function resolvePath (path: string, opts: ResolveOptions = {}) {
// Resolve relative to base or cwd // Resolve relative to base or cwd
resolvedPath = resolve(opts.base || '.', resolvedPath) resolvedPath = resolve(opts.base || '.', resolvedPath)
const resolvedPathFiles = readdirSync(dirname(resolvedPath))
// Check if resolvedPath is a file // Check if resolvedPath is a file
let isDirectory = false let isDirectory = false
if (existsSync(resolvedPath)) { if (existsSyncSensitive(resolvedPath, resolvedPathFiles)) {
isDirectory = lstatSync(resolvedPath).isDirectory() isDirectory = lstatSync(resolvedPath).isDirectory()
if (!isDirectory) { if (!isDirectory) {
return resolvedPath return resolvedPath
@ -47,12 +49,12 @@ function resolvePath (path: string, opts: ResolveOptions = {}) {
for (const ext of opts.extensions) { for (const ext of opts.extensions) {
// resolvedPath.[ext] // resolvedPath.[ext]
const resolvedPathwithExt = resolvedPath + ext const resolvedPathwithExt = resolvedPath + ext
if (!isDirectory && existsSync(resolvedPathwithExt)) { if (!isDirectory && existsSyncSensitive(resolvedPathwithExt, resolvedPathFiles)) {
return resolvedPathwithExt return resolvedPathwithExt
} }
// resolvedPath/index.[ext] // resolvedPath/index.[ext]
const resolvedPathwithIndex = join(resolvedPath, 'index' + ext) const resolvedPathwithIndex = join(resolvedPath, 'index' + ext)
if (isDirectory && existsSync(resolvedPathwithIndex)) { if (isDirectory && existsSyncSensitive(resolvedPathwithIndex)) {
return resolvedPathwithIndex return resolvedPathwithIndex
} }
} }
@ -66,6 +68,12 @@ function resolvePath (path: string, opts: ResolveOptions = {}) {
throw new Error(`Cannot resolve "${path}" from "${resolvedPath}"`) throw new Error(`Cannot resolve "${path}" from "${resolvedPath}"`)
} }
function existsSyncSensitive (path: string, files?: string[]) {
if (!existsSync(path)) { return false }
const _files = files || readdirSync(dirname(path))
return _files.includes(basename(path))
}
/** /**
* Return a path with any relevant aliases resolved. * Return a path with any relevant aliases resolved.
* *