Skip to content

Commit

Permalink
Fix: show the correct parameters at help
Browse files Browse the repository at this point in the history
  • Loading branch information
MiguelYax committed Oct 23, 2024
1 parent d4dabaa commit cf25a58
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 41 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-donkeys-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"yax-cli": patch
---

Fix: show the correct parameters at help
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"files": [
"/dist",
"/types",
"/docs",
"tsconfig.json"
],
"scripts": {
Expand Down
17 changes: 10 additions & 7 deletions src/Register.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { readdirSync } from "node:fs";
import { parser, getArgs } from "./parser";
import { Arguments, CommandInterface, Options, Rule, RegisterOptions, ProcessType } from "./types";
import { Arguments, CommandInterface, Options, Rule, RegisterOptions, ProcessType, CommandRoute } from "./types";
import { showHelp } from "./helpers";
import { verify } from "./validations";
import { pathfinder } from "./pathfinder";
Expand All @@ -11,7 +10,8 @@ const isClass = (v: CommandInterface | object): boolean => {

export class Register implements CommandInterface {
examples = [];
commands: string[] = [];
commands: CommandRoute[] = [];
command?: CommandRoute;
description: string;
commandsPath: string;
process: ProcessType;
Expand All @@ -32,6 +32,11 @@ export class Register implements CommandInterface {
this.process = ops.process;
this.errors = [];
this.args = getArgs(this.process.argv);
const { command, commands } = pathfinder(ops.commandsPath, this.args);

this.commands = commands;
this.command = command;

this.resolve(this);
}

Expand All @@ -41,7 +46,6 @@ export class Register implements CommandInterface {

resolve(context: CommandInterface) {
const options = parser(this.args.flags, this.validations);
this.commands = readdirSync(this.commandsPath);
const { isValid, errors } = verify(options, context.validations);
this.errors = errors;

Expand All @@ -53,9 +57,8 @@ export class Register implements CommandInterface {
}

async handler(ops: Options, args: Arguments) {
const resolution = pathfinder(this.commandsPath, this.args);
if (resolution.config) {
const module = await import(resolution.config.filePath);
if (this.command) {
const module = await import(this.command.filePath);

const Command = module.default;
const cmd = isClass(Command) ? new Command() : Command;
Expand Down
14 changes: 11 additions & 3 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Arguments, CommandInterface, Rule } from "./types";
import { Arguments, CommandInterface, CommandRoute, Rule } from "./types";

export const toList = (title: string, items: string[]): string[] => {
return items.length ? [`${title}:`, ...items.map((i) => ` * ${i}`)] : [];
Expand All @@ -20,13 +20,21 @@ export const getFlags = (validations: Rule[]): string[] => {
return validations.length > 0 ? ['OPTIONS:', ...flags] : [];
};

export const showHelp = (cmd: CommandInterface, args: Arguments, commands: string[] = [], errors: string[] = []): string[] => {
export const getCommands = (commands: CommandRoute[]): string[] => {
const cmds = commands
.filter((i) => i.commands[0] !== '')
.map((c) => ` * ${c.commands.join(' ')}`);

return commands.length > 0 ? ["COMMANDS:", ...cmds]: [];
};

export const showHelp = (cmd: CommandInterface, args: Arguments, commands: CommandRoute[] = [], errors: string[] = []): string[] => {
const commandInfo = args.commands.length ? [`COMMAND: ${args.commands.join(' ')}`] : [];
const content = [
`USAGE: ${args.bin} <COMMAND> [OPTIONS]`,
...commandInfo,
`DESCRIPTION: ${cmd.description}`,
...toList('COMMANDS', commands),
...getCommands(commands),
...toList('EXAMPLES', cmd.examples),
...toList('ERRORS', errors),
...getFlags(cmd.validations)
Expand Down
24 changes: 7 additions & 17 deletions src/pathfinder.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
import { readdirSync, statSync } from "node:fs";
import { basename, extname } from "node:path";
import { Arguments } from "./types";
import { Arguments, CommandRoute } from "./types";

const supportedExtensions = ['.ts', '.js'];

export type CommandFile = {
filePath: string
commands: string[],
}

export type PathfinderResolution = {
commandFiles: CommandFile[],
config?: CommandFile,
}

export const resolveCommands = (root: string, commandsPath: string, commandFiles: CommandFile[] = []): CommandFile[] => {
export const resolveCommands = (root: string, commandsPath: string, commandFiles: CommandRoute[]): CommandRoute[] => {
const fileList = readdirSync(commandsPath);
for (const file of fileList) {
const filePath = `${commandsPath}/${file}`;
Expand Down Expand Up @@ -47,14 +37,14 @@ export const resolveCommands = (root: string, commandsPath: string, commandFiles
return commandFiles;
};

export const pathfinder = (commandsPath: string, args: Arguments): PathfinderResolution => {
const commandFiles = resolveCommands(commandsPath, commandsPath, []);
export const pathfinder = (commandsPath: string, args: Arguments): { commands: CommandRoute[], command?: CommandRoute } => {
const commands = resolveCommands(commandsPath, commandsPath, []);
const cmd = args.commands.join(' ');
const config = commandFiles.find((c) => c.commands.join(' ') === cmd);
const command = commands.find((c) => c.commands.join(' ') === cmd);

return {
commandFiles,
config
commands,
command
};
};

25 changes: 15 additions & 10 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
export type All = boolean | string | number | number[] | string[]

export type Options = Map<string, All>;
export type Options = Map<string, All>;

export type CommandRoute = {
filePath: string;
commands: string[];
}

export type Rule = {
flag: string,
alias: string,
description: string,
required: boolean,
list?: boolean,
type: 'boolean' | 'string' | 'number' | 'list'
default?: All
flag: string,
alias: string,
description: string,
required: boolean,
list?: boolean,
type: 'boolean' | 'string' | 'number' | 'list'
default?: All
}

export type Arguments = {
node: string
bin: string,
bin: string,
path: string;
commands: string[],
flags: string[],
Expand All @@ -24,8 +29,8 @@ export type Arguments = {
export interface CommandInterface {
description: string,
examples: string[],
handler: (options: Options, args: Arguments)=> void;
validations: Rule[]
handler: (options: Options, args: Arguments) => void;
}

export type RegisterOptions = {
Expand Down
6 changes: 3 additions & 3 deletions tests/pathfinder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Arguments, pathfinder, resolveCommands } from '../src';
describe('get files', () => {
test('resolve posible cmds', () => {
const commandsPath = `${__dirname}/cmds`;
const commandFiles = resolveCommands(commandsPath, commandsPath);
const commandFiles = resolveCommands(commandsPath, commandsPath, []);

expect(commandFiles.length).toBe(4);
expect(commandFiles).toEqual(
Expand All @@ -30,7 +30,7 @@ describe('pathfinder', () => {
};

const resolution = await pathfinder(commandsPath, args);
expect(resolution.config).toBeUndefined();
expect(resolution.command).toBeUndefined();
});
test('should resolve command', async () => {
const commandsPath = `${__dirname}/cmds`;
Expand All @@ -44,6 +44,6 @@ describe('pathfinder', () => {
};

const resolution = await pathfinder(commandsPath, args);
expect(resolution.config).toBeDefined();
expect(resolution.command).toBeDefined();
});
});

0 comments on commit cf25a58

Please # to comment.