Skip to content

Commit

Permalink
feat: consolidate list-* into "list" command (wip--need tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
boneskull committed Jan 3, 2024
1 parent 62823bf commit 831ae53
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 139 deletions.
4 changes: 1 addition & 3 deletions packages/midnight-smoker/src/cli/command/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export {LintCommand} from './lint';
export {ListPluginsCommand} from './list-plugins';
export {ListReportersCommand} from './list-reporters';
export {ListRulesCommand} from './list-rules';
export {ListCommand} from './list';
export {RunScriptCommand} from './run-script';
42 changes: 0 additions & 42 deletions packages/midnight-smoker/src/cli/command/list-plugins.ts

This file was deleted.

34 changes: 0 additions & 34 deletions packages/midnight-smoker/src/cli/command/list-reporters.ts

This file was deleted.

50 changes: 0 additions & 50 deletions packages/midnight-smoker/src/cli/command/list-rules.ts

This file was deleted.

144 changes: 144 additions & 0 deletions packages/midnight-smoker/src/cli/command/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import Debug from 'debug';
import {isFunction, orderBy} from 'lodash';
import path from 'node:path';
import terminalLink from 'terminal-link';
import type {ArgumentsCamelCase, Argv} from 'yargs';
import {kComponentId} from '../../component/component';
import {isBlessedPlugin} from '../../plugin/blessed';
import {Smoker} from '../../smoker';
import type {CommonOptionTypes, GlobalOptionTypes} from '../cli-options';
import {CommonOptions} from '../cli-options';
import {createTable} from '../cli-util';
import {BaseCommand} from './base';

const debug = Debug('midnight-smoker:cli:list-rules');

export class ListCommand extends BaseCommand {
override aliases = ['ls'];
override command = 'list <component>';
override describe = 'Show available components';

override builder(argv: Argv<GlobalOptionTypes>): Argv<CommonOptionTypes> {
return argv
.positional('component', {
describe: 'Type of component to query',
choices: ['plugins', 'pkg-managers', 'reporters', 'rules'],
type: 'string',
})
.demandOption('component')
.options(CommonOptions);
}

async handler(opts: ArgumentsCamelCase<CommonOptionTypes>): Promise<void> {
switch (opts.component) {
case 'plugins':
return ListCommand.listPlugins(opts);
case 'pkg-managers':
return ListCommand.listPkgManagers(opts);
case 'reporters':
return ListCommand.listReporters(opts);
case 'rules':
return ListCommand.listRules(opts);
}
}

static async listPkgManagers(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
opts: ArgumentsCamelCase<CommonOptionTypes>,
): Promise<void> {
const pkgManagers = await Smoker.getPkgManagerDefs(opts);
debug('Found %d pkg manager modules', pkgManagers.length);
const table = createTable(
pkgManagers.map((pm) => {
const data: string[] = [pm.id, pm.bin];
if (!isFunction(pm.accepts)) {
// eslint-disable-next-line @typescript-eslint/no-base-to-string
data.push(`${pm.accepts}`);
}
return data;
}),
['Name', 'Executable', 'Accepts'],
);

console.log(`${table}`);
}

static async listReporters(
opts: ArgumentsCamelCase<CommonOptionTypes>,
): Promise<void> {
const reporters = await Smoker.getReporters(opts);
debug('Found %d reporters', reporters.length);
const table = createTable(
reporters.map((reporter) => [
reporter.name,
reporter.description,
isBlessedPlugin(reporter[kComponentId].pluginName)
? '(built-in)'
: reporter[kComponentId].pluginName,
]),
['Name', 'Description', 'Plugin'],
);

console.log(`${table}`);
}

static async listPlugins(
opts: ArgumentsCamelCase<CommonOptionTypes>,
): Promise<void> {
const plugins = await Smoker.getPlugins(opts);
debug('Found %d plugins', plugins.length);

// blessed plugins first
const sortedPlugins = orderBy(
plugins,
(plugin) => isBlessedPlugin(plugin.id),
['desc'],
);

const table = createTable(
sortedPlugins.map((plugin) => [
isBlessedPlugin(plugin.id) ? '(built-in)' : plugin.id,
plugin.version || '(n/a)',
plugin.description || '(n/a)',
path.relative(process.cwd(), plugin.entryPoint),
]),
['Name', 'Version', 'Description', 'Resolved'],
);

console.log(`${table}`);
}

static async listRules(
opts: ArgumentsCamelCase<CommonOptionTypes>,
): Promise<void> {
const rules = await Smoker.getRules(opts);

const headers = terminalLink.isSupported
? ['Name', 'Description', 'Plugin']
: ['Name', 'Description', 'Plugin', 'URL'];

debug('Found %d rules', rules.length);

const data = rules.map((rule) => {
const ruleName =
terminalLink.isSupported && rule.url
? terminalLink(rule.id, rule.url)
: rule.id;
const row: (undefined | string)[] = [
ruleName,
rule.description,
isBlessedPlugin(rule[kComponentId].pluginName)
? '(built-in)'
: rule[kComponentId].pluginName,
];
if (!terminalLink.isSupported) {
row.push(rule.url);
}
return row;
});

const table = createTable(data, headers);

console.log(`${table}`);
}
}
12 changes: 2 additions & 10 deletions packages/midnight-smoker/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@ import {readConfigFile} from '../config-file';
import {readSmokerPkgJson} from '../pkg-util';
import {GlobalOptions} from './cli-options';
import {handleRejection, mergeOptions} from './cli-util';
import {
LintCommand,
ListPluginsCommand,
ListReportersCommand,
ListRulesCommand,
RunScriptCommand,
} from './command';
import {LintCommand, ListCommand, RunScriptCommand} from './command';

const debug = Debug('midnight-smoker:cli');

Expand Down Expand Up @@ -53,9 +47,7 @@ async function main(args: string[]): Promise<void> {
.scriptName('smoker')
.options(GlobalOptions)
.command(new LintCommand())
.command(new ListPluginsCommand())
.command(new ListReportersCommand())
.command(new ListRulesCommand())
.command(new ListCommand())
.command(new RunScriptCommand())
.middleware(mergeOpts)
.epilog(epilog)
Expand Down

0 comments on commit 831ae53

Please sign in to comment.