diff --git a/.yarn/install-state.gz b/.yarn/install-state.gz index 11eaef0..e9813dd 100644 Binary files a/.yarn/install-state.gz and b/.yarn/install-state.gz differ diff --git a/src/commands.json b/src/commands.json index a4e4733..c803796 100644 --- a/src/commands.json +++ b/src/commands.json @@ -72,45 +72,89 @@ ] }, "add": { - "description": "Add a new task", - "handler": [ - { - "component": "conditional", - "output": { - "if": "isLoggedIn == 'true'", - "then": [ - { - "component": "input", - "name": "taskName", - "message": "Enter task name:" - }, - { - "component": "select", - "name": "priority", - "message": "Select task priority:", - "choices": [ - { "name": "Low", "value": "low" }, - { "name": "Medium", "value": "medium" }, - { "name": "High", "value": "high" } - ] - }, - { - "component": "progressBar", - "output": "Adding task...", - "duration": 1500 - }, - { - "component": "text", - "output": "Task '{{taskName}}' added with {{priority}} priority." + "description": "Add a new task or subtask", + "commands": { + "task": { + "description": "Add a new main task", + "handler": [ + { + "component": "conditional", + "output": { + "if": "isLoggedIn == 'true'", + "then": [ + { + "component": "input", + "name": "taskName", + "message": "Enter task name:" + }, + { + "component": "select", + "name": "priority", + "message": "Select task priority:", + "choices": [ + { "name": "Low", "value": "low" }, + { + "name": "Medium", + "value": "medium" + }, + { "name": "High", "value": "high" } + ] + }, + { + "component": "progressBar", + "output": "Adding task...", + "duration": 1500 + }, + { + "component": "text", + "output": "Task '{{taskName}}' added with {{priority}} priority." + } + ], + "else": { + "component": "text", + "output": "Please log in to add tasks." + } } - ], - "else": { - "component": "text", - "output": "Please log in to add tasks." } - } + ] + }, + "subtask": { + "description": "Add a new subtask to an existing task", + "handler": [ + { + "component": "conditional", + "output": { + "if": "isLoggedIn == 'true'", + "then": [ + { + "component": "input", + "name": "parentTaskId", + "message": "Enter the ID of the parent task:" + }, + { + "component": "input", + "name": "subtaskName", + "message": "Enter subtask name:" + }, + { + "component": "progressBar", + "output": "Adding subtask...", + "duration": 1500 + }, + { + "component": "text", + "output": "Subtask '{{subtaskName}}' added to task {{parentTaskId}}." + } + ], + "else": { + "component": "text", + "output": "Please log in to add subtasks." + } + } + } + ] } - ] + } }, "list": { "description": "List all tasks", @@ -183,6 +227,72 @@ } } ] + }, + "report": { + "description": "Generate reports", + "commands": { + "summary": { + "description": "Generate a summary report of all tasks", + "handler": [ + { + "component": "conditional", + "output": { + "if": "isLoggedIn == 'true'", + "then": [ + { + "component": "spinner", + "output": "Generating summary report...", + "duration": 2000 + }, + { + "component": "text", + "output": "Summary Report:\n- Total tasks: 10\n- Completed: 3\n- In Progress: 5\n- Todo: 2" + } + ], + "else": { + "component": "text", + "output": "Please log in to generate reports." + } + } + } + ] + }, + "priority": { + "description": "Generate a report of tasks by priority", + "handler": [ + { + "component": "conditional", + "output": { + "if": "isLoggedIn == 'true'", + "then": [ + { + "component": "spinner", + "output": "Generating priority report...", + "duration": 2000 + }, + { + "component": "table", + "output": [ + [ + "Priority", + "Total Tasks", + "Completed" + ], + ["High", "4", "1"], + ["Medium", "3", "1"], + ["Low", "3", "1"] + ] + } + ], + "else": { + "component": "text", + "output": "Please log in to generate reports." + } + } + } + ] + } + } } } } diff --git a/src/io/yargs/commandsToYargs.js b/src/io/yargs/commandsToYargs.js index 35347b0..898be30 100644 --- a/src/io/yargs/commandsToYargs.js +++ b/src/io/yargs/commandsToYargs.js @@ -5,92 +5,102 @@ var globalVariables = {} export default function commandsToYargs(yargs, config, localEcho) { globalVariables = config.variables - // Process custom commands - Object.entries(config.commands).forEach(([commandName, commandConfig]) => { - yargs.command( - commandName, - commandConfig.description || - commandConfig.desc || - commandConfig.describe || - "", - (yargs) => { - // Process positional arguments - if (commandConfig.positional) { - Object.entries(commandConfig.positional).forEach( - ([argName, argConfig]) => { - yargs.positional(argName, argConfig) - } - ) - } - // Process options - if (commandConfig.options) { - Object.entries(commandConfig.options).forEach( - ([optionName, optionConfig]) => { - yargs.option(optionName, optionConfig) - } - ) - } - // Process subcommands recursively - // TODO subcommands is bugged - if (commandConfig.commands) { - Object.entries(commandConfig.commands).forEach( - ([subCommandName, subCommandConfig]) => { - yargs.command( - subCommandName, - subCommandConfig.description, - (subYargs) => { - // Recursive call to handle nested commands - commandsToYargs( - subYargs, - { - commands: { - [subCommandName]: - subCommandConfig, - }, - }, - localEcho - ) - } - ) - } - ) - } - }, - // Add async handler function - async (argv) => { - try { - await componentsToYargsHandler( - commandConfig.handler, - argv, - localEcho, - globalVariables - ) - } catch (error) { - console.error("Error in command handler:", error) - } - } - ) + function buildCommands(commands, yargs) { + Object.entries(commands).forEach(([commandName, commandConfig]) => { + yargs.command( + ///////////////////////////////////////// + // Command name + ///////////////////////////////////////// + commandName, - // Handle command aliases - if (commandConfig.alias) { - const aliases = Array.isArray(commandConfig.alias) - ? commandConfig.alias - : [commandConfig.alias] - aliases.forEach((alias) => { - yargs.alias(commandName, alias) - }) - } + ///////////////////////////////////////// + // Command description + ///////////////////////////////////////// + commandConfig.description || + commandConfig.desc || + commandConfig.describe || + "", + (y) => { + ///////////////////////////////////////// + // Aliases + ///////////////////////////////////////// + if (commandConfig.alias) { + const aliases = Array.isArray(commandConfig.alias) + ? commandConfig.alias + : [commandConfig.alias] + aliases.forEach((alias) => { + y.alias(commandName, alias) + }) + } - // Handle command examples - if (commandConfig.example) { - const examples = Array.isArray(commandConfig.example[0]) - ? commandConfig.example - : [commandConfig.example] - examples.forEach(([cmd, desc]) => { - yargs.example(cmd, desc) - }) - } - }) + ///////////////////////////////////////// + // Positional arguments + ///////////////////////////////////////// + if (commandConfig.positional) { + Object.entries(commandConfig.positional).forEach( + ([argName, argConfig]) => { + y.positional(argName, argConfig) + } + ) + } + + ///////////////////////////////////////// + // Options + ///////////////////////////////////////// + if (commandConfig.options) { + Object.entries(commandConfig.options).forEach( + ([optionName, optionConfig]) => { + y.option(optionName, optionConfig) + } + ) + } + + ///////////////////////////////////////// + // Examples + ///////////////////////////////////////// + if (commandConfig.example) { + const examples = Array.isArray(commandConfig.example[0]) + ? commandConfig.example + : [commandConfig.example] + examples.forEach(([cmd, desc]) => { + y.example(cmd, desc) + }) + } + + ///////////////////////////////////////// + // Subcommands + ///////////////////////////////////////// + if (commandConfig.commands) { + buildCommands(commandConfig.commands, y) + } + + return y + }, + + ///////////////////////////////////////// + // Handler function + ///////////////////////////////////////// + commandConfig.handler + ? async (argv) => { + try { + await componentsToYargsHandler( + commandConfig.handler, + argv, + localEcho, + globalVariables + ) + } catch (error) { + console.error("Error in command handler:", error) + } + } + : () => { + yargs.showHelp() + } + ) + }) + } + + buildCommands(config.commands, yargs) // Add default commands yargs.command(