feat: Enhance remove-task command to handle multiple comma-separated task IDs

This commit is contained in:
Kresna Sucandra
2025-04-17 07:27:41 +08:00
committed by Ralph Khreish
parent d99fa00980
commit 3eca720f36
2 changed files with 215 additions and 168 deletions

View File

@@ -0,0 +1,5 @@
---
'task-master-ai': patch
---
Fix remove-task command to handle multiple comma-separated task IDs

View File

@@ -1374,18 +1374,18 @@ function registerCommands(programInstance) {
// remove-task command // remove-task command
programInstance programInstance
.command('remove-task') .command('remove-task')
.description('Remove a task or subtask permanently') .description('Remove one or more tasks or subtasks permanently')
.option( .option(
'-i, --id <id>', '-i, --id <id>',
'ID of the task or subtask to remove (e.g., "5" or "5.2")' 'ID(s) of the task(s) or subtask(s) to remove (e.g., "5" or "5.2" or "5,6,7")'
) )
.option('-f, --file <file>', 'Path to the tasks file', 'tasks/tasks.json') .option('-f, --file <file>', 'Path to the tasks file', 'tasks/tasks.json')
.option('-y, --yes', 'Skip confirmation prompt', false) .option('-y, --yes', 'Skip confirmation prompt', false)
.action(async (options) => { .action(async (options) => {
const tasksPath = options.file; const tasksPath = options.file;
const taskId = options.id; const taskIds = options.id;
if (!taskId) { if (!taskIds) {
console.error(chalk.red('Error: Task ID is required')); console.error(chalk.red('Error: Task ID is required'));
console.error( console.error(
chalk.yellow('Usage: task-master remove-task --id=<taskId>') chalk.yellow('Usage: task-master remove-task --id=<taskId>')
@@ -1394,7 +1394,7 @@ function registerCommands(programInstance) {
} }
try { try {
// Check if the task exists // Check if the tasks file exists and is valid
const data = readJSON(tasksPath); const data = readJSON(tasksPath);
if (!data || !data.tasks) { if (!data || !data.tasks) {
console.error( console.error(
@@ -1403,25 +1403,30 @@ function registerCommands(programInstance) {
process.exit(1); process.exit(1);
} }
if (!taskExists(data.tasks, taskId)) { // Split task IDs if comma-separated
console.error(chalk.red(`Error: Task with ID ${taskId} not found`)); const taskIdArray = taskIds.split(',').map(id => id.trim());
// Validate all task IDs exist before proceeding
const invalidTasks = taskIdArray.filter(id => !taskExists(data.tasks, id));
if (invalidTasks.length > 0) {
console.error(chalk.red(`Error: The following tasks were not found: ${invalidTasks.join(', ')}`));
process.exit(1); process.exit(1);
} }
// Load task for display
const task = findTaskById(data.tasks, taskId);
// Skip confirmation if --yes flag is provided // Skip confirmation if --yes flag is provided
if (!options.yes) { if (!options.yes) {
// Display task information // Display tasks to be removed
console.log(); console.log();
console.log( console.log(
chalk.red.bold( chalk.red.bold(
'⚠️ WARNING: This will permanently delete the following task:' '⚠️ WARNING: This will permanently delete the following tasks:'
) )
); );
console.log(); console.log();
for (const taskId of taskIdArray) {
const task = findTaskById(data.tasks, taskId);
if (typeof taskId === 'string' && taskId.includes('.')) { if (typeof taskId === 'string' && taskId.includes('.')) {
// It's a subtask // It's a subtask
const [parentId, subtaskId] = taskId.split('.'); const [parentId, subtaskId] = taskId.split('.');
@@ -1462,8 +1467,8 @@ function registerCommands(programInstance) {
}); });
} }
} }
console.log(); console.log();
}
// Prompt for confirmation // Prompt for confirmation
const { confirm } = await inquirer.prompt([ const { confirm } = await inquirer.prompt([
@@ -1471,7 +1476,7 @@ function registerCommands(programInstance) {
type: 'confirm', type: 'confirm',
name: 'confirm', name: 'confirm',
message: chalk.red.bold( message: chalk.red.bold(
'Are you sure you want to permanently delete this task?' `Are you sure you want to permanently delete ${taskIdArray.length > 1 ? 'these tasks' : 'this task'}?`
), ),
default: false default: false
} }
@@ -1483,32 +1488,69 @@ function registerCommands(programInstance) {
} }
} }
const indicator = startLoadingIndicator('Removing task...'); const indicator = startLoadingIndicator('Removing tasks...');
// Remove the task // Remove each task
const results = [];
for (const taskId of taskIdArray) {
try {
const result = await removeTask(tasksPath, taskId); const result = await removeTask(tasksPath, taskId);
results.push({ taskId, success: true, ...result });
} catch (error) {
results.push({ taskId, success: false, error: error.message });
}
}
stopLoadingIndicator(indicator); stopLoadingIndicator(indicator);
// Display success message with appropriate color based on task or subtask // Display results
if (typeof taskId === 'string' && taskId.includes('.')) { const successfulRemovals = results.filter(r => r.success);
// It was a subtask const failedRemovals = results.filter(r => !r.success);
if (successfulRemovals.length > 0) {
console.log( console.log(
boxen( boxen(
chalk.green(`Subtask ${taskId} has been successfully removed`), chalk.green(
{ padding: 1, borderColor: 'green', borderStyle: 'round' } `Successfully removed ${successfulRemovals.length} task${successfulRemovals.length > 1 ? 's' : ''}`
) ) +
); '\n\n' +
} else { successfulRemovals.map(r =>
// It was a main task chalk.white(`${r.taskId.includes('.') ? 'Subtask' : 'Task'} ${r.taskId}`)
console.log( ).join('\n'),
boxen(chalk.green(`Task ${taskId} has been successfully removed`), { {
padding: 1, padding: 1,
borderColor: 'green', borderColor: 'green',
borderStyle: 'round' borderStyle: 'round',
}) margin: { top: 1 }
}
)
); );
} }
if (failedRemovals.length > 0) {
console.log(
boxen(
chalk.red(
`Failed to remove ${failedRemovals.length} task${failedRemovals.length > 1 ? 's' : ''}`
) +
'\n\n' +
failedRemovals.map(r =>
chalk.white(`${r.taskId}: ${r.error}`)
).join('\n'),
{
padding: 1,
borderColor: 'red',
borderStyle: 'round',
margin: { top: 1 }
}
)
);
// Exit with error if any removals failed
if (successfulRemovals.length === 0) {
process.exit(1);
}
}
} catch (error) { } catch (error) {
console.error( console.error(
chalk.red(`Error: ${error.message || 'An unknown error occurred'}`) chalk.red(`Error: ${error.message || 'An unknown error occurred'}`)