1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 |
|
10 | var path = require('path');
|
11 | var parent = require('glob-parent');
|
12 | var isGlob = require('is-glob');
|
13 |
|
14 | module.exports = function globBase(pattern) {
|
15 | if (typeof pattern !== 'string') {
|
16 | throw new TypeError('glob-base expects a string.');
|
17 | }
|
18 |
|
19 | var res = {};
|
20 | res.base = parent(pattern);
|
21 | res.isGlob = isGlob(pattern);
|
22 |
|
23 | if (res.base !== '.') {
|
24 | res.glob = pattern.substr(res.base.length);
|
25 | if (res.glob.charAt(0) === '/') {
|
26 | res.glob = res.glob.substr(1);
|
27 | }
|
28 | } else {
|
29 | res.glob = pattern;
|
30 | }
|
31 |
|
32 | if (!res.isGlob) {
|
33 | res.base = dirname(pattern);
|
34 | res.glob = res.base !== '.'
|
35 | ? pattern.substr(res.base.length)
|
36 | : pattern;
|
37 | }
|
38 |
|
39 | if (res.glob.substr(0, 2) === './') {
|
40 | res.glob = res.glob.substr(2);
|
41 | }
|
42 | if (res.glob.charAt(0) === '/') {
|
43 | res.glob = res.glob.substr(1);
|
44 | }
|
45 | return res;
|
46 | };
|
47 |
|
48 | function dirname(glob) {
|
49 | if (glob.slice(-1) === '/') return glob;
|
50 | return path.dirname(glob);
|
51 | }
|