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 12, 2023
1 parent 16b6b23 commit 61ab95b
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 29 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
6 changes: 6 additions & 0 deletions packages/nx/src/command-line/yargs-utils/shared-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ export function withRunOptions(yargs: Argv): Argv {
describe:
'Show the task graph of the command. Pass a file path to save the graph data instead of viewing it in the browser.',
default: false,
coerce: (value) =>
value === 'true' || value === true
? true
: value === 'false' || value === false
? false
: value,
})
.option('verbose', {
type: 'boolean',
Expand Down
17 changes: 6 additions & 11 deletions packages/nx/src/utils/command-line-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,6 @@ export function splitArgsIntoNxArgsAndOverrides(
delete (nxArgs as any).$0;
delete (nxArgs as any).__overrides_unparsed__;

if (!(nxArgs.graph === null || nxArgs.graph === undefined)) {
nxArgs.graph =
nxArgs.graph === 'true' || nxArgs.graph === true
? true
: nxArgs.graph === 'false' || nxArgs.graph === false
? false
: nxArgs.graph;
}

if (mode === 'run-many') {
const args = nxArgs as any;
if (!args.projects) {
Expand Down Expand Up @@ -297,8 +288,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 +336,9 @@ export function getProjectRoots(
): string[] {
return projectNames.map((name) => nodes[name].data.root);
}

export function readGraphFileFromGraphArg({ graph }: NxArgs) {
return typeof graph === 'string' && graph !== 'true' && graph !== ''
? graph
: undefined;
}

0 comments on commit 61ab95b

Please sign in to comment.