From 37bb72acd93e2a4cafb48a27377b34e9388b6b98 Mon Sep 17 00:00:00 2001 From: AgentEnder Date: Fri, 12 May 2023 11:33:28 -0400 Subject: [PATCH] feat(core): add tasks to json output --- e2e/nx-run/src/affected-graph.test.ts | 5 +- packages/nx/src/command-line/graph/graph.ts | 77 +++++++++++++++++++-- packages/nx/src/utils/command-line-utils.ts | 3 + 3 files changed, 77 insertions(+), 8 deletions(-) diff --git a/e2e/nx-run/src/affected-graph.test.ts b/e2e/nx-run/src/affected-graph.test.ts index 260af42c12239d..798a7e29a7f76d 100644 --- a/e2e/nx-run/src/affected-graph.test.ts +++ b/e2e/nx-run/src/affected-graph.test.ts @@ -504,10 +504,11 @@ describe('Nx Affected and Graph Tests', () => { }); it('graph should output valid json when stdout is specified', () => { - const result = runCLI(`graph --out stdout`); + const result = runCLI(`affected -t build --graph stdout`); let model; expect(() => (model = JSON.parse(result))).not.toThrow(); - expect(model).toHaveProperty('nodes'); + expect(model).toHaveProperty('graph'); + expect(model).toHaveProperty('tasks'); }); it('affected:graph should include affected projects in environment file', () => { diff --git a/packages/nx/src/command-line/graph/graph.ts b/packages/nx/src/command-line/graph/graph.ts index d8744626dae50f..352fdbbbc1564f 100644 --- a/packages/nx/src/command-line/graph/graph.ts +++ b/packages/nx/src/command-line/graph/graph.ts @@ -187,7 +187,12 @@ export async function generateGraph( }, affectedProjects: string[] ): Promise { - if (Array.isArray(args.targets) && args.targets.length > 1) { + if ( + Array.isArray(args.targets) && + args.targets.length > 1 && + args.file && + !(args.file === 'stdout' || args.file.endsWith('.json')) + ) { output.warn({ title: 'Showing Multiple Targets is not supported yet', bodyLines: [ @@ -248,7 +253,13 @@ export async function generateGraph( if (args.file) { // stdout is a magical constant that doesn't actually write a file if (args.file === 'stdout') { - console.log(JSON.stringify(graph, null, 2)); + console.log( + JSON.stringify( + createJsonOutput(graph, args.projects, args.targets), + null, + 2 + ) + ); process.exit(0); } @@ -300,10 +311,20 @@ export async function generateGraph( } else if (ext === '.json') { ensureDirSync(dirname(fullFilePath)); - writeJsonFile(fullFilePath, { - graph, - affectedProjects, - criticalPath: affectedProjects, + const json = createJsonOutput(graph, args.projects, args.targets); + json.affectedProjects = affectedProjects; + json.criticalPath = affectedProjects; + + writeJsonFile(fullFilePath, json); + + output.warn({ + title: 'JSON output contains deprecated fields:', + bodyLines: [ + '- affectedProjects', + '- criticalPath', + '', + 'These fields will be removed in Nx 18. If you need to see which projects were affected, use `nx show projects --affected`.', + ], }); output.success({ @@ -691,3 +712,47 @@ function createTaskId( return `${projectId}:${targetId}`; } } + +interface GraphJsonResponse { + tasks?: TaskGraph; + graph: ProjectGraph; + + /** + * @deprecated To see affected projects, use `nx show projects --affected`. This will be removed in Nx 18. + */ + affectedProjects?: string[]; + + /** + * @deprecated To see affected projects, use `nx show projects --affected`. This will be removed in Nx 18. + */ + criticalPath?: string[]; +} + +function createJsonOutput( + graph: ProjectGraph, + projects: string[], + targets?: string[] +): GraphJsonResponse { + const response: GraphJsonResponse = { + graph, + }; + + if (targets?.length) { + const nxJson = readNxJson(); + + const defaultDependencyConfigs = mapTargetDefaultsToDependencies( + nxJson.targetDefaults + ); + + response.tasks = createTaskGraph( + graph, + defaultDependencyConfigs, + projects, + targets, + undefined, + {} + ); + } + + return response; +} diff --git a/packages/nx/src/utils/command-line-utils.ts b/packages/nx/src/utils/command-line-utils.ts index f45c9f72648c9d..5d947276d10bdc 100644 --- a/packages/nx/src/utils/command-line-utils.ts +++ b/packages/nx/src/utils/command-line-utils.ts @@ -46,6 +46,9 @@ export function splitArgsIntoNxArgsAndOverrides( nxArgs: NxArgs; overrides: Arguments & { __overrides_unparsed__: string[] }; } { + // If args.graph is set to stdout, we can't print other things. + options.printWarnings &&= !(args.graph === 'stdout'); + // this is to lerna case when this function is invoked imperatively if (args['target'] && !args['targets']) { args['targets'] = [args['target']];