|
1 | 1 | import {
|
2 | 2 | CreateDependenciesContext,
|
| 3 | + CreateNodes, |
3 | 4 | CreateNodesContext,
|
| 5 | + CreateNodesContextV2, |
| 6 | + createNodesFromFiles, |
4 | 7 | CreateNodesResult,
|
| 8 | + CreateNodesResultV2, |
| 9 | + CreateNodesV2, |
5 | 10 | DependencyType,
|
6 | 11 | joinPathFragments,
|
7 | 12 | logger,
|
8 | 13 | ProjectConfiguration,
|
9 |
| - ProjectGraph, |
10 |
| - ProjectGraphBuilder, |
11 |
| - ProjectGraphProcessorContext, |
12 | 14 | ProjectType,
|
13 | 15 | RawProjectGraphDependency,
|
14 | 16 | TargetConfiguration,
|
@@ -116,95 +118,58 @@ function getProjectFilesGlob(projectFiles: string[]): string {
|
116 | 118 | : `**/${projectFiles[0]}`;
|
117 | 119 | }
|
118 | 120 |
|
119 |
| -// Project Graph V1 |
120 |
| - |
121 |
| -export function getProjectGraph( |
122 |
| - pluginName: string, |
123 |
| - projectFilter: (project: { root: string }) => boolean, |
124 |
| - getPackageInfo: (project: { root: string }) => PackageInfo, |
125 |
| - graph: ProjectGraph, |
126 |
| - ctx: ProjectGraphProcessorContext |
127 |
| -): ProjectGraph { |
128 |
| - const builder = new ProjectGraphBuilder(graph); |
129 |
| - if (process.env['NX_VERBOSE_LOGGING'] === 'true') { |
130 |
| - logger.debug( |
131 |
| - `[${pluginName}]: Looking related projects inside the workspace...` |
132 |
| - ); |
133 |
| - } |
134 |
| - let workspace = undefined; |
135 |
| - let dependencies: RawProjectGraphDependency[] = []; |
136 |
| - |
137 |
| - for (const source in ctx.filesToProcess) { |
138 |
| - const changed = ctx.filesToProcess[source]; |
139 |
| - for (const file of changed) { |
140 |
| - // we only create the workspace map once and only if changed file is of interest |
141 |
| - workspace ??= getPackageInfosForNxProjects( |
142 |
| - pluginName, |
143 |
| - projectFilter, |
144 |
| - getPackageInfo, |
145 |
| - ctx.projectsConfigurations |
146 |
| - ); |
147 |
| - |
148 |
| - dependencies = dependencies.concat( |
149 |
| - getDependenciesForProject(pluginName, file.file, source, workspace) |
150 |
| - ); |
151 |
| - } |
152 |
| - } |
153 |
| - |
154 |
| - for (const dep of dependencies) { |
155 |
| - builder.addDependency(dep.source, dep.target, dep.type, dep.source); |
156 |
| - } |
157 |
| - |
158 |
| - return builder.getUpdatedProjectGraph(); |
159 |
| -} |
160 |
| - |
161 |
| -// Project Graph V2 |
162 |
| - |
| 121 | +// Project Graph using CreateNode (V1) |
163 | 122 | export const createNodesFor = <T = unknown>(
|
164 | 123 | projectFiles: string[],
|
165 | 124 | projectFilter: (project: { root: string }) => boolean,
|
166 | 125 | getProjectTypeAndTargets: (
|
167 | 126 | projectFile: string,
|
168 |
| - options?: T |
| 127 | + options?: T | undefined |
169 | 128 | ) => {
|
170 | 129 | projectType: ProjectType;
|
171 | 130 | targets: {
|
172 | 131 | [targetName: string]: TargetConfiguration;
|
173 | 132 | };
|
174 | 133 | },
|
175 | 134 | pluginName: string
|
176 |
| -) => |
177 |
| - [ |
178 |
| - getProjectFilesGlob(projectFiles), |
179 |
| - ( |
180 |
| - file: string, |
181 |
| - options: T, |
182 |
| - context: CreateNodesContext |
183 |
| - ): CreateNodesResult => { |
184 |
| - if (!projectFilter({ root: getProjectRootFromFile(file) })) { |
185 |
| - return {}; // back off if the file/project does not match the criteria |
186 |
| - } |
| 135 | +): CreateNodes<T> => [ |
| 136 | + getProjectFilesGlob(projectFiles), |
| 137 | + createNodesInternal<T>(projectFilter, getProjectTypeAndTargets, pluginName), |
| 138 | +]; |
187 | 139 |
|
188 |
| - const root = dirname(file); |
189 |
| - |
190 |
| - // eslint-disable-next-line no-useless-escape -- eslint's wrong |
191 |
| - const parts = root.split(/[\/\\]/g); |
192 |
| - const name = parts[parts.length - 1].toLowerCase(); |
193 |
| - |
194 |
| - return { |
195 |
| - projects: { |
196 |
| - [name]: { |
197 |
| - name, |
198 |
| - root, |
199 |
| - ...(isNxCrystalEnabled() |
200 |
| - ? getProjectTypeAndTargets(file, options) |
201 |
| - : {}), |
202 |
| - tags: [`type:${pluginName.replace('@nxrocks/', '')}`], |
203 |
| - }, |
204 |
| - }, |
205 |
| - }; |
206 |
| - }, |
207 |
| - ] as const; |
| 140 | +// Project Graph using CreateNode (V2) |
| 141 | +export const createNodesForV2 = <T = unknown>( |
| 142 | + projectFiles: string[], |
| 143 | + projectFilter: (project: { root: string }) => boolean, |
| 144 | + getProjectTypeAndTargets: ( |
| 145 | + projectFile: string, |
| 146 | + options: T | undefined |
| 147 | + ) => { |
| 148 | + projectType: ProjectType; |
| 149 | + targets: { |
| 150 | + [targetName: string]: TargetConfiguration; |
| 151 | + }; |
| 152 | + }, |
| 153 | + pluginName: string |
| 154 | +): CreateNodesV2<T> => [ |
| 155 | + getProjectFilesGlob(projectFiles), |
| 156 | + ( |
| 157 | + files: readonly string[], |
| 158 | + options: T | undefined, |
| 159 | + context: CreateNodesContextV2 |
| 160 | + ) => { |
| 161 | + return createNodesFromFiles<T>( |
| 162 | + createNodesInternal<T>( |
| 163 | + projectFilter, |
| 164 | + getProjectTypeAndTargets, |
| 165 | + pluginName |
| 166 | + ), |
| 167 | + files, |
| 168 | + options as T, |
| 169 | + context |
| 170 | + ); |
| 171 | + }, |
| 172 | +]; |
208 | 173 |
|
209 | 174 | export const createDependenciesIf = (
|
210 | 175 | pluginName: string,
|
@@ -246,3 +211,46 @@ export const createDependenciesIf = (
|
246 | 211 |
|
247 | 212 | return dependencies;
|
248 | 213 | };
|
| 214 | + |
| 215 | +function createNodesInternal<T = unknown>( |
| 216 | + projectFilter: (project: { root: string }) => boolean, |
| 217 | + getProjectTypeAndTargets: ( |
| 218 | + projectFile: string, |
| 219 | + options: T | undefined |
| 220 | + ) => { |
| 221 | + projectType: ProjectType; |
| 222 | + targets: { |
| 223 | + [targetName: string]: TargetConfiguration; |
| 224 | + }; |
| 225 | + }, |
| 226 | + pluginName: string |
| 227 | +) { |
| 228 | + return ( |
| 229 | + file: string, |
| 230 | + options: T | undefined, |
| 231 | + context: CreateNodesContext |
| 232 | + ): CreateNodesResult => { |
| 233 | + if (!projectFilter({ root: getProjectRootFromFile(file) })) { |
| 234 | + return {}; // back off if the file/project does not match the criteria |
| 235 | + } |
| 236 | + |
| 237 | + const root = dirname(file); |
| 238 | + |
| 239 | + // eslint-disable-next-line no-useless-escape -- eslint's wrong |
| 240 | + const parts = root.split(/[\/\\]/g); |
| 241 | + const name = parts[parts.length - 1].toLowerCase(); |
| 242 | + |
| 243 | + return { |
| 244 | + projects: { |
| 245 | + [name]: { |
| 246 | + name, |
| 247 | + root, |
| 248 | + ...(isNxCrystalEnabled() |
| 249 | + ? getProjectTypeAndTargets(file, options) |
| 250 | + : {}), |
| 251 | + tags: [`type:${pluginName.replace('@nxrocks/', '')}`], |
| 252 | + }, |
| 253 | + }, |
| 254 | + }; |
| 255 | + }; |
| 256 | +} |
0 commit comments