Skip to content

Commit 14cf48c

Browse files
committed
refactor(utils): extract shared Nx project graph utilities
1 parent 14da494 commit 14cf48c

8 files changed

Lines changed: 51 additions & 63 deletions

File tree

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

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,11 @@ import {
1212
logger,
1313
pluralize,
1414
pluralizeToken,
15-
stringifyError,
15+
resolveCachedProjectGraph,
1616
} from '@code-pushup/utils';
1717
import type { CoverageResult } from '../config.js';
1818
import { formatMetaLog } from '../format.js';
1919

20-
/**
21-
* Resolves the cached project graph for the current Nx workspace.
22-
* First tries to read cache and if not possible, go for the async creation.
23-
*/
24-
async function resolveCachedProjectGraph() {
25-
const { readCachedProjectGraph, createProjectGraphAsync } = await import(
26-
'@nx/devkit'
27-
);
28-
try {
29-
return readCachedProjectGraph();
30-
} catch (error) {
31-
logger.warn(
32-
`Could not read cached project graph, falling back to async creation - ${stringifyError(error)}`,
33-
);
34-
return await createProjectGraphAsync({ exitOnError: false });
35-
}
36-
}
37-
3820
/**
3921
* @param targets nx targets to be used for measuring coverage, test by default
4022
* @returns An array of coverage result information for the coverage plugin.

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

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,13 @@
1-
import { logger, pluralizeToken, stringifyError } from '@code-pushup/utils';
1+
import {
2+
logger,
3+
pluralizeToken,
4+
resolveCachedProjectGraph,
5+
} from '@code-pushup/utils';
26
import type { ESLintTarget } from '../config.js';
37
import { formatMetaLog } from '../meta/format.js';
48
import { filterProjectGraph } from './filter-project-graph.js';
59
import { nxProjectsToConfig } from './projects-to-config.js';
610

7-
/**
8-
* Resolves the cached project graph for the current Nx workspace.
9-
* First tries to read cache and if not possible, go for the async creation.
10-
*/
11-
async function resolveCachedProjectGraph() {
12-
const { readCachedProjectGraph, createProjectGraphAsync } = await import(
13-
'@nx/devkit'
14-
);
15-
try {
16-
return readCachedProjectGraph();
17-
} catch (error) {
18-
logger.warn(
19-
`Could not read cached project graph, falling back to async creation.\n${stringifyError(error)}`,
20-
);
21-
return await createProjectGraphAsync({ exitOnError: false });
22-
}
23-
}
24-
2511
/**
2612
* Finds all Nx projects in workspace and converts their lint configurations to Code PushUp ESLint plugin parameters.
2713
*

packages/plugin-eslint/src/lib/nx/find-project-with-deps.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { logger, pluralizeToken } from '@code-pushup/utils';
1+
import { createProjectGraph, logger, pluralizeToken } from '@code-pushup/utils';
22
import type { ESLintTarget } from '../config.js';
33
import { formatMetaLog } from '../meta/format.js';
44
import { nxProjectsToConfig } from './projects-to-config.js';
@@ -32,8 +32,7 @@ import { findAllDependencies } from './traverse-graph.js';
3232
export async function eslintConfigFromNxProjectAndDeps(
3333
projectName: string,
3434
): Promise<ESLintTarget[]> {
35-
const { createProjectGraphAsync } = await import('@nx/devkit');
36-
const projectGraph = await createProjectGraphAsync({ exitOnError: false });
35+
const projectGraph = await createProjectGraph();
3736

3837
const dependencies = findAllDependencies(projectName, projectGraph);
3938

packages/plugin-eslint/src/lib/nx/find-project-without-deps.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { logger } from '@code-pushup/utils';
1+
import { createProjectGraph, logger } from '@code-pushup/utils';
22
import type { ESLintTarget } from '../config.js';
33
import { formatMetaLog } from '../meta/format.js';
44
import { nxProjectsToConfig } from './projects-to-config.js';
@@ -31,8 +31,7 @@ import { nxProjectsToConfig } from './projects-to-config.js';
3131
export async function eslintConfigFromNxProject(
3232
projectName: string,
3333
): Promise<ESLintTarget> {
34-
const { createProjectGraphAsync } = await import('@nx/devkit');
35-
const projectGraph = await createProjectGraphAsync({ exitOnError: false });
34+
const projectGraph = await createProjectGraph();
3635

3736
const [project] = await nxProjectsToConfig(
3837
projectGraph,

packages/plugin-typescript/src/lib/nx/tsconfig-paths.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import type { ProjectConfiguration } from '@nx/devkit';
22
import { readdir } from 'node:fs/promises';
33
import path from 'node:path';
44
import { readConfigFile, sys } from 'typescript';
5-
import { logger, pluralizeToken, stringifyError } from '@code-pushup/utils';
5+
import {
6+
logger,
7+
pluralizeToken,
8+
resolveCachedProjectGraph,
9+
} from '@code-pushup/utils';
610
import { formatMetaLog } from '../format.js';
711

812
/**
@@ -35,24 +39,6 @@ function hasFilesToCompile(tsconfigPath: string): boolean {
3539
return !(filesEmpty && includeEmpty);
3640
}
3741

38-
/**
39-
* Resolves the cached project graph for the current Nx workspace.
40-
* Tries to read from cache first, falls back to async creation.
41-
*/
42-
async function resolveCachedProjectGraph() {
43-
const { readCachedProjectGraph, createProjectGraphAsync } = await import(
44-
'@nx/devkit'
45-
);
46-
try {
47-
return readCachedProjectGraph();
48-
} catch (error) {
49-
logger.warn(
50-
`Could not read cached project graph, falling back to async creation.\n${stringifyError(error)}`,
51-
);
52-
return await createProjectGraphAsync({ exitOnError: false });
53-
}
54-
}
55-
5642
function isProjectIncluded(
5743
project: ProjectConfiguration,
5844
exclude?: string[],

packages/utils/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@
3939
"wrap-ansi": "^9.0.2",
4040
"zod": "^4.2.1"
4141
},
42+
"peerDependencies": {
43+
"@nx/devkit": ">=17.0.0"
44+
},
45+
"peerDependenciesMeta": {
46+
"@nx/devkit": {
47+
"optional": true
48+
}
49+
},
4250
"files": [
4351
"src",
4452
"!**/*.tsbuildinfo"

packages/utils/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export {
9494
export { interpolate } from './lib/interpolate.js';
9595
export { Logger, logger } from './lib/logger.js';
9696
export { mergeConfigs } from './lib/merge-configs.js';
97+
export { createProjectGraph, resolveCachedProjectGraph } from './lib/nx.js';
9798
export {
9899
addIndex,
99100
expandAuditsForUrls,

packages/utils/src/lib/nx.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { ProjectGraph } from '@nx/devkit';
2+
import { stringifyError } from './errors.js';
3+
import { logger } from './logger.js';
4+
5+
/**
6+
* Creates the project graph for the current Nx workspace.
7+
*/
8+
export async function createProjectGraph(): Promise<ProjectGraph> {
9+
const { createProjectGraphAsync } = await import('@nx/devkit');
10+
return createProjectGraphAsync({ exitOnError: false });
11+
}
12+
13+
/**
14+
* Resolves the cached project graph for the current Nx workspace.
15+
* Tries to read from cache first, falls back to async creation.
16+
*/
17+
export async function resolveCachedProjectGraph(): Promise<ProjectGraph> {
18+
const { readCachedProjectGraph } = await import('@nx/devkit');
19+
try {
20+
return readCachedProjectGraph();
21+
} catch (error) {
22+
logger.warn(
23+
`Could not read cached project graph, falling back to async creation.\n${stringifyError(error)}`,
24+
);
25+
return createProjectGraph();
26+
}
27+
}

0 commit comments

Comments
 (0)