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