/
project-node.ts
94 lines (81 loc) · 2.09 KB
/
project-node.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// nx-ignore-next-line
import type { ProjectGraphProjectNode } from '@nrwl/devkit';
import * as cy from 'cytoscape';
import { parseParentDirectoriesFromPilePath } from '../util';
interface NodeDataDefinition extends cy.NodeDataDefinition {
id: string;
type: string;
tags: string[];
}
interface Ancestor {
id: string;
parentId: string;
label: string;
}
export class ProjectNode {
affected = false;
focused = false;
constructor(
private project: ProjectGraphProjectNode,
private workspaceRoot: string
) {}
getCytoscapeNodeDef(groupByFolder: boolean): cy.NodeDefinition {
return {
group: 'nodes',
data: this.getData(groupByFolder),
classes: this.getClasses(),
selectable: false,
grabbable: false,
pannable: true,
};
}
private getData(groupByFolder: boolean): NodeDataDefinition {
return {
id: this.project.name,
type: this.project.type,
tags: this.project.data.tags,
parent:
groupByFolder && this.project.data.hasOwnProperty('root')
? this.getParentId()
: null,
};
}
private getClasses(): string {
let classes = this.project.type ?? '';
if (this.affected) {
classes += ' affected';
}
return classes;
}
private getParentId(): string | null {
const ancestors = this.getAncestors();
if (ancestors.length > 0) {
return ancestors[ancestors.length - 1].id;
} else {
return null;
}
}
public getAncestors(): Ancestor[] {
// if there's no root, we can't figure out the parent
if (!this.project.data.root) {
return [];
}
const directories = parseParentDirectoriesFromPilePath(
this.project.data.root,
this.workspaceRoot
);
return directories.map((directory, index, allDirectories) => {
const label = [...allDirectories].slice(0, index + 1).join('/');
const id = `dir-${label}`;
const parentId =
index > 0
? `dir-${[...allDirectories].slice(0, index).join('/')}`
: null;
return {
label,
id,
parentId,
};
});
}
}