1
|
|
export const absolutePath = /^(?:\/|(?:[A-Za-z]:)?[\\|/])/;
|
2
|
|
export const relativePath = /^\.?\.\//;
|
3
|
|
|
4
|
|
export function isAbsolute(path: string) {
|
5
|
|
return absolutePath.test(path);
|
6
|
|
}
|
7
|
|
|
8
|
|
export function isRelative(path: string) {
|
9
|
|
return relativePath.test(path);
|
10
|
|
}
|
11
|
|
|
12
|
|
export function normalize(path: string) {
|
13
|
|
return path.replace(/\\/g, '/');
|
14
|
|
}
|
15
|
|
|
16
|
|
export function basename(path: string) {
|
17
|
|
return path.split(/(\/|\\)/).pop();
|
18
|
|
}
|
19
|
|
|
20
|
|
export function dirname(path: string) {
|
21
|
|
const match = /(\/|\\)[^/\\]*$/.exec(path);
|
22
|
|
if (!match) return '.';
|
23
|
|
|
24
|
|
const dir = path.slice(0, -match[0].length);
|
25
|
|
|
26
|
|
// If `dir` is the empty string, we're at root.
|
27
|
|
return dir ? dir : '/';
|
28
|
|
}
|
29
|
|
|
30
|
|
export function extname(path: string) {
|
31
|
|
const match = /\.[^.]+$/.exec(basename(path)!);
|
32
|
|
if (!match) return '';
|
33
|
|
return match[0];
|
34
|
|
}
|
35
|
|
|
36
|
|
export function relative(from: string, to: string) {
|
37
|
2
|
const fromParts = from.split(/[/\\]/).filter(Boolean);
|
38
|
2
|
const toParts = to.split(/[/\\]/).filter(Boolean);
|
39
|
|
|
40
|
2
|
if (fromParts[0] === '.') fromParts.shift();
|
41
|
2
|
if (toParts[0] === '.') toParts.shift();
|
42
|
|
|
43
|
2
|
while (fromParts[0] && toParts[0] && fromParts[0] === toParts[0]) {
|
44
|
2
|
fromParts.shift();
|
45
|
2
|
toParts.shift();
|
46
|
|
}
|
47
|
|
|
48
|
2
|
while (toParts[0] === '..' && fromParts.length > 0) {
|
49
|
0
|
toParts.shift();
|
50
|
0
|
fromParts.pop();
|
51
|
|
}
|
52
|
|
|
53
|
2
|
while (fromParts.pop()) {
|
54
|
2
|
toParts.unshift('..');
|
55
|
|
}
|
56
|
|
|
57
|
2
|
return toParts.join('/');
|
58
|
|
}
|
59
|
|
|
60
|
|
export function resolve(...paths: string[]) {
|
61
|
|
let resolvedParts = paths.shift()!.split(/[/\\]/);
|
62
|
|
|
63
|
|
paths.forEach(path => {
|
64
|
|
if (isAbsolute(path)) {
|
65
|
|
resolvedParts = path.split(/[/\\]/);
|
66
|
|
} else {
|
67
|
|
const parts = path.split(/[/\\]/);
|
68
|
|
|
69
|
|
while (parts[0] === '.' || parts[0] === '..') {
|
70
|
|
const part = parts.shift();
|
71
|
|
if (part === '..') {
|
72
|
|
resolvedParts.pop();
|
73
|
|
}
|
74
|
|
}
|
75
|
|
|
76
|
|
resolvedParts.push.apply(resolvedParts, parts);
|
77
|
|
}
|
78
|
|
});
|
79
|
|
|
80
|
|
return resolvedParts.join('/');
|
81
|
|
}
|