Skip to content

Commit aa69f37

Browse files
committed
fix(create-cli): resolve nx devkit from target workspace
1 parent 60cc9a2 commit aa69f37

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

packages/create-cli/src/lib/setup/monorepo.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { select } from '@inquirer/prompts';
2+
import { createRequire } from 'node:module';
23
import path from 'node:path';
34
import {
45
MONOREPO_TOOL_DETECTORS,
@@ -8,9 +9,9 @@ import {
89
hasScript,
910
listPackages,
1011
listWorkspaces,
11-
loadNxProjectGraph,
1212
logger,
1313
readPnpmWorkspacePatterns,
14+
stringifyError,
1415
toUnixPath,
1516
} from '@code-pushup/utils';
1617
import type {
@@ -83,7 +84,15 @@ export async function listProjects(
8384
}
8485

8586
async function listNxProjects(cwd: string): Promise<WizardProject[]> {
86-
const graph = await loadNxProjectGraph();
87+
const require = createRequire(path.join(cwd, 'package.json'));
88+
const { readCachedProjectGraph, createProjectGraphAsync } =
89+
require('@nx/devkit') as typeof import('@nx/devkit');
90+
91+
const graph = await loadProjectGraph(
92+
readCachedProjectGraph,
93+
createProjectGraphAsync,
94+
);
95+
8796
return Object.values(graph.nodes).map(({ name, data }) => ({
8897
name,
8998
directory: path.join(cwd, data.root),
@@ -133,7 +142,7 @@ async function addNxTarget(
133142
return false;
134143
}
135144
const config = JSON.parse(raw);
136-
if (config.targets[TASK_NAME] != null) {
145+
if (config.targets?.[TASK_NAME] != null) {
137146
return true;
138147
}
139148
const updated = {
@@ -180,3 +189,17 @@ function toProject(cwd: string, pkg: WorkspacePackage): WizardProject {
180189
relativeDir: toUnixPath(path.relative(cwd, pkg.directory)),
181190
};
182191
}
192+
193+
async function loadProjectGraph(
194+
readCached: typeof import('@nx/devkit').readCachedProjectGraph,
195+
createAsync: typeof import('@nx/devkit').createProjectGraphAsync,
196+
) {
197+
try {
198+
return readCached();
199+
} catch (error) {
200+
logger.warn(
201+
`Could not read cached project graph, falling back to async creation.\n${stringifyError(error)}`,
202+
);
203+
return createAsync({ exitOnError: false });
204+
}
205+
}

0 commit comments

Comments
 (0)