/
project-graph-incremental-recomputation.ts
183 lines (168 loc) · 5.47 KB
/
project-graph-incremental-recomputation.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { FileData, ProjectFileMap } from '@nrwl/devkit';
import { appRootPath } from '@nrwl/tao/src/utils/app-root';
import { performance } from 'perf_hooks';
import {
createProjectFileMap,
readWorkspaceJson,
updateProjectFileMap,
} from '../../../file-utils';
import { defaultFileHasher } from '../../../hasher/file-hasher';
import { getGitHashForFiles } from '../../../hasher/git-hasher';
import { serverLogger } from './logger';
import { buildProjectGraphUsingProjectFileMap } from '../../build-project-graph';
import { workspaceConfigName } from '@nrwl/tao/src/shared/workspace';
import {
nxDepsPath,
ProjectGraphCache,
readCache,
} from '../../../nx-deps/nx-deps-cache';
import { fileExists } from '../../../../utilities/fileutils';
const configName = workspaceConfigName(appRootPath);
let cachedSerializedProjectGraphPromise: Promise<{
error: Error | null;
serializedProjectGraph: string | null;
}>;
let projectFileMapWithFiles:
| { projectFileMap: ProjectFileMap; allWorkspaceFiles: FileData[] }
| undefined;
let currentProjectGraphCache: ProjectGraphCache | undefined;
const collectedUpdatedFiles = new Set<string>();
const collectedDeletedFiles = new Set<string>();
let waitPeriod = 100;
let scheduledTimeoutId;
export function getCachedSerializedProjectGraphPromise() {
// recomputing it now on demand. we can ignore the scheduled timeout
if (scheduledTimeoutId) {
clearTimeout(scheduledTimeoutId);
scheduledTimeoutId = undefined;
}
// reset the wait time
waitPeriod = 100;
resetInternalStateIfNxDepsMissing();
if (collectedUpdatedFiles.size == 0 && collectedDeletedFiles.size == 0) {
if (!cachedSerializedProjectGraphPromise) {
processCollectedUpdatedAndDeletedFiles(); // this creates a project graph
cachedSerializedProjectGraphPromise = createAndSerializeProjectGraph();
}
} else {
processCollectedUpdatedAndDeletedFiles();
cachedSerializedProjectGraphPromise = createAndSerializeProjectGraph();
}
return cachedSerializedProjectGraphPromise;
}
export function addUpdatedAndDeletedFiles(
updatedFiles: string[],
deletedFiles: string[]
) {
for (let f of updatedFiles) {
collectedDeletedFiles.delete(f);
collectedUpdatedFiles.add(f);
}
for (let f of deletedFiles) {
collectedUpdatedFiles.delete(f);
collectedDeletedFiles.add(f);
}
if (!scheduledTimeoutId) {
scheduledTimeoutId = setTimeout(() => {
scheduledTimeoutId = undefined;
if (waitPeriod < 4000) {
waitPeriod = waitPeriod * 2;
}
processCollectedUpdatedAndDeletedFiles();
cachedSerializedProjectGraphPromise = createAndSerializeProjectGraph();
}, waitPeriod);
}
}
function processCollectedUpdatedAndDeletedFiles() {
performance.mark('hash-watched-changes-start');
const updatedFiles = getGitHashForFiles(
[...collectedUpdatedFiles.values()],
appRootPath
);
const deletedFiles = [...collectedDeletedFiles.values()];
performance.mark('hash-watched-changes-end');
performance.measure(
'hash changed files from watcher',
'hash-watched-changes-start',
'hash-watched-changes-end'
);
defaultFileHasher.incrementalUpdate(updatedFiles, deletedFiles);
const workspaceJson = readWorkspaceJson();
serverLogger.nestedLog(
`Updated file-hasher based on watched changes, recomputing project graph...`
);
// when workspace.json changes we cannot be sure about the correctness of the project file map
if (
collectedUpdatedFiles.has(configName) ||
collectedDeletedFiles.has(configName)
) {
projectFileMapWithFiles = createProjectFileMap(workspaceJson);
} else {
projectFileMapWithFiles = projectFileMapWithFiles
? updateProjectFileMap(
workspaceJson,
projectFileMapWithFiles.projectFileMap,
projectFileMapWithFiles.allWorkspaceFiles,
updatedFiles,
deletedFiles
)
: createProjectFileMap(workspaceJson);
}
collectedUpdatedFiles.clear();
collectedDeletedFiles.clear();
}
async function createAndSerializeProjectGraph() {
try {
performance.mark('create-project-graph-start');
const workspaceJson = readWorkspaceJson();
const { projectGraph, projectGraphCache } =
await buildProjectGraphUsingProjectFileMap(
workspaceJson,
projectFileMapWithFiles.projectFileMap,
projectFileMapWithFiles.allWorkspaceFiles,
currentProjectGraphCache || readCache(),
true
);
currentProjectGraphCache = projectGraphCache;
performance.mark('create-project-graph-end');
performance.measure(
'total execution time for createProjectGraph()',
'create-project-graph-start',
'create-project-graph-end'
);
performance.mark('json-stringify-start');
const serializedProjectGraph = JSON.stringify(projectGraph);
performance.mark('json-stringify-end');
performance.measure(
'serialize graph',
'json-stringify-start',
'json-stringify-end'
);
return {
error: null,
serializedProjectGraph,
};
} catch (err) {
return {
error: err,
serializedProjectGraph: null,
};
}
}
export function resetInternalState() {
cachedSerializedProjectGraphPromise = undefined;
projectFileMapWithFiles = undefined;
currentProjectGraphCache = undefined;
collectedUpdatedFiles.clear();
collectedDeletedFiles.clear();
waitPeriod = 100;
}
function resetInternalStateIfNxDepsMissing() {
try {
if (!fileExists(nxDepsPath)) {
resetInternalState();
}
} catch (e) {
resetInternalState();
}
}