UNPKG

1.32 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = getRootDir;
7
8var _glob = require("./glob");
9
10const path = require('path');
11
12function 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 // bail out. there is no common root.
23 // this can happen on windows, e.g. C:\foo\bar vs. D:\foo\bar
24 return process.cwd();
25 } else {
26 // find the common path parts.
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} // Transforms a path like `packages/*/src/index.js` to the root of the glob, `packages/`
42
43
44function 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