UNPKG

896 BJavaScriptView Raw
1// @flow strict-local
2
3import type {FilePath} from '@parcel/types';
4
5const path = require('path');
6
7export 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 // bail out. there is no common root.
16 // this can happen on windows, e.g. C:\foo\bar vs. D:\foo\bar
17 return process.cwd();
18 } else {
19 // find the common path parts.
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}