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: experiment with a decorator to hide commands from the help screen #574

Merged
merged 7 commits into from
Aug 20, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions __tests__/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ Options
Related commands

$ rdme validate Validate your OpenAPI/Swagger definition.
$ rdme swagger Alias for \`rdme openapi\`. [deprecated]
"
`;

Expand All @@ -50,7 +49,6 @@ Options
Related commands

$ rdme validate Validate your OpenAPI/Swagger definition.
$ rdme swagger Alias for \`rdme openapi\`. [deprecated]
"
`;

Expand All @@ -77,7 +75,6 @@ Options
Related commands

$ rdme validate Validate your OpenAPI/Swagger definition.
$ rdme swagger Alias for \`rdme openapi\`. [deprecated]
"
`;

Expand Down
2 changes: 2 additions & 0 deletions src/cmds/swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import type { CommandOptions } from '../lib/baseCommand';
import type { Options } from './openapi';

import Command from '../lib/baseCommand';
import isHidden from '../lib/decorators/isHidden';

import OpenAPICommand from './openapi';

@isHidden()
export default class SwaggerCommand extends OpenAPICommand {
constructor() {
super();
Expand Down
5 changes: 5 additions & 0 deletions src/lib/baseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ export default class Command {
*/
position: number;

/**
* Should the command be hidden from our `--help` screens?
*/
hidden: boolean;

/**
* Arguments to hide from the individual command help screen
* (typically used for hiding default arguments)
Expand Down
4 changes: 3 additions & 1 deletion src/lib/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export function getCategories(): Record<
name: string;
description: string;
position: number;
hidden: boolean;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we really do have so many stupid little moving parts in these command helper functions. thank god for TS

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

honestly blown away by how much the rewrite has been helping us this past week

}[];
}
> {
Expand Down Expand Up @@ -74,6 +75,7 @@ export function listByCategory() {
name: c.command.command,
description: c.command.description,
position: c.command.position,
hidden: c.command.hidden,
});
});

Expand All @@ -82,5 +84,5 @@ export function listByCategory() {

export function getSimilar(cmdCategory: CommandCategories, excludeCommand: string) {
const categories = listByCategory();
return categories[cmdCategory].commands.filter(cmd => cmd.name !== excludeCommand);
return categories[cmdCategory].commands.filter(cmd => cmd.name !== excludeCommand && !cmd.hidden);
}
7 changes: 7 additions & 0 deletions src/lib/decorators/isHidden.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function hideFromHelpScreen() {
// eslint-disable-next-line @typescript-eslint/ban-types
return function (constructor: Function) {
// eslint-disable-next-line no-param-reassign
constructor.prototype.hidden = true;
};
}
3 changes: 2 additions & 1 deletion src/lib/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import config from 'config';

import * as commands from './commands';

function formatCommands(cmds: { name: string; description: string; position: number }[]) {
function formatCommands(cmds: { name: string; description: string; hidden: boolean; position: number }[]) {
return cmds
.sort((a, b) => (a.position > b.position ? 1 : -1))
.filter(command => !command.hidden)
.map(command => {
return {
name: `${chalk.grey('$')} ${config.get('cli')} ${command.name}`,
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"baseUrl": "./src",
"downlevelIteration": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"lib": ["es2020"],
"noImplicitAny": true,
"outDir": "dist/",
Expand Down