Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow specific description for CLI options #14953

Merged
merged 5 commits into from Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/cli.js
Expand Up @@ -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
Expand Down Expand Up @@ -97,10 +98,23 @@ 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;
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
if (schema.description) return schema.description;
}
};

/**
*
* @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;
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
}
};

/**
*
* @param {any} schemaPart schema
Expand Down Expand Up @@ -167,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 = {
Expand All @@ -176,6 +191,10 @@ const getArguments = (schema = webpackSchema) => {
path: path[0].path
};

if (negatedDescription) {
argConfig.negatedDescription = negatedDescription;
}

if (!flags[name]) {
flags[name] = {
configs: [],
Expand Down
28 changes: 28 additions & 0 deletions test/Cli.basictest.js
Expand Up @@ -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();
Expand Down
46 changes: 46 additions & 0 deletions test/__snapshots__/Cli.basictest.js.snap
Expand Up @@ -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",
},
}
`;
1 change: 1 addition & 0 deletions types.d.ts
Expand Up @@ -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";
Expand Down