Skip to content

Commit

Permalink
feat(core): add support for --graph stdout and graph --file stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed May 10, 2023
1 parent 7921ac8 commit 9cc3ea3
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 20 deletions.
7 changes: 7 additions & 0 deletions e2e/nx-run/src/affected-graph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,13 @@ describe('Nx Affected and Graph Tests', () => {
expect(environmentJs).toContain('"affected":[]');
});

it('graph should output valid json when stdout is specified', () => {
const result = runCLI(`graph --out stdout`);
let model;
expect(() => (model = JSON.parse(result))).not.toThrow();
expect(model).toHaveProperty('nodes');
});

it('affected:graph should include affected projects in environment file', () => {
runCLI(`affected:graph --file=project-graph.html`);

Expand Down
12 changes: 6 additions & 6 deletions packages/nx/src/command-line/affected/affected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { output } from '../../utils/output';
import { generateGraph } from '../graph/graph';
import { printAffected } from './print-affected';
import { connectToNxCloudIfExplicitlyAsked } from '../connect/connect-to-nx-cloud';
import type { NxArgs } from '../../utils/command-line-utils';
import {
NxArgs,
readGraphFileFromGraphArg,
} from '../../utils/command-line-utils';
import {
parseFiles,
splitArgsIntoNxArgsAndOverrides,
Expand Down Expand Up @@ -83,11 +86,8 @@ export async function affected(
const projectsWithTarget = allProjectsWithTarget(projects, nxArgs);
if (nxArgs.graph) {
const projectNames = projectsWithTarget.map((t) => t.name);
const file =
typeof nxArgs.graph === 'string' &&
(nxArgs.graph.endsWith('.json') || nxArgs.graph.endsWith('html'))
? nxArgs.graph
: undefined;
const file = readGraphFileFromGraphArg(nxArgs);

return await generateGraph(
{
watch: false,
Expand Down
6 changes: 6 additions & 0 deletions packages/nx/src/command-line/graph/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ export async function generateGraph(
graph = filterGraph(graph, args.focus || null, 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(graph, null, 2));
process.exit(0);
}

const workspaceFolder = workspaceRoot;
const ext = extname(args.file);
const fullFilePath = isAbsolute(args.file)
Expand Down
11 changes: 5 additions & 6 deletions packages/nx/src/command-line/run-many/run-many.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { runCommand } from '../../tasks-runner/run-command';
import type { NxArgs } from '../../utils/command-line-utils';
import {
NxArgs,
readGraphFileFromGraphArg,
} from '../../utils/command-line-utils';
import { splitArgsIntoNxArgsAndOverrides } from '../../utils/command-line-utils';
import { projectHasTarget } from '../../utils/project-graph-utils';
import { connectToNxCloudIfExplicitlyAsked } from '../connect/connect-to-nx-cloud';
Expand Down Expand Up @@ -46,11 +49,7 @@ export async function runMany(
const projects = projectsToRun(nxArgs, projectGraph);

if (nxArgs.graph) {
const file =
typeof nxArgs.graph === 'string' &&
(nxArgs.graph.endsWith('.json') || nxArgs.graph.endsWith('html'))
? nxArgs.graph
: undefined;
const file = readGraphFileFromGraphArg(nxArgs);
const projectNames = projects.map((t) => t.name);
return await generateGraph(
{
Expand Down
11 changes: 5 additions & 6 deletions packages/nx/src/command-line/run/run-one.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { runCommand } from '../../tasks-runner/run-command';
import { splitArgsIntoNxArgsAndOverrides } from '../../utils/command-line-utils';
import {
readGraphFileFromGraphArg,
splitArgsIntoNxArgsAndOverrides,
} from '../../utils/command-line-utils';
import { connectToNxCloudIfExplicitlyAsked } from '../connect/connect-to-nx-cloud';
import { performance } from 'perf_hooks';
import {
Expand Down Expand Up @@ -66,11 +69,7 @@ export async function runOne(

if (nxArgs.graph) {
const projectNames = projects.map((t) => t.name);
const file =
typeof nxArgs.graph === 'string' &&
(nxArgs.graph.endsWith('.json') || nxArgs.graph.endsWith('html'))
? nxArgs.graph
: undefined;
const file = readGraphFileFromGraphArg(nxArgs);

return await generateGraph(
{
Expand Down
1 change: 1 addition & 0 deletions packages/nx/src/command-line/yargs-utils/shared-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ export function withDepGraphOptions(yargs: Argv): Argv {
describe:
'Output file (e.g. --file=output.json or --file=dep-graph.html)',
type: 'string',
alias: ['out'],
})
.option('view', {
describe: 'Choose whether to view the projects or task graph',
Expand Down
10 changes: 8 additions & 2 deletions packages/nx/src/utils/command-line-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,6 @@ function getUncommittedFiles(): string[] {
return parseGitOutput(`git diff --name-only --no-renames --relative HEAD .`);
}

``;

function getUntrackedFiles(): string[] {
return parseGitOutput(`git ls-files --others --exclude-standard`);
}
Expand Down Expand Up @@ -347,3 +345,11 @@ export function getProjectRoots(
): string[] {
return projectNames.map((name) => nodes[name].data.root);
}

export function readGraphFileFromGraphArg({ graph }: NxArgs) {
return typeof graph === 'string' &&
(graph === 'stdout' ||
['.json', '.html'].some((ext) => graph.endsWith(ext)))
? graph
: undefined;
}

0 comments on commit 9cc3ea3

Please sign in to comment.