Skip to content

Commit ff2c50c

Browse files
MaxKlessFrozenPandaz
authored andcommitted
fix(core): sort --help output by flag priority (#14869)
(cherry picked from commit 20b66fc)
1 parent 385934c commit ff2c50c

File tree

2 files changed

+110
-2
lines changed

2 files changed

+110
-2
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { Schema } from './params';
2+
import { logger } from './logger';
3+
import { printHelp } from './print-help';
4+
5+
describe('printHelp', () => {
6+
it('should sort options by priority and name', () => {
7+
const schema: Schema = {
8+
properties: {
9+
aImp: {
10+
'x-priority': 'important',
11+
},
12+
bImp: {
13+
'x-priority': 'important',
14+
},
15+
aNormal: {},
16+
bNormal: {},
17+
aDep: {
18+
'x-deprecated': 'great reason',
19+
},
20+
bDep: {
21+
'x-deprecated': 'even better reason',
22+
},
23+
aReq: {},
24+
bReq: {},
25+
aInt: {
26+
'x-priority': 'internal',
27+
},
28+
bInt: {
29+
'x-priority': 'internal',
30+
},
31+
},
32+
required: ['aReq', 'bReq'],
33+
description: 'description',
34+
};
35+
36+
let output = '';
37+
jest.spyOn(logger, 'info').mockImplementation((x) => (output = x));
38+
39+
printHelp('nx g @nrwl/demo:example', schema, {
40+
mode: 'generate',
41+
plugin: '@nrwl/demo',
42+
entity: 'example',
43+
aliases: ['ex'],
44+
});
45+
46+
const flagsFromOutput = output
47+
.match(/--[a|b]\S*/g)
48+
.map((x) => x.replace('--', ''));
49+
50+
expect(flagsFromOutput).toMatchInlineSnapshot(`
51+
Array [
52+
"aReq",
53+
"bReq",
54+
"aImp",
55+
"bImp",
56+
"aNormal",
57+
"bNormal",
58+
"aInt",
59+
"bInt",
60+
"aDep",
61+
"bDep",
62+
]
63+
`);
64+
});
65+
});

packages/nx/src/utils/print-help.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,10 @@ function generateOptionsOutput(schema: Schema): string {
214214
}
215215
>();
216216
let requiredSpaceToRenderAllFlagsAndAliases = 0;
217-
218-
for (const [optionName, optionConfig] of Object.entries(schema.properties)) {
217+
const sorted = Object.entries(schema.properties).sort((a, b) =>
218+
compareByPriority(a, b, schema)
219+
);
220+
for (const [optionName, optionConfig] of sorted) {
219221
const renderedFlagAndAlias =
220222
`--${optionName}` +
221223
(optionConfig.alias ? `, -${optionConfig.alias}` : '');
@@ -336,3 +338,44 @@ function generateLinkOutput({
336338
'Find more information and examples at:'
337339
)} ${chalk.bold(link)}`;
338340
}
341+
342+
/**
343+
* sorts properties in the following order
344+
* - required
345+
* - x-priority: important
346+
* - everything else
347+
* - x-priority: internal
348+
* - deprecated
349+
* if two properties have equal priority, they are sorted by name
350+
*/
351+
function compareByPriority(
352+
a: [string, Schema['properties'][0]],
353+
b: [string, Schema['properties'][0]],
354+
schema: Schema
355+
): number {
356+
function getPriority([name, property]: [
357+
string,
358+
Schema['properties'][0]
359+
]): number {
360+
if (schema.required?.includes(name)) {
361+
return 0;
362+
}
363+
if (property['x-priority'] === 'important') {
364+
return 1;
365+
}
366+
if (property['x-deprecated']) {
367+
return 4;
368+
}
369+
if (property['x-priority'] === 'internal') {
370+
return 3;
371+
}
372+
return 2;
373+
}
374+
375+
const aPriority = getPriority(a);
376+
const bPriority = getPriority(b);
377+
if (aPriority === bPriority) {
378+
return a[0].localeCompare(b[0]);
379+
}
380+
return aPriority - bPriority;
381+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy