fix(core): implement missing --print flag for nx graph command (#31406)

## Current Behavior

The `nx graph --print` flag is documented and shows in CLI help, but
when used, it opens the graph UI in a browser instead of printing the
dependency graph to the console.

## Expected Behavior

With this PR, `nx graph --print` correctly prints the dependency graph
JSON to stdout in the terminal and exits, matching the documented
behavior and CLI help description.

## Related Issue(s)

Fixes #30255

---------

Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Co-authored-by: FrozenPandaz <FrozenPandaz@users.noreply.github.com>
This commit is contained in:
Jason Jean 2025-06-02 17:18:51 -04:00 committed by GitHub
parent 7990930cd0
commit fc99ded082
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -240,6 +240,7 @@ function filterGraph(
export async function generateGraph(
args: {
file?: string;
print?: boolean;
host?: string;
port?: number;
groupByFolder?: boolean;
@ -353,7 +354,7 @@ export async function generateGraph(
splitArgsIntoNxArgsAndOverrides(
args,
'affected',
{ printWarnings: args.file !== 'stdout' },
{ printWarnings: !args.print && args.file !== 'stdout' },
readNxJson()
).nxArgs,
rawGraph
@ -390,25 +391,24 @@ export async function generateGraph(
args.exclude || []
);
if (args.file) {
// stdout is a magical constant that doesn't actually write a file
if (args.file === 'stdout') {
console.log(
JSON.stringify(
await createJsonOutput(
prunedGraph,
rawGraph,
args.projects,
args.targets
),
null,
2
)
);
await output.drain();
process.exit(0);
}
if (args.print || args.file === 'stdout') {
console.log(
JSON.stringify(
await createJsonOutput(
prunedGraph,
rawGraph,
args.projects,
args.targets
),
null,
2
)
);
await output.drain();
process.exit(0);
}
if (args.file) {
const workspaceFolder = workspaceRoot;
const ext = extname(args.file);
const fullFilePath = isAbsolute(args.file)