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 all 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
47 changes: 44 additions & 3 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 @@ -96,11 +97,42 @@ const getArguments = (schema = webpackSchema) => {
*/
const getDescription = path => {
for (const { schema } of path) {
if (schema.cli && schema.cli.helper) continue;
if (schema.cli) {
if (schema.cli.helper) continue;
if (schema.cli.description) return schema.cli.description;
}
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) {
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;
}
}
};

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

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

if (!flags[name]) {
flags[name] = {
configs: [],
Expand Down
44 changes: 44 additions & 0 deletions test/Cli.basictest.js
Expand Up @@ -5,6 +5,50 @@ 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-reset-description": {
type: "array",
items: {
type: "string"
},
description: "original description",
cli: {
resetDescription: "custom reset"
}
},
"with-cli-description": {
type: "string",
description: "original description",
cli: {
description: "description for CLI option"
}
},
"with-negative-description": {
type: "boolean",
description: "original description",
cli: {
negatedDescription: "custom negative description"
}
},
"with-both-cli-and-negative-description": {
type: "boolean",
description: "original description",
cli: {
description: "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
72 changes: 72 additions & 0 deletions test/__snapshots__/Cli.basictest.js.snap
Expand Up @@ -8931,3 +8931,75 @@ 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",
},
"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",
},
}
`;
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