From 5c76d8d39df20abd039293429a1e33d175261df0 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sun, 12 Dec 2021 14:47:45 +0530 Subject: [PATCH 1/5] feat: allow specific description for cli options --- lib/cli.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/cli.js b/lib/cli.js index 5488a7d47ac..f8b13d961d8 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -97,6 +97,7 @@ const getArguments = (schema = webpackSchema) => { const getDescription = path => { for (const { schema } of path) { if (schema.cli && schema.cli.helper) continue; + if (schema.cliDescription) return schema.cliDescription; if (schema.description) return schema.description; } }; From b86cfd7df920a5261d31435bfa95431c51bea8e3 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sun, 12 Dec 2021 14:51:18 +0530 Subject: [PATCH 2/5] feat: add `negatedDescription` property for negative flags --- lib/cli.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/cli.js b/lib/cli.js index f8b13d961d8..984b1539a17 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -37,6 +37,7 @@ const webpackSchema = require("../schemas/WebpackOptions.json"); /** * @typedef {Object} ArgumentConfig * @property {string} description + * @property {string} [negatedDescription] * @property {string} path * @property {boolean} multiple * @property {"enum"|"string"|"path"|"number"|"boolean"|"RegExp"|"reset"} type @@ -102,6 +103,18 @@ const getArguments = (schema = webpackSchema) => { } }; + /** + * + * @param {PathItem[]} path path in the schema + * @returns {string | undefined} negative description + */ + const getNegatedDescription = path => { + for (const { schema } of path) { + if (schema.cli && schema.cli.helper) continue; + if (schema.negatedDescription) return schema.negatedDescription; + } + }; + /** * * @param {any} schemaPart schema @@ -168,6 +181,7 @@ const getArguments = (schema = webpackSchema) => { const argConfigBase = schemaToArgumentConfig(path[0].schema); if (!argConfigBase) return 0; + const negatedDescription = getNegatedDescription(path); const name = pathToArgumentName(path[0].path); /** @type {ArgumentConfig} */ const argConfig = { @@ -177,6 +191,10 @@ const getArguments = (schema = webpackSchema) => { path: path[0].path }; + if (negatedDescription) { + argConfig.negatedDescription = negatedDescription; + } + if (!flags[name]) { flags[name] = { configs: [], From 34471196f3bad8a4118a2ae31f6cf61eac425b15 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sun, 12 Dec 2021 14:56:38 +0530 Subject: [PATCH 3/5] chore: update types --- types.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/types.d.ts b/types.d.ts index 05d308f1e3c..a9d9b4d2d46 100644 --- a/types.d.ts +++ b/types.d.ts @@ -215,6 +215,7 @@ declare interface Argument { } declare interface ArgumentConfig { description: string; + negatedDescription?: string; path: string; multiple: boolean; type: "string" | "number" | "boolean" | "path" | "enum" | "RegExp" | "reset"; From ddb0c74da8576e697194076e9144d58b9d850269 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sun, 12 Dec 2021 15:06:44 +0530 Subject: [PATCH 4/5] test: add more tests for CLI --- test/Cli.basictest.js | 28 +++++++++++++++ test/__snapshots__/Cli.basictest.js.snap | 46 ++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/test/Cli.basictest.js b/test/Cli.basictest.js index 7a82fd1bc76..0223db56c62 100644 --- a/test/Cli.basictest.js +++ b/test/Cli.basictest.js @@ -5,6 +5,34 @@ describe("Cli", () => { expect(getArguments()).toMatchSnapshot(); }); + it("should generate the correct cli flags with custom schema", () => { + const schema = { + title: "custom CLI options", + type: "object", + additionalProperties: false, + properties: { + "with-cli-description": { + type: "string", + description: "original description", + cliDescription: "description for CLI option" + }, + "with-negative-description": { + type: "boolean", + description: "original description", + negatedDescription: "custom negative description" + }, + "with-both-cli-and-negative-description": { + type: "boolean", + description: "original description", + cliDescription: "description for CLI option", + negatedDescription: "custom negative description" + } + } + }; + + expect(getArguments(schema)).toMatchSnapshot(); + }); + const test = (name, values, config, fn) => { it(`should correctly process arguments for ${name}`, () => { const args = getArguments(); diff --git a/test/__snapshots__/Cli.basictest.js.snap b/test/__snapshots__/Cli.basictest.js.snap index 7dfe3d84ef0..bde140f6fb6 100644 --- a/test/__snapshots__/Cli.basictest.js.snap +++ b/test/__snapshots__/Cli.basictest.js.snap @@ -8931,3 +8931,49 @@ Object { }, } `; + +exports[`Cli should generate the correct cli flags with custom schema 1`] = ` +Object { + "with-both-cli-and-negative-description": Object { + "configs": Array [ + Object { + "description": "description for CLI option", + "multiple": false, + "negatedDescription": "custom negative description", + "path": "with-both-cli-and-negative-description", + "type": "boolean", + }, + ], + "description": "description for CLI option", + "multiple": false, + "simpleType": "boolean", + }, + "with-cli-description": Object { + "configs": Array [ + Object { + "description": "description for CLI option", + "multiple": false, + "path": "with-cli-description", + "type": "string", + }, + ], + "description": "description for CLI option", + "multiple": false, + "simpleType": "string", + }, + "with-negative-description": Object { + "configs": Array [ + Object { + "description": "original description", + "multiple": false, + "negatedDescription": "custom negative description", + "path": "with-negative-description", + "type": "boolean", + }, + ], + "description": "original description", + "multiple": false, + "simpleType": "boolean", + }, +} +`; From 8c6077c246062ea29f8678751c2f0eaa965bea60 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Fri, 14 Jan 2022 13:19:48 +0100 Subject: [PATCH 5/5] move to cli add resetDescription --- lib/cli.js | 34 +++++++++++++++++++----- test/Cli.basictest.js | 24 ++++++++++++++--- test/__snapshots__/Cli.basictest.js.snap | 26 ++++++++++++++++++ 3 files changed, 74 insertions(+), 10 deletions(-) diff --git a/lib/cli.js b/lib/cli.js index 984b1539a17..7165b3ccc28 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -97,8 +97,10 @@ const getArguments = (schema = webpackSchema) => { */ const getDescription = path => { for (const { schema } of path) { - if (schema.cli && schema.cli.helper) continue; - if (schema.cliDescription) return schema.cliDescription; + if (schema.cli) { + if (schema.cli.helper) continue; + if (schema.cli.description) return schema.cli.description; + } if (schema.description) return schema.description; } }; @@ -110,8 +112,24 @@ const getArguments = (schema = webpackSchema) => { */ const getNegatedDescription = path => { for (const { schema } of path) { - if (schema.cli && schema.cli.helper) continue; - if (schema.negatedDescription) return schema.negatedDescription; + if (schema.cli) { + if (schema.cli.helper) continue; + if (schema.cli.negatedDescription) return schema.cli.negatedDescription; + } + } + }; + + /** + * + * @param {PathItem[]} path path in the schema + * @returns {string | undefined} reset description + */ + const getResetDescription = path => { + for (const { schema } of path) { + if (schema.cli) { + if (schema.cli.helper) continue; + if (schema.cli.resetDescription) return schema.cli.resetDescription; + } } }; @@ -156,13 +174,17 @@ const getArguments = (schema = webpackSchema) => { const addResetFlag = path => { const schemaPath = path[0].path; const name = pathToArgumentName(`${schemaPath}.reset`); - const description = getDescription(path); + const description = + getResetDescription(path) || + `Clear all items provided in '${schemaPath}' configuration. ${getDescription( + path + )}`; flags[name] = { configs: [ { type: "reset", multiple: false, - description: `Clear all items provided in '${schemaPath}' configuration. ${description}`, + description, path: schemaPath } ], diff --git a/test/Cli.basictest.js b/test/Cli.basictest.js index 0223db56c62..b3ae20301a6 100644 --- a/test/Cli.basictest.js +++ b/test/Cli.basictest.js @@ -11,21 +11,37 @@ describe("Cli", () => { type: "object", additionalProperties: false, properties: { + "with-reset-description": { + type: "array", + items: { + type: "string" + }, + description: "original description", + cli: { + resetDescription: "custom reset" + } + }, "with-cli-description": { type: "string", description: "original description", - cliDescription: "description for CLI option" + cli: { + description: "description for CLI option" + } }, "with-negative-description": { type: "boolean", description: "original description", - negatedDescription: "custom negative description" + cli: { + negatedDescription: "custom negative description" + } }, "with-both-cli-and-negative-description": { type: "boolean", description: "original description", - cliDescription: "description for CLI option", - negatedDescription: "custom negative description" + cli: { + description: "description for CLI option", + negatedDescription: "custom negative description" + } } } }; diff --git a/test/__snapshots__/Cli.basictest.js.snap b/test/__snapshots__/Cli.basictest.js.snap index bde140f6fb6..95cc00b8f27 100644 --- a/test/__snapshots__/Cli.basictest.js.snap +++ b/test/__snapshots__/Cli.basictest.js.snap @@ -8975,5 +8975,31 @@ Object { "multiple": false, "simpleType": "boolean", }, + "with-reset-description": Object { + "configs": Array [ + Object { + "description": "original description", + "multiple": true, + "path": "with-reset-description[]", + "type": "string", + }, + ], + "description": "original description", + "multiple": true, + "simpleType": "string", + }, + "with-reset-description-reset": Object { + "configs": Array [ + Object { + "description": "custom reset", + "multiple": false, + "path": "with-reset-description", + "type": "reset", + }, + ], + "description": "custom reset", + "multiple": false, + "simpleType": "boolean", + }, } `;