/
util.ts
57 lines (48 loc) · 1.32 KB
/
util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// nx-ignore-next-line
import { ProjectGraphDependency } from '@nrwl/devkit';
export function trimBackSlash(value: string): string {
return value.replace(/\/$/, '');
}
export function parseParentDirectoriesFromPilePath(
path: string,
workspaceRoot: string
) {
const root = trimBackSlash(path);
// split the source root on directory separator
const split: string[] = root.split('/');
// check the first part for libs or apps, depending on workspaceLayout
if (split[0] === trimBackSlash(workspaceRoot)) {
split.shift();
}
// pop off the last element, which should be the lib name
split.pop();
return split;
}
export function hasPath(
dependencies: Record<string, ProjectGraphDependency[]>,
target: string,
node: string,
visited: string[],
currentSearchDepth: number,
maxSearchDepth: number = -1 // -1 indicates unlimited search depth
) {
if (target === node) return true;
if (maxSearchDepth === -1 || currentSearchDepth <= maxSearchDepth) {
for (let d of dependencies[node] || []) {
if (visited.indexOf(d.target) > -1) continue;
visited.push(d.target);
if (
hasPath(
dependencies,
target,
d.target,
visited,
currentSearchDepth + 1,
maxSearchDepth
)
)
return true;
}
}
return false;
}