Skip to content

Commit 951dac6

Browse files
authored
Merge pull request #14953 from snitin315/feat/cli-description
feat: allow specific description for CLI options
2 parents c344abb + 8c6077c commit 951dac6

4 files changed

Lines changed: 161 additions & 3 deletions

File tree

lib/cli.js

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const webpackSchema = require("../schemas/WebpackOptions.json");
3737
/**
3838
* @typedef {Object} ArgumentConfig
3939
* @property {string} description
40+
* @property {string} [negatedDescription]
4041
* @property {string} path
4142
* @property {boolean} multiple
4243
* @property {"enum"|"string"|"path"|"number"|"boolean"|"RegExp"|"reset"} type
@@ -96,11 +97,42 @@ const getArguments = (schema = webpackSchema) => {
9697
*/
9798
const getDescription = path => {
9899
for (const { schema } of path) {
99-
if (schema.cli && schema.cli.helper) continue;
100+
if (schema.cli) {
101+
if (schema.cli.helper) continue;
102+
if (schema.cli.description) return schema.cli.description;
103+
}
100104
if (schema.description) return schema.description;
101105
}
102106
};
103107

108+
/**
109+
*
110+
* @param {PathItem[]} path path in the schema
111+
* @returns {string | undefined} negative description
112+
*/
113+
const getNegatedDescription = path => {
114+
for (const { schema } of path) {
115+
if (schema.cli) {
116+
if (schema.cli.helper) continue;
117+
if (schema.cli.negatedDescription) return schema.cli.negatedDescription;
118+
}
119+
}
120+
};
121+
122+
/**
123+
*
124+
* @param {PathItem[]} path path in the schema
125+
* @returns {string | undefined} reset description
126+
*/
127+
const getResetDescription = path => {
128+
for (const { schema } of path) {
129+
if (schema.cli) {
130+
if (schema.cli.helper) continue;
131+
if (schema.cli.resetDescription) return schema.cli.resetDescription;
132+
}
133+
}
134+
};
135+
104136
/**
105137
*
106138
* @param {any} schemaPart schema
@@ -142,13 +174,17 @@ const getArguments = (schema = webpackSchema) => {
142174
const addResetFlag = path => {
143175
const schemaPath = path[0].path;
144176
const name = pathToArgumentName(`${schemaPath}.reset`);
145-
const description = getDescription(path);
177+
const description =
178+
getResetDescription(path) ||
179+
`Clear all items provided in '${schemaPath}' configuration. ${getDescription(
180+
path
181+
)}`;
146182
flags[name] = {
147183
configs: [
148184
{
149185
type: "reset",
150186
multiple: false,
151-
description: `Clear all items provided in '${schemaPath}' configuration. ${description}`,
187+
description,
152188
path: schemaPath
153189
}
154190
],
@@ -167,6 +203,7 @@ const getArguments = (schema = webpackSchema) => {
167203
const argConfigBase = schemaToArgumentConfig(path[0].schema);
168204
if (!argConfigBase) return 0;
169205

206+
const negatedDescription = getNegatedDescription(path);
170207
const name = pathToArgumentName(path[0].path);
171208
/** @type {ArgumentConfig} */
172209
const argConfig = {
@@ -176,6 +213,10 @@ const getArguments = (schema = webpackSchema) => {
176213
path: path[0].path
177214
};
178215

216+
if (negatedDescription) {
217+
argConfig.negatedDescription = negatedDescription;
218+
}
219+
179220
if (!flags[name]) {
180221
flags[name] = {
181222
configs: [],

test/Cli.basictest.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,50 @@ describe("Cli", () => {
55
expect(getArguments()).toMatchSnapshot();
66
});
77

8+
it("should generate the correct cli flags with custom schema", () => {
9+
const schema = {
10+
title: "custom CLI options",
11+
type: "object",
12+
additionalProperties: false,
13+
properties: {
14+
"with-reset-description": {
15+
type: "array",
16+
items: {
17+
type: "string"
18+
},
19+
description: "original description",
20+
cli: {
21+
resetDescription: "custom reset"
22+
}
23+
},
24+
"with-cli-description": {
25+
type: "string",
26+
description: "original description",
27+
cli: {
28+
description: "description for CLI option"
29+
}
30+
},
31+
"with-negative-description": {
32+
type: "boolean",
33+
description: "original description",
34+
cli: {
35+
negatedDescription: "custom negative description"
36+
}
37+
},
38+
"with-both-cli-and-negative-description": {
39+
type: "boolean",
40+
description: "original description",
41+
cli: {
42+
description: "description for CLI option",
43+
negatedDescription: "custom negative description"
44+
}
45+
}
46+
}
47+
};
48+
49+
expect(getArguments(schema)).toMatchSnapshot();
50+
});
51+
852
const test = (name, values, config, fn) => {
953
it(`should correctly process arguments for ${name}`, () => {
1054
const args = getArguments();

test/__snapshots__/Cli.basictest.js.snap

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8973,3 +8973,75 @@ Object {
89738973
},
89748974
}
89758975
`;
8976+
8977+
exports[`Cli should generate the correct cli flags with custom schema 1`] = `
8978+
Object {
8979+
"with-both-cli-and-negative-description": Object {
8980+
"configs": Array [
8981+
Object {
8982+
"description": "description for CLI option",
8983+
"multiple": false,
8984+
"negatedDescription": "custom negative description",
8985+
"path": "with-both-cli-and-negative-description",
8986+
"type": "boolean",
8987+
},
8988+
],
8989+
"description": "description for CLI option",
8990+
"multiple": false,
8991+
"simpleType": "boolean",
8992+
},
8993+
"with-cli-description": Object {
8994+
"configs": Array [
8995+
Object {
8996+
"description": "description for CLI option",
8997+
"multiple": false,
8998+
"path": "with-cli-description",
8999+
"type": "string",
9000+
},
9001+
],
9002+
"description": "description for CLI option",
9003+
"multiple": false,
9004+
"simpleType": "string",
9005+
},
9006+
"with-negative-description": Object {
9007+
"configs": Array [
9008+
Object {
9009+
"description": "original description",
9010+
"multiple": false,
9011+
"negatedDescription": "custom negative description",
9012+
"path": "with-negative-description",
9013+
"type": "boolean",
9014+
},
9015+
],
9016+
"description": "original description",
9017+
"multiple": false,
9018+
"simpleType": "boolean",
9019+
},
9020+
"with-reset-description": Object {
9021+
"configs": Array [
9022+
Object {
9023+
"description": "original description",
9024+
"multiple": true,
9025+
"path": "with-reset-description[]",
9026+
"type": "string",
9027+
},
9028+
],
9029+
"description": "original description",
9030+
"multiple": true,
9031+
"simpleType": "string",
9032+
},
9033+
"with-reset-description-reset": Object {
9034+
"configs": Array [
9035+
Object {
9036+
"description": "custom reset",
9037+
"multiple": false,
9038+
"path": "with-reset-description",
9039+
"type": "reset",
9040+
},
9041+
],
9042+
"description": "custom reset",
9043+
"multiple": false,
9044+
"simpleType": "boolean",
9045+
},
9046+
}
9047+
`;

types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ declare interface Argument {
215215
}
216216
declare interface ArgumentConfig {
217217
description: string;
218+
negatedDescription?: string;
218219
path: string;
219220
multiple: boolean;
220221
type: "string" | "number" | "boolean" | "path" | "enum" | "RegExp" | "reset";

0 commit comments

Comments
 (0)