fix: Join and normalize paths consistently.

This commit is contained in:
Ruben Verborgh
2021-01-04 12:45:16 +01:00
committed by Joachim Van Herwegen
parent ee072b038a
commit f454b781ff
13 changed files with 120 additions and 62 deletions

View File

@@ -1,5 +1,46 @@
import platform, { posix } from 'path';
import type { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
/**
* Changes a potential Windows path into a POSIX path.
*
* @param path - Path to check (POSIX or Windows).
*
* @returns The potentially changed path (POSIX).
*/
const windowsToPosixPath = (path: string): string => path.replace(/\\+/gu, '/');
/**
* Resolves relative segments in the path/
*
* @param path - Path to check (POSIX or Windows).
*
* @returns The potentially changed path (POSIX).
*/
export const normalizeFilePath = (path: string): string =>
posix.normalize(windowsToPosixPath(path));
/**
* Adds the paths to the base path.
*
* @param basePath - The base path (POSIX or Windows).
* @param paths - Subpaths to attach (POSIX).
*
* @returns The potentially changed path (POSIX).
*/
export const joinFilePath = (basePath: string, ...paths: string[]): string =>
posix.join(windowsToPosixPath(basePath), ...paths);
/**
* Converts the path into an OS-dependent path.
*
* @param path - Path to check (POSIX).
*
* @returns The potentially changed path (OS-dependent).
*/
export const toSystemFilePath = (path: string): string =>
platform.normalize(path);
/**
* Makes sure the input path has exactly 1 slash at the end.
* Multiple slashes will get merged into one.