mirror of
https://github.com/pnpm/pnpm.git
synced 2026-06-28 01:45:30 -04:00
18 lines
496 B
TypeScript
18 lines
496 B
TypeScript
import path from 'node:path'
|
|
|
|
interface PathUtils {
|
|
isAbsolute: (path: string) => boolean
|
|
relative: (from: string, to: string) => string
|
|
sep: string
|
|
}
|
|
|
|
export function isSubdirectory (parentDir: string, childPath: string, pathUtils: PathUtils = path): boolean {
|
|
const relativePath = pathUtils.relative(parentDir, childPath)
|
|
|
|
return relativePath === '' || (
|
|
relativePath !== '..' &&
|
|
!relativePath.startsWith(`..${pathUtils.sep}`) &&
|
|
!pathUtils.isAbsolute(relativePath)
|
|
)
|
|
}
|