-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathworkspace-context.ts
157 lines (144 loc) · 4.6 KB
/
workspace-context.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
import type { NxWorkspaceFilesExternals, WorkspaceContext } from '../native';
import { performance } from 'perf_hooks';
import { workspaceDataDirectoryForWorkspace } from './cache-directory';
import { isOnDaemon } from '../daemon/is-on-daemon';
import { daemonClient } from '../daemon/client/client';
let workspaceContext: WorkspaceContext | undefined;
export function setupWorkspaceContext(workspaceRoot: string) {
const { WorkspaceContext } =
require('../native') as typeof import('../native');
performance.mark('workspace-context');
workspaceContext = new WorkspaceContext(
workspaceRoot,
workspaceDataDirectoryForWorkspace(workspaceRoot)
);
performance.mark('workspace-context:end');
performance.measure(
'workspace context init',
'workspace-context',
'workspace-context:end'
);
}
export async function getNxWorkspaceFilesFromContext(
workspaceRoot: string,
projectRootMap: Record<string, string>
) {
if (isOnDaemon() || !daemonClient.enabled()) {
ensureContextAvailable(workspaceRoot);
return workspaceContext.getWorkspaceFiles(projectRootMap);
}
return daemonClient.getWorkspaceFiles(projectRootMap);
}
/**
* Sync method to get files matching globs from workspace context.
* NOTE: This method will create the workspace context if it doesn't exist.
* It should only be used within Nx internal in code paths that **must** be sync.
* If used in an isolated plugin thread this will cause the workspace context
* to be recreated which is slow.
*/
export function globWithWorkspaceContextSync(
workspaceRoot: string,
globs: string[],
exclude?: string[]
) {
ensureContextAvailable(workspaceRoot);
return workspaceContext.glob(globs, exclude);
}
export async function globWithWorkspaceContext(
workspaceRoot: string,
globs: string[],
exclude?: string[]
) {
if (isOnDaemon() || !daemonClient.enabled()) {
ensureContextAvailable(workspaceRoot);
return workspaceContext.glob(globs, exclude);
} else {
return daemonClient.glob(globs, exclude);
}
}
export async function hashWithWorkspaceContext(
workspaceRoot: string,
globs: string[],
exclude?: string[]
) {
if (isOnDaemon() || !daemonClient.enabled()) {
ensureContextAvailable(workspaceRoot);
return workspaceContext.hashFilesMatchingGlob(globs, exclude);
}
return daemonClient.hashGlob(globs, exclude);
}
export async function updateContextWithChangedFiles(
workspaceRoot: string,
createdFiles: string[],
updatedFiles: string[],
deletedFiles: string[]
) {
if (!daemonClient.enabled()) {
updateFilesInContext(
workspaceRoot,
[...createdFiles, ...updatedFiles],
deletedFiles
);
} else if (isOnDaemon()) {
// make sure to only import this when running on the daemon
const { addUpdatedAndDeletedFiles } = await import(
'../daemon/server/project-graph-incremental-recomputation'
);
// update files for the incremental graph recomputation on the daemon
addUpdatedAndDeletedFiles(createdFiles, updatedFiles, deletedFiles);
} else {
// daemon is enabled but we are not running on it, ask the daemon to update the context
await daemonClient.updateWorkspaceContext(
createdFiles,
updatedFiles,
deletedFiles
);
}
}
export function updateFilesInContext(
workspaceRoot: string,
updatedFiles: string[],
deletedFiles: string[]
) {
ensureContextAvailable(workspaceRoot);
return workspaceContext?.incrementalUpdate(updatedFiles, deletedFiles);
}
export async function getAllFileDataInContext(workspaceRoot: string) {
if (isOnDaemon() || !daemonClient.enabled()) {
ensureContextAvailable(workspaceRoot);
return workspaceContext.allFileData();
}
return daemonClient.getWorkspaceContextFileData();
}
export async function getFilesInDirectoryUsingContext(
workspaceRoot: string,
dir: string
) {
if (isOnDaemon() || !daemonClient.enabled()) {
ensureContextAvailable(workspaceRoot);
return workspaceContext.getFilesInDirectory(dir);
}
return daemonClient.getFilesInDirectory(dir);
}
export function updateProjectFiles(
projectRootMappings: Record<string, string>,
rustReferences: NxWorkspaceFilesExternals,
updatedFiles: Record<string, string>,
deletedFiles: string[]
) {
return workspaceContext?.updateProjectFiles(
projectRootMappings,
rustReferences.projectFiles,
rustReferences.globalFiles,
updatedFiles,
deletedFiles
);
}
function ensureContextAvailable(workspaceRoot: string) {
if (!workspaceContext || workspaceContext?.workspaceRoot !== workspaceRoot) {
setupWorkspaceContext(workspaceRoot);
}
}
export function resetWorkspaceContext() {
workspaceContext = undefined;
}