Skip to content

Commit

Permalink
feat(core): add tasks to json output
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed May 12, 2023
1 parent 61ab95b commit fc9f76a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 8 deletions.
5 changes: 3 additions & 2 deletions e2e/nx-run/src/affected-graph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 --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', () => {
Expand Down
77 changes: 71 additions & 6 deletions packages/nx/src/command-line/graph/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,12 @@ export async function generateGraph(
},
affectedProjects: string[]
): Promise<void> {
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: [
Expand Down Expand Up @@ -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(
createJsonResponse(graph, args.projects, args.targets),
null,
2
)
);
process.exit(0);
}

Expand Down Expand Up @@ -300,10 +311,20 @@ export async function generateGraph(
} else if (ext === '.json') {
ensureDirSync(dirname(fullFilePath));

writeJsonFile(fullFilePath, {
graph,
affectedProjects,
criticalPath: affectedProjects,
const json = createJsonResponse(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({
Expand Down Expand Up @@ -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 createJsonResponse(
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;
}
3 changes: 3 additions & 0 deletions packages/nx/src/utils/command-line-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']];
Expand Down

0 comments on commit fc9f76a

Please sign in to comment.