1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = getRootDir;
|
7 |
|
8 | var _glob = require("./glob");
|
9 |
|
10 | const path = require('path');
|
11 |
|
12 | function getRootDir(files) {
|
13 | let cur = null;
|
14 |
|
15 | for (let file of files) {
|
16 | let parsed = path.parse(file);
|
17 | parsed.dir = findGlobRoot(parsed.dir);
|
18 |
|
19 | if (!cur) {
|
20 | cur = parsed;
|
21 | } else if (parsed.root !== cur.root) {
|
22 |
|
23 |
|
24 | return process.cwd();
|
25 | } else {
|
26 |
|
27 | let curParts = cur.dir.split(path.sep);
|
28 | let newParts = parsed.dir.split(path.sep);
|
29 | let len = Math.min(curParts.length, newParts.length);
|
30 | let i = 0;
|
31 |
|
32 | while (i < len && curParts[i] === newParts[i]) {
|
33 | i++;
|
34 | }
|
35 |
|
36 | cur.dir = i > 1 ? curParts.slice(0, i).join(path.sep) : cur.root;
|
37 | }
|
38 | }
|
39 |
|
40 | return cur ? cur.dir : process.cwd();
|
41 | }
|
42 |
|
43 |
|
44 | function findGlobRoot(dir) {
|
45 | let parts = dir.split(path.sep);
|
46 | let last = parts.length;
|
47 |
|
48 | for (let i = parts.length - 1; i >= 0; i--) {
|
49 | if ((0, _glob.isGlob)(parts[i])) {
|
50 | last = i;
|
51 | }
|
52 | }
|
53 |
|
54 | return parts.slice(0, last).join(path.sep);
|
55 | } |
\ | No newline at end of file |