Skip to content

Commit

Permalink
Fixed nested commands bug
Browse files Browse the repository at this point in the history
  • Loading branch information
dgtlntv committed Oct 4, 2024
1 parent bdea407 commit bfb4ac0
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 120 deletions.
Binary file modified .yarn/install-state.gz
Binary file not shown.
182 changes: 146 additions & 36 deletions src/commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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."
}
}
}
]
}
}
}
}
}
178 changes: 94 additions & 84 deletions src/io/yargs/commandsToYargs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit bfb4ac0

Please sign in to comment.