1 |
|
2 | import type {FilePath} from '@parcel/types';
|
3 | import path from 'path';
|
4 |
|
5 | const SEPARATOR_REGEX = /[\\]+/g;
|
6 |
|
7 | export function normalizeSeparators(filePath: FilePath): FilePath {
|
8 | return filePath.replace(SEPARATOR_REGEX, '/');
|
9 | }
|
10 |
|
11 | export type PathOptions = {
|
12 | noLeadingDotSlash?: boolean,
|
13 | ...
|
14 | };
|
15 |
|
16 | export 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 |
|
27 | export function relativePath(
|
28 | from: string,
|
29 | to: string,
|
30 | leadingDotSlash: boolean = true,
|
31 | ) {
|
32 | return normalizePath(path.relative(from, to), leadingDotSlash);
|
33 | }
|