(from: string, to: string)
| 128 | * Build a relative path from one file path to another file path. |
| 129 | */ |
| 130 | export function buildRelativePath(from: string, to: string): string { |
| 131 | from = normalize(from); |
| 132 | to = normalize(to); |
| 133 | |
| 134 | // Convert to arrays. |
| 135 | const fromParts = from.split('/'); |
| 136 | const toParts = to.split('/'); |
| 137 | |
| 138 | // Remove file names (preserving destination) |
| 139 | fromParts.pop(); |
| 140 | const toFileName = toParts.pop(); |
| 141 | |
| 142 | const relativePath = relative( |
| 143 | normalize(fromParts.join('/') || '/'), |
| 144 | normalize(toParts.join('/') || '/'), |
| 145 | ); |
| 146 | let pathPrefix = ''; |
| 147 | |
| 148 | // Set the path prefix for same dir or child dir, parent dir starts with `..` |
| 149 | if (!relativePath) { |
| 150 | pathPrefix = '.'; |
| 151 | } else if (!relativePath.startsWith('.')) { |
| 152 | pathPrefix = `./`; |
| 153 | } |
| 154 | if (pathPrefix && !pathPrefix.endsWith('/')) { |
| 155 | pathPrefix += '/'; |
| 156 | } |
| 157 | |
| 158 | return pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName; |
| 159 | } |
no test coverage detected