-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy pathget-pdv-data.ts
More file actions
175 lines (156 loc) · 5.12 KB
/
get-pdv-data.ts
File metadata and controls
175 lines (156 loc) · 5.12 KB
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
import { directoryExists } from '@nx-console/shared-file-system';
import { findNxPackagePath } from '@nx-console/shared-npm';
import { gte } from '@nx-console/nx-version';
import { PDVData } from '@nx-console/shared-types';
import type {
ProjectConfiguration,
ProjectGraphProjectNode,
} from 'nx/src/devkit-exports';
import { dirname, join, relative } from 'path';
import { getNxCloudStatus } from './get-nx-cloud-status';
import {
getNxVersion,
nxWorkspace,
} from '@nx-console/shared-nx-workspace-info';
import { getProjectByPath } from './get-project-by-path';
import { getSourceMapFilesToProjectsMap } from './get-source-map';
import { lspLogger } from '@nx-console/language-server-utils';
import { readNxJson } from '@nx-console/shared-npm';
export async function getPDVData(
workspacePath: string,
filePath: string,
): Promise<PDVData> {
const graphBasePath = await getGraphBasePath(workspacePath);
if (!graphBasePath) {
return {
resultType: 'NO_GRAPH_ERROR',
graphBasePath: undefined,
pdvDataSerialized: undefined,
pdvDataSerializedMulti: undefined,
errorsSerialized: undefined,
errorMessage: undefined,
};
}
const nxVersion = await getNxVersion(workspacePath);
if (!gte(nxVersion, '19.8.0')) {
return {
resultType: 'OLD_NX_VERSION',
graphBasePath,
pdvDataSerialized: undefined,
pdvDataSerializedMulti: undefined,
errorsSerialized: undefined,
errorMessage: undefined,
};
}
const workspace = await nxWorkspace(workspacePath, lspLogger);
const hasProjects = Object.keys(workspace.projectGraph.nodes).length > 0;
if (!hasProjects || (workspace.errors && workspace.isPartial != true)) {
let errorMessage = '';
if (!hasProjects) {
errorMessage = 'No projects found in the workspace.';
}
return {
resultType: 'ERROR',
graphBasePath,
pdvDataSerialized: undefined,
pdvDataSerializedMulti: undefined,
errorsSerialized: JSON.stringify(workspace.errors),
errorMessage,
};
}
const relativePath = relative(workspacePath, filePath);
const sourceMapsFilesToProjectsMap =
await getSourceMapFilesToProjectsMap(workspacePath);
const projectRootsForConfigFile = sourceMapsFilesToProjectsMap[relativePath];
const nxCloudStatus = await getNxCloudStatus(workspacePath);
const disabledTaskSyncGenerators =
await getDisabledTaskSyncGenerators(workspacePath);
if (!projectRootsForConfigFile || projectRootsForConfigFile.length <= 1) {
const project = await getProjectByPath(filePath, workspacePath);
if (!isCompleteProjectConfiguration(project)) {
return {
resultType: 'ERROR',
graphBasePath,
pdvDataSerialized: undefined,
pdvDataSerializedMulti: undefined,
errorsSerialized: JSON.stringify(workspace.errors),
errorMessage: `No project found at ${filePath}`,
};
}
const projectNode = workspace.projectGraph.nodes[project.name];
return {
resultType: 'SUCCESS',
graphBasePath,
pdvDataSerialized: JSON.stringify({
project: projectNode,
sourceMap: workspace.sourceMaps?.[project.root],
errors: workspace.errors,
connectedToCloud: nxCloudStatus.isConnected,
disabledTaskSyncGenerators,
}),
pdvDataSerializedMulti: undefined,
errorsSerialized: undefined,
errorMessage: undefined,
};
} else {
const projectNodes: ProjectGraphProjectNode[] = [];
for (const project of Object.values(workspace.projectGraph.nodes)) {
if (
projectRootsForConfigFile.includes(project.data.root) &&
isCompleteProjectConfiguration(project.data)
) {
projectNodes.push(project);
}
}
const pdvDataSerializedMulti: Record<string, string> = {};
for (const project of projectNodes) {
pdvDataSerializedMulti[project.name] = JSON.stringify({
project,
sourceMap: workspace.sourceMaps?.[project.data.root],
errors: workspace.errors,
connectedToCloud: nxCloudStatus.isConnected,
disabledTaskSyncGenerators,
});
}
return {
resultType: 'SUCCESS_MULTI',
graphBasePath,
pdvDataSerializedMulti,
pdvDataSerialized: undefined,
errorsSerialized: undefined,
errorMessage: undefined,
};
}
}
async function getGraphBasePath(
workspacePath: string,
): Promise<string | undefined> {
const graphIndexPath = await findNxPackagePath(
workspacePath,
join('src', 'core', 'graph', 'index.html'),
);
if (!graphIndexPath) {
return undefined;
}
const graphBasePath = dirname(graphIndexPath);
if (await directoryExists(graphBasePath)) {
return graphBasePath;
} else {
return undefined;
}
}
function isCompleteProjectConfiguration(
project: ProjectConfiguration | undefined,
): project is ProjectConfiguration & { name: string } {
return !!project && !!project.name;
}
async function getDisabledTaskSyncGenerators(
workspacePath: string,
): Promise<string[] | undefined> {
try {
const nxJson = await readNxJson(workspacePath);
return nxJson.sync?.disabledTaskSyncGenerators;
} catch (e) {
return undefined;
}
}