Skip to content

Commit 2637c38

Browse files
committed
chore: adjust project graph creation 2
1 parent 18707a6 commit 2637c38

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

packages/plugin-coverage/src/lib/nx/coverage-paths.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@ import path from 'node:path';
1111
import { importModule, ui } from '@code-pushup/utils';
1212
import type { CoverageResult } from '../config.js';
1313

14+
/**
15+
* Resolves the cached project graph for the current Nx workspace.
16+
* First tries to read cache and if not possible, go for the async creation.
17+
*/
18+
export async function resolveCachedProjectGraph() {
19+
const { readCachedProjectGraph, createProjectGraphAsync } = await import(
20+
'@nx/devkit'
21+
);
22+
try {
23+
return readCachedProjectGraph();
24+
} catch (e) {
25+
ui().logger.warn(
26+
'Could not read cached project graph, falling back to async creation.',
27+
);
28+
return await createProjectGraphAsync({ exitOnError: false });
29+
}
30+
}
31+
1432
/**
1533
* @param targets nx targets to be used for measuring coverage, test by default
1634
* @returns An array of coverage result information for the coverage plugin.
@@ -25,8 +43,7 @@ export async function getNxCoveragePaths(
2543
);
2644
}
2745

28-
const { readCachedProjectGraph } = await import('@nx/devkit');
29-
const { nodes } = readCachedProjectGraph();
46+
const { nodes } = await resolveCachedProjectGraph();
3047

3148
const coverageResults = await Promise.all(
3249
targets.map(async target => {

packages/plugin-eslint/src/lib/nx/find-all-projects.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
1+
import { ui } from '@code-pushup/utils';
12
import type { ESLintTarget } from '../config.js';
23
import { filterProjectGraph } from './filter-project-graph.js';
34
import { nxProjectsToConfig } from './projects-to-config.js';
45

6+
/**
7+
* Resolves the cached project graph for the current Nx workspace.
8+
* First tries to read cache and if not possible, go for the async creation.
9+
*/
10+
export async function resolveCachedProjectGraph() {
11+
const { readCachedProjectGraph, createProjectGraphAsync } = await import(
12+
'@nx/devkit'
13+
);
14+
try {
15+
return readCachedProjectGraph();
16+
} catch (e) {
17+
ui().logger.warn(
18+
'Could not read cached project graph, falling back to async creation.',
19+
);
20+
return await createProjectGraphAsync({ exitOnError: false });
21+
}
22+
}
23+
524
/**
625
* Finds all Nx projects in workspace and converts their lint configurations to Code PushUp ESLint plugin parameters.
726
*
@@ -31,8 +50,7 @@ import { nxProjectsToConfig } from './projects-to-config.js';
3150
export async function eslintConfigFromAllNxProjects(
3251
options: { exclude?: string[] } = {},
3352
): Promise<ESLintTarget[]> {
34-
const { readCachedProjectGraph } = await import('@nx/devkit');
35-
const projectGraph = readCachedProjectGraph();
53+
const projectGraph = await resolveCachedProjectGraph();
3654
const filteredProjectGraph = filterProjectGraph(
3755
projectGraph,
3856
options.exclude,

0 commit comments

Comments
 (0)