|
1 | | -import { |
2 | | - CreateNodes, |
3 | | - joinPathFragments, |
4 | | - ProjectConfiguration, |
5 | | - readJsonFile, |
6 | | -} from '@nx/devkit'; |
7 | | -import { dirname, join } from 'node:path'; |
8 | | -import { |
9 | | - JestPluginOptions, |
10 | | - createNodes as createJestNodes, |
11 | | -} from '@nx/jest/plugin'; |
12 | 1 | import { readdirSync } from 'fs'; |
| 2 | +import { dirname, join } from 'path'; |
13 | 3 |
|
14 | | -export const createNodes: CreateNodes< |
15 | | - JestPluginOptions & { skipProjects: string[] } |
16 | | -> = [ |
17 | | - createJestNodes[0], |
18 | | - async (configFilePath, options, context) => { |
19 | | - const projectRoot = dirname(configFilePath); |
| 4 | +import { CreateNodesV2, joinPathFragments, ProjectConfiguration, readJsonFile } from '@nx/devkit'; |
| 5 | +import { JestPluginOptions, createNodesV2 as createJestNodesV2 } from '@nx/jest/plugin'; |
20 | 6 |
|
21 | | - const siblingFiles = readdirSync(join(context.workspaceRoot, projectRoot)); |
22 | | - if (!siblingFiles.includes('project.json')) { |
23 | | - return {}; |
24 | | - } |
| 7 | +export const createNodesV2: CreateNodesV2<JestPluginOptions & { skipProjects: string[] }> = [ |
| 8 | + createJestNodesV2[0], |
| 9 | + async (allConfigFiles, options, context) => { |
| 10 | + const configFiles: string[] = []; |
| 11 | + const errors: Array<[file: string, error: Error]> = []; |
| 12 | + |
| 13 | + await Promise.all( |
| 14 | + allConfigFiles.map(async (file) => { |
| 15 | + try { |
| 16 | + const projectRoot = dirname(file); |
| 17 | + const siblingFiles = readdirSync(join(context.workspaceRoot, projectRoot)); |
| 18 | + |
| 19 | + if (siblingFiles.includes('project.json')) { |
| 20 | + const path = joinPathFragments(projectRoot, 'project.json'); |
| 21 | + const projectJson = readJsonFile<ProjectConfiguration>(path); |
| 22 | + const projectName = projectJson.name ?? ''; |
25 | 23 |
|
26 | | - const path = joinPathFragments(projectRoot, 'project.json'); |
27 | | - const projectJson = readJsonFile<ProjectConfiguration>(path); |
28 | | - const projectName = projectJson.name; |
| 24 | + if (!options?.skipProjects.includes(projectName)) { |
| 25 | + configFiles.push(file); |
| 26 | + } |
| 27 | + } |
| 28 | + } catch (e) { |
| 29 | + errors.push([file, e] as const); |
| 30 | + } |
| 31 | + }) |
| 32 | + ); |
29 | 33 |
|
30 | | - if (projectName && options?.skipProjects.includes(projectName)) { |
31 | | - return {}; |
| 34 | + if (errors.length > 0) { |
| 35 | + throw new Error( |
| 36 | + `Failed to read the following configuration files:\n |
| 37 | + ${errors.map(([file, error]) => `${file}: ${error.message}`).join('\n')}` |
| 38 | + ); |
32 | 39 | } |
33 | 40 |
|
34 | | - return createJestNodes[1](configFilePath, options, context); |
| 41 | + // Exclude by projectRoot |
| 42 | + // |
| 43 | + // const nodesResult = await createNodesFromFiles(createJestNodesV2[1], files, options, context); |
| 44 | + |
| 45 | + // nodesResult.map((nodeResult, index) => { |
| 46 | + // if (nodeResult[1]?.projects !== undefined) { |
| 47 | + // const projects: Record<string, Optional<ProjectConfiguration, 'root'>> = {}; |
| 48 | + |
| 49 | + // Object.keys(nodeResult[1].projects).forEach(function (key) { |
| 50 | + // const project = nodeResult[1].projects ? nodeResult[1].projects[key] : undefined; |
| 51 | + |
| 52 | + // if (project !== undefined && !options?.skipProjects.includes(project.name ?? '')) { |
| 53 | + // projects[key] = project; |
| 54 | + // } |
| 55 | + // }); |
| 56 | + |
| 57 | + // nodesResult[index][1].projects = projects; |
| 58 | + // } |
| 59 | + // }); |
| 60 | + |
| 61 | + // return nodesResult; |
| 62 | + |
| 63 | + return await createJestNodesV2[1](configFiles, options, context); |
35 | 64 | }, |
36 | 65 | ]; |
0 commit comments