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