( from: string, to: string, stripExtension: boolean = false, )
| 63 | } |
| 64 | |
| 65 | export function relativePath( |
| 66 | from: string, |
| 67 | to: string, |
| 68 | stripExtension: boolean = false, |
| 69 | ) { |
| 70 | // Always normalize backslashes to forward slashes for Windows compatibility |
| 71 | const normalized = from.replace(/\\/g, '/') |
| 72 | const cleanedFrom = normalized.startsWith('./') |
| 73 | ? normalized.slice(2) |
| 74 | : normalized |
| 75 | const cleanedTo = to.startsWith('./') ? to.slice(2) : to |
| 76 | |
| 77 | const fromSegments = cleanedFrom.split('/') |
| 78 | const toSegments = cleanedTo.split('/') |
| 79 | |
| 80 | fromSegments.pop() |
| 81 | toSegments.pop() |
| 82 | |
| 83 | let commonIndex = 0 |
| 84 | while ( |
| 85 | commonIndex < fromSegments.length && |
| 86 | commonIndex < toSegments.length && |
| 87 | fromSegments[commonIndex] === toSegments[commonIndex] |
| 88 | ) { |
| 89 | commonIndex++ |
| 90 | } |
| 91 | |
| 92 | const upLevels = fromSegments.length - commonIndex |
| 93 | const downLevels = toSegments.slice(commonIndex) |
| 94 | |
| 95 | if (stripExtension) { |
| 96 | to = to.replace(extname(to), '') |
| 97 | } |
| 98 | |
| 99 | if (upLevels === 0 && downLevels.length === 0) { |
| 100 | return `./${basename(to)}` |
| 101 | } else if (upLevels === 0 && downLevels.length > 0) { |
| 102 | return `./${downLevels.join('/')}/${basename(to)}` |
| 103 | } else { |
| 104 | const relativePath = [...Array(upLevels).fill('..'), ...downLevels].join( |
| 105 | '/', |
| 106 | ) |
| 107 | return `${relativePath}/${basename(to)}` |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | export function isDirectory(path: string): boolean { |
| 112 | return statSync(path).isDirectory() |
no test coverage detected