1 |
|
2 |
|
3 | import type {FilePath} from '@parcel/types';
|
4 |
|
5 | const path = require('path');
|
6 |
|
7 | export default function getRootDir(files: Array<FilePath>): FilePath {
|
8 | let cur = null;
|
9 |
|
10 | for (let file of files) {
|
11 | let parsed = path.parse(file);
|
12 | if (!cur) {
|
13 | cur = parsed;
|
14 | } else if (parsed.root !== cur.root) {
|
15 |
|
16 |
|
17 | return process.cwd();
|
18 | } else {
|
19 |
|
20 | let curParts = cur.dir.split(path.sep);
|
21 | let newParts = parsed.dir.split(path.sep);
|
22 | let len = Math.min(curParts.length, newParts.length);
|
23 | let i = 0;
|
24 | while (i < len && curParts[i] === newParts[i]) {
|
25 | i++;
|
26 | }
|
27 |
|
28 | cur.dir = i > 1 ? curParts.slice(0, i).join(path.sep) : cur.root;
|
29 | }
|
30 | }
|
31 |
|
32 | return cur ? cur.dir : process.cwd();
|
33 | }
|