(p: string | undefined)
| 83 | /// Shortens a path to use ~ if it's inside the home directory and always |
| 84 | // uses forward slashes in that case. |
| 85 | export function homeRelativePath(p: string | undefined) { |
| 86 | if (!p) return undefined; |
| 87 | const homedir = os.homedir(); |
| 88 | if (isWithinPath(p, homedir)) { |
| 89 | if (isWin) |
| 90 | return path.join("~", path.relative(homedir, p)).replace(/\\/g, "/"); |
| 91 | else |
| 92 | return path.join("~", path.relative(homedir, p)); |
| 93 | } |
| 94 | return p; |
| 95 | } |
| 96 | |
| 97 | export function isWithinPath(file: string, folder: string) { |
| 98 | const relative = path.relative(folder.toLowerCase(), file.toLowerCase()); |
no test coverage detected