Skip to content

Commit 8eff766

Browse files
dasco144AgentEnder
authored andcommitted
feat(core): add an option to seperate the output of show with provide… (#23172)
Co-authored-by: Craigory Coppola <[email protected]> (cherry picked from commit d9a9712)
1 parent c18d98a commit 8eff766

File tree

6 files changed

+484
-85
lines changed

6 files changed

+484
-85
lines changed

docs/generated/cli/show.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ Type: `string`
145145

146146
Show only projects that match a given pattern.
147147

148+
##### sep
149+
150+
Type: `string`
151+
152+
Outputs projects with the specified seperator
153+
148154
##### type
149155

150156
Type: `string`

docs/generated/packages/nx/documents/show.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ Type: `string`
145145

146146
Show only projects that match a given pattern.
147147

148+
##### sep
149+
150+
Type: `string`
151+
152+
Outputs projects with the specified seperator
153+
148154
##### type
149155

150156
Type: `string`

packages/nx/src/command-line/show/command-object.ts

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@ export interface NxShowArgs {
1212
}
1313

1414
export type ShowProjectsOptions = NxShowArgs & {
15-
exclude: string;
16-
files: string;
17-
uncommitted: any;
18-
untracked: any;
19-
base: string;
20-
head: string;
21-
affected: boolean;
22-
type: ProjectGraphProjectNode['type'];
23-
projects: string[];
24-
withTarget: string[];
25-
verbose: boolean;
15+
exclude?: string[];
16+
files?: string;
17+
uncommitted?: any;
18+
untracked?: any;
19+
base?: string;
20+
head?: string;
21+
affected?: boolean;
22+
type?: ProjectGraphProjectNode['type'];
23+
projects?: string[];
24+
withTarget?: string[];
25+
verbose?: boolean;
26+
sep?: string;
2627
};
2728

2829
export type ShowProjectOptions = NxShowArgs & {
@@ -90,11 +91,17 @@ const showProjectsCommand: CommandModule<NxShowArgs, ShowProjectsOptions> = {
9091
description: 'Select only projects of the given type',
9192
choices: ['app', 'lib', 'e2e'],
9293
})
94+
.option('sep', {
95+
type: 'string',
96+
description: 'Outputs projects with the specified seperator',
97+
})
9398
.implies('untracked', 'affected')
9499
.implies('uncommitted', 'affected')
95100
.implies('files', 'affected')
96101
.implies('base', 'affected')
97102
.implies('head', 'affected')
103+
.conflicts('sep', 'json')
104+
.conflicts('json', 'sep')
98105
.example(
99106
'$0 show projects --projects "apps/*"',
100107
'Show all projects in the apps directory'
@@ -119,7 +126,9 @@ const showProjectsCommand: CommandModule<NxShowArgs, ShowProjectsOptions> = {
119126
return handleErrors(
120127
args.verbose ?? process.env.NX_VERBOSE_LOGGING === 'true',
121128
async () => {
122-
return (await import('./show')).showProjectsHandler(args);
129+
const { showProjectsHandler } = await import('./projects');
130+
await showProjectsHandler(args);
131+
process.exit(0);
123132
}
124133
);
125134
},
@@ -145,21 +154,23 @@ const showProjectCommand: CommandModule<NxShowArgs, ShowProjectOptions> = {
145154
description:
146155
'Prints additional information about the commands (e.g., stack traces)',
147156
})
148-
.check((argv) => {
149-
if (argv.web) {
150-
argv.json = false;
151-
}
152-
return true;
153-
})
157+
.conflicts('json', 'web')
158+
.conflicts('web', 'json')
154159
.example(
155160
'$0 show project my-app',
156161
'View project information for my-app in JSON format'
162+
)
163+
.example(
164+
'$0 show project my-app --web',
165+
'View project information for my-app in the browser'
157166
),
158167
handler: (args) => {
159168
return handleErrors(
160169
args.verbose ?? process.env.NX_VERBOSE_LOGGING === 'true',
161170
async () => {
162-
return (await import('./show')).showProjectHandler(args);
171+
const { showProjectHandler } = await import('./project');
172+
await showProjectHandler(args);
173+
process.exit(0);
163174
}
164175
);
165176
},
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { output } from '../../utils/output';
2+
import { createProjectGraphAsync } from '../../project-graph/project-graph';
3+
import { ShowProjectOptions } from './command-object';
4+
import { generateGraph } from '../graph/graph';
5+
6+
export async function showProjectHandler(
7+
args: ShowProjectOptions
8+
): Promise<void> {
9+
const graph = await createProjectGraphAsync();
10+
const node = graph.nodes[args.projectName];
11+
if (!node) {
12+
console.log(`Could not find project ${args.projectName}`);
13+
process.exit(1);
14+
}
15+
if (args.json) {
16+
console.log(JSON.stringify(node.data));
17+
} else if (args.web) {
18+
await generateGraph(
19+
{
20+
view: 'project-details',
21+
focus: node.name,
22+
watch: true,
23+
open: true,
24+
},
25+
[]
26+
);
27+
} else {
28+
const chalk = require('chalk') as typeof import('chalk');
29+
const logIfExists = (label, key: keyof typeof node['data']) => {
30+
if (node.data[key]) {
31+
console.log(`${chalk.bold(label)}: ${node.data[key]}`);
32+
}
33+
};
34+
35+
logIfExists('Name', 'name');
36+
logIfExists('Root', 'root');
37+
logIfExists('Source Root', 'sourceRoot');
38+
logIfExists('Tags', 'tags');
39+
logIfExists('Implicit Dependencies', 'implicitDependencies');
40+
41+
const targets = Object.entries(node.data.targets ?? {});
42+
const maxTargetNameLength = Math.max(...targets.map(([t]) => t.length));
43+
const maxExecutorNameLength = Math.max(
44+
...targets.map(([, t]) => t?.executor?.length ?? 0)
45+
);
46+
47+
if (targets.length > 0) {
48+
console.log(`${chalk.bold('Targets')}: `);
49+
for (const [target, targetConfig] of targets) {
50+
console.log(
51+
`- ${chalk.bold((target + ':').padEnd(maxTargetNameLength + 2))} ${(
52+
targetConfig?.executor ?? ''
53+
).padEnd(maxExecutorNameLength + 2)} ${(() => {
54+
const configurations = Object.keys(
55+
targetConfig.configurations ?? {}
56+
);
57+
if (configurations.length) {
58+
return chalk.dim(configurations.join(', '));
59+
}
60+
return '';
61+
})()}`
62+
);
63+
}
64+
}
65+
}
66+
await output.drain();
67+
}

0 commit comments

Comments
 (0)