UNPKG

766 BJavaScriptView Raw
1// @flow strict-local
2import type {FilePath} from '@parcel/types';
3import path from 'path';
4
5const SEPARATOR_REGEX = /[\\]+/g;
6
7export function normalizeSeparators(filePath: FilePath): FilePath {
8 return filePath.replace(SEPARATOR_REGEX, '/');
9}
10
11export type PathOptions = {
12 noLeadingDotSlash?: boolean,
13 ...
14};
15
16export function normalizePath(
17 filePath: FilePath,
18 leadingDotSlash: boolean = true,
19): FilePath {
20 if (leadingDotSlash && filePath[0] !== '.' && filePath[0] !== '/') {
21 return normalizeSeparators('./' + filePath);
22 } else {
23 return normalizeSeparators(filePath);
24 }
25}
26
27export function relativePath(
28 from: string,
29 to: string,
30 leadingDotSlash: boolean = true,
31) {
32 return normalizePath(path.relative(from, to), leadingDotSlash);
33}