yargs-file-commands
Version:
A yargs helper function that lets you define your commands structure via directory and file naming conventions.
72 lines (71 loc) • 2.4 kB
TypeScript
import { type ArgumentsCamelCase, type CommandBuilder, type CommandModule } from 'yargs';
/**
* Represents command alias configuration
* @type {readonly string[] | string | undefined}
*/
export type CommandAlias = readonly string[] | string | undefined;
/**
* Represents command name configuration
* @type {readonly string[] | string | undefined}
*/
export type CommandName = readonly string[] | string | undefined;
/**
* Represents command deprecation configuration
* @type {boolean | string | undefined}
*/
export type CommandDeprecated = boolean | string | undefined;
/**
* Represents command description configuration
* @type {string | false | undefined}
*/
export type CommandDescribe = string | false | undefined;
/**
* Command handler function type
* @type {Function}
*/
export type CommandHandler = (args: ArgumentsCamelCase<any>) => void | Promise<any>;
/**
* Parameters for file commands configuration
* @interface FileCommandsParams
*/
export interface FileCommandsParams {
/** Root directory for command files */
rootDir: string;
}
/**
* Structure of a command module import with individual exports
* @interface CommandImportModule
*/
export interface CommandImportModule {
/** Command aliases */
aliases?: CommandAlias;
/** Command builder function */
builder?: CommandBuilder;
/** Command name */
command?: CommandName;
/** Deprecation status */
deprecated?: CommandDeprecated;
/** Command description */
describe?: CommandDescribe;
/** Command handler function */
handler?: CommandHandler;
}
export interface ImportCommandOptions {
logLevel?: 'info' | 'debug';
}
/**
* Imports a command module from a file
* @async
* @param {string} filePath - Path to the command file
* @param {string} name - Command name
* @param {ImportCommandOptions} options - Import options
* @returns {Promise<CommandModule>} Imported command module
*
* @description
* Dynamically imports a command file and constructs a Yargs command module.
* Supports two styles of command declaration:
* 1. Single export of CommandModule named 'command'
* 2. Individual exports of command parts (command, describe, alias, etc.)
* If no handler is provided, creates a null implementation.
*/
export declare const importCommandFromFile: (filePath: string, name: string, options: ImportCommandOptions) => Promise<CommandModule>;