/
mock-project-graph-service.ts
102 lines (93 loc) · 2.42 KB
/
mock-project-graph-service.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
95
96
97
98
99
100
101
102
// nx-ignore-next-line
import type {
ProjectGraphDependency,
ProjectGraphProjectNode,
} from '@nrwl/devkit';
// nx-ignore-next-line
import type { DepGraphClientResponse } from 'nx/src/command-line/dep-graph';
import { ProjectGraphService } from '../app/interfaces';
export class MockProjectGraphService implements ProjectGraphService {
private response: DepGraphClientResponse = {
hash: '79054025255fb1a26e4bc422aef54eb4',
layout: {
appsDir: 'apps',
libsDir: 'libs',
},
projects: [
{
name: 'existing-app-1',
type: 'app',
data: {
root: 'apps/app1',
tags: [],
},
},
{
name: 'existing-lib-1',
type: 'lib',
data: {
root: 'libs/lib1',
tags: [],
},
},
],
dependencies: {
'existing-app-1': [
{
source: 'existing-app-1',
target: 'existing-lib-1',
type: 'statis',
},
],
'existing-lib-1': [],
},
affected: [],
focus: null,
exclude: [],
groupByFolder: false,
};
constructor(updateFrequency: number = 5000) {
setInterval(() => this.updateResponse(), updateFrequency);
}
async getHash(): Promise<string> {
return new Promise((resolve) => resolve(this.response.hash));
}
getProjectGraph(url: string): Promise<DepGraphClientResponse> {
return new Promise((resolve) => resolve(this.response));
}
private createNewProject(): ProjectGraphProjectNode {
const type = Math.random() > 0.25 ? 'lib' : 'app';
const name = `${type}-${this.response.projects.length + 1}`;
return {
name,
type,
data: {
root: type === 'app' ? `apps/${name}` : `libs/${name}`,
tags: [],
},
};
}
private updateResponse() {
const newProject = this.createNewProject();
const libProjects = this.response.projects.filter(
(project) => project.type === 'lib'
);
const targetDependency =
libProjects[Math.floor(Math.random() * libProjects.length)];
const newDependency: ProjectGraphDependency[] = [
{
source: newProject.name,
target: targetDependency.name,
type: 'static',
},
];
this.response = {
...this.response,
projects: [...this.response.projects, newProject],
dependencies: {
...this.response.dependencies,
[newProject.name]: newDependency,
},
};
}
}