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

@@ -129,26 +129,26 @@ function registerCommands(programInstance) {
console.log( console.log(
boxen( boxen(
chalk.white.bold('Parse PRD Help') + chalk.white.bold('Parse PRD Help') +
'\n\n' + '\n\n' +
chalk.cyan('Usage:') + chalk.cyan('Usage:') +
'\n' + '\n' +
` task-master parse-prd <prd-file.txt> [options]\n\n` + ` task-master parse-prd <prd-file.txt> [options]\n\n` +
chalk.cyan('Options:') + chalk.cyan('Options:') +
'\n' + '\n' +
' -i, --input <file> Path to the PRD file (alternative to positional argument)\n' + ' -i, --input <file> Path to the PRD file (alternative to positional argument)\n' +
' -o, --output <file> Output file path (default: "tasks/tasks.json")\n' + ' -o, --output <file> Output file path (default: "tasks/tasks.json")\n' +
' -n, --num-tasks <number> Number of tasks to generate (default: 10)\n' + ' -n, --num-tasks <number> Number of tasks to generate (default: 10)\n' +
' -f, --force Skip confirmation when overwriting existing tasks\n\n' + ' -f, --force Skip confirmation when overwriting existing tasks\n\n' +
chalk.cyan('Example:') + chalk.cyan('Example:') +
'\n' + '\n' +
' task-master parse-prd requirements.txt --num-tasks 15\n' + ' task-master parse-prd requirements.txt --num-tasks 15\n' +
' task-master parse-prd --input=requirements.txt\n' + ' task-master parse-prd --input=requirements.txt\n' +
' task-master parse-prd --force\n\n' + ' task-master parse-prd --force\n\n' +
chalk.yellow('Note: This command will:') + chalk.yellow('Note: This command will:') +
'\n' + '\n' +
' 1. Look for a PRD file at scripts/prd.txt by default\n' + ' 1. Look for a PRD file at scripts/prd.txt by default\n' +
' 2. Use the file specified by --input or positional argument if provided\n' + ' 2. Use the file specified by --input or positional argument if provided\n' +
' 3. Generate tasks from the PRD and overwrite any existing tasks.json file', ' 3. Generate tasks from the PRD and overwrite any existing tasks.json file',
{ padding: 1, borderColor: 'blue', borderStyle: 'round' } { padding: 1, borderColor: 'blue', borderStyle: 'round' }
) )
); );
@@ -1132,25 +1132,25 @@ function registerCommands(programInstance) {
chalk.white.bold( chalk.white.bold(
`Subtask ${parentId}.${subtask.id} Added Successfully` `Subtask ${parentId}.${subtask.id} Added Successfully`
) + ) +
'\n\n' + '\n\n' +
chalk.white(`Title: ${subtask.title}`) + chalk.white(`Title: ${subtask.title}`) +
'\n' + '\n' +
chalk.white(`Status: ${getStatusWithColor(subtask.status)}`) + chalk.white(`Status: ${getStatusWithColor(subtask.status)}`) +
'\n' + '\n' +
(dependencies.length > 0 (dependencies.length > 0
? chalk.white(`Dependencies: ${dependencies.join(', ')}`) + ? chalk.white(`Dependencies: ${dependencies.join(', ')}`) +
'\n' '\n'
: '') + : '') +
'\n' + '\n' +
chalk.white.bold('Next Steps:') + chalk.white.bold('Next Steps:') +
'\n' + '\n' +
chalk.cyan( chalk.cyan(
`1. Run ${chalk.yellow(`task-master show ${parentId}`)} to see the parent task with all subtasks` `1. Run ${chalk.yellow(`task-master show ${parentId}`)} to see the parent task with all subtasks`
) + ) +
'\n' + '\n' +
chalk.cyan( chalk.cyan(
`2. Run ${chalk.yellow(`task-master set-status --id=${parentId}.${subtask.id} --status=in-progress`)} to start working on it` `2. Run ${chalk.yellow(`task-master set-status --id=${parentId}.${subtask.id} --status=in-progress`)} to start working on it`
), ),
{ {
padding: 1, padding: 1,
borderColor: 'green', borderColor: 'green',
@@ -1166,19 +1166,19 @@ function registerCommands(programInstance) {
console.log( console.log(
boxen( boxen(
chalk.white.bold('Usage Examples:') + chalk.white.bold('Usage Examples:') +
'\n\n' + '\n\n' +
chalk.white('Convert existing task to subtask:') + chalk.white('Convert existing task to subtask:') +
'\n' + '\n' +
chalk.yellow( chalk.yellow(
` task-master add-subtask --parent=5 --task-id=8` ` task-master add-subtask --parent=5 --task-id=8`
) + ) +
'\n\n' + '\n\n' +
chalk.white('Create new subtask:') + chalk.white('Create new subtask:') +
'\n' + '\n' +
chalk.yellow( chalk.yellow(
` task-master add-subtask --parent=5 --title="Implement login UI" --description="Create the login form"` ` task-master add-subtask --parent=5 --title="Implement login UI" --description="Create the login form"`
) + ) +
'\n\n', '\n\n',
{ padding: 1, borderColor: 'blue', borderStyle: 'round' } { padding: 1, borderColor: 'blue', borderStyle: 'round' }
) )
); );
@@ -1200,25 +1200,25 @@ function registerCommands(programInstance) {
console.log( console.log(
boxen( boxen(
chalk.white.bold('Add Subtask Command Help') + chalk.white.bold('Add Subtask Command Help') +
'\n\n' + '\n\n' +
chalk.cyan('Usage:') + chalk.cyan('Usage:') +
'\n' + '\n' +
` task-master add-subtask --parent=<id> [options]\n\n` + ` task-master add-subtask --parent=<id> [options]\n\n` +
chalk.cyan('Options:') + chalk.cyan('Options:') +
'\n' + '\n' +
' -p, --parent <id> Parent task ID (required)\n' + ' -p, --parent <id> Parent task ID (required)\n' +
' -i, --task-id <id> Existing task ID to convert to subtask\n' + ' -i, --task-id <id> Existing task ID to convert to subtask\n' +
' -t, --title <title> Title for the new subtask\n' + ' -t, --title <title> Title for the new subtask\n' +
' -d, --description <text> Description for the new subtask\n' + ' -d, --description <text> Description for the new subtask\n' +
' --details <text> Implementation details for the new subtask\n' + ' --details <text> Implementation details for the new subtask\n' +
' --dependencies <ids> Comma-separated list of dependency IDs\n' + ' --dependencies <ids> Comma-separated list of dependency IDs\n' +
' -s, --status <status> Status for the new subtask (default: "pending")\n' + ' -s, --status <status> Status for the new subtask (default: "pending")\n' +
' -f, --file <file> Path to the tasks file (default: "tasks/tasks.json")\n' + ' -f, --file <file> Path to the tasks file (default: "tasks/tasks.json")\n' +
' --skip-generate Skip regenerating task files\n\n' + ' --skip-generate Skip regenerating task files\n\n' +
chalk.cyan('Examples:') + chalk.cyan('Examples:') +
'\n' + '\n' +
' task-master add-subtask --parent=5 --task-id=8\n' + ' task-master add-subtask --parent=5 --task-id=8\n' +
' task-master add-subtask -p 5 -t "Implement login UI" -d "Create the login form"', ' task-master add-subtask -p 5 -t "Implement login UI" -d "Create the login form"',
{ padding: 1, borderColor: 'blue', borderStyle: 'round' } { padding: 1, borderColor: 'blue', borderStyle: 'round' }
) )
); );
@@ -1291,24 +1291,24 @@ function registerCommands(programInstance) {
chalk.white.bold( chalk.white.bold(
`Subtask ${subtaskId} Converted to Task #${result.id}` `Subtask ${subtaskId} Converted to Task #${result.id}`
) + ) +
'\n\n' + '\n\n' +
chalk.white(`Title: ${result.title}`) + chalk.white(`Title: ${result.title}`) +
'\n' + '\n' +
chalk.white(`Status: ${getStatusWithColor(result.status)}`) + chalk.white(`Status: ${getStatusWithColor(result.status)}`) +
'\n' + '\n' +
chalk.white( chalk.white(
`Dependencies: ${result.dependencies.join(', ')}` `Dependencies: ${result.dependencies.join(', ')}`
) + ) +
'\n\n' + '\n\n' +
chalk.white.bold('Next Steps:') + chalk.white.bold('Next Steps:') +
'\n' + '\n' +
chalk.cyan( chalk.cyan(
`1. Run ${chalk.yellow(`task-master show ${result.id}`)} to see details of the new task` `1. Run ${chalk.yellow(`task-master show ${result.id}`)} to see details of the new task`
) + ) +
'\n' + '\n' +
chalk.cyan( chalk.cyan(
`2. Run ${chalk.yellow(`task-master set-status --id=${result.id} --status=in-progress`)} to start working on it` `2. Run ${chalk.yellow(`task-master set-status --id=${result.id} --status=in-progress`)} to start working on it`
), ),
{ {
padding: 1, padding: 1,
borderColor: 'green', borderColor: 'green',
@@ -1322,8 +1322,8 @@ function registerCommands(programInstance) {
console.log( console.log(
boxen( boxen(
chalk.white.bold(`Subtask ${subtaskId} Removed`) + chalk.white.bold(`Subtask ${subtaskId} Removed`) +
'\n\n' + '\n\n' +
chalk.white('The subtask has been successfully deleted.'), chalk.white('The subtask has been successfully deleted.'),
{ {
padding: 1, padding: 1,
borderColor: 'green', borderColor: 'green',
@@ -1351,21 +1351,21 @@ function registerCommands(programInstance) {
console.log( console.log(
boxen( boxen(
chalk.white.bold('Remove Subtask Command Help') + chalk.white.bold('Remove Subtask Command Help') +
'\n\n' + '\n\n' +
chalk.cyan('Usage:') + chalk.cyan('Usage:') +
'\n' + '\n' +
` task-master remove-subtask --id=<parentId.subtaskId> [options]\n\n` + ` task-master remove-subtask --id=<parentId.subtaskId> [options]\n\n` +
chalk.cyan('Options:') + chalk.cyan('Options:') +
'\n' + '\n' +
' -i, --id <id> Subtask ID(s) to remove in format "parentId.subtaskId" (can be comma-separated, required)\n' + ' -i, --id <id> Subtask ID(s) to remove in format "parentId.subtaskId" (can be comma-separated, required)\n' +
' -c, --convert Convert the subtask to a standalone task instead of deleting it\n' + ' -c, --convert Convert the subtask to a standalone task instead of deleting it\n' +
' -f, --file <file> Path to the tasks file (default: "tasks/tasks.json")\n' + ' -f, --file <file> Path to the tasks file (default: "tasks/tasks.json")\n' +
' --skip-generate Skip regenerating task files\n\n' + ' --skip-generate Skip regenerating task files\n\n' +
chalk.cyan('Examples:') + chalk.cyan('Examples:') +
'\n' + '\n' +
' task-master remove-subtask --id=5.2\n' + ' task-master remove-subtask --id=5.2\n' +
' task-master remove-subtask --id=5.2,6.3,7.1\n' + ' task-master remove-subtask --id=5.2,6.3,7.1\n' +
' task-master remove-subtask --id=5.2 --convert', ' task-master remove-subtask --id=5.2 --convert',
{ padding: 1, borderColor: 'blue', borderStyle: 'round' } { padding: 1, borderColor: 'blue', borderStyle: 'round' }
) )
); );
@@ -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,75 +1403,80 @@ 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();
if (typeof taskId === 'string' && taskId.includes('.')) { for (const taskId of taskIdArray) {
// It's a subtask const task = findTaskById(data.tasks, taskId);
const [parentId, subtaskId] = taskId.split('.');
console.log(chalk.white.bold(`Subtask ${taskId}: ${task.title}`));
console.log(
chalk.gray(
`Parent Task: ${task.parentTask.id} - ${task.parentTask.title}`
)
);
} else {
// It's a main task
console.log(chalk.white.bold(`Task ${taskId}: ${task.title}`));
// Show if it has subtasks if (typeof taskId === 'string' && taskId.includes('.')) {
if (task.subtasks && task.subtasks.length > 0) { // It's a subtask
const [parentId, subtaskId] = taskId.split('.');
console.log(chalk.white.bold(`Subtask ${taskId}: ${task.title}`));
console.log( console.log(
chalk.yellow( chalk.gray(
`⚠️ This task has ${task.subtasks.length} subtasks that will also be deleted!` `Parent Task: ${task.parentTask.id} - ${task.parentTask.title}`
) )
); );
} } else {
// It's a main task
console.log(chalk.white.bold(`Task ${taskId}: ${task.title}`));
// Show if other tasks depend on it // Show if it has subtasks
const dependentTasks = data.tasks.filter( if (task.subtasks && task.subtasks.length > 0) {
(t) => console.log(
t.dependencies && t.dependencies.includes(parseInt(taskId, 10)) chalk.yellow(
); `⚠️ This task has ${task.subtasks.length} subtasks that will also be deleted!`
)
);
}
if (dependentTasks.length > 0) { // Show if other tasks depend on it
console.log( const dependentTasks = data.tasks.filter(
chalk.yellow( (t) =>
`⚠️ Warning: ${dependentTasks.length} other tasks depend on this task!` t.dependencies && t.dependencies.includes(parseInt(taskId, 10))
)
); );
console.log(chalk.yellow('These dependencies will be removed:'));
dependentTasks.forEach((t) => { if (dependentTasks.length > 0) {
console.log(chalk.yellow(` - Task ${t.id}: ${t.title}`)); console.log(
}); chalk.yellow(
`⚠️ Warning: ${dependentTasks.length} other tasks depend on this task!`
)
);
console.log(chalk.yellow('These dependencies will be removed:'));
dependentTasks.forEach((t) => {
console.log(chalk.yellow(` - Task ${t.id}: ${t.title}`));
});
}
} }
console.log();
} }
console.log();
// Prompt for confirmation // Prompt for confirmation
const { confirm } = await inquirer.prompt([ const { confirm } = await inquirer.prompt([
{ {
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,31 +1488,68 @@ function registerCommands(programInstance) {
} }
} }
const indicator = startLoadingIndicator('Removing task...'); const indicator = startLoadingIndicator('Removing tasks...');
// Remove the task // Remove each task
const result = await removeTask(tasksPath, taskId); const results = [];
for (const taskId of taskIdArray) {
try {
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' +
successfulRemovals.map(r =>
chalk.white(`${r.taskId.includes('.') ? 'Subtask' : 'Task'} ${r.taskId}`)
).join('\n'),
{
padding: 1,
borderColor: 'green',
borderStyle: 'round',
margin: { top: 1 }
}
) )
); );
} else { }
// It was a main task
if (failedRemovals.length > 0) {
console.log( console.log(
boxen(chalk.green(`Task ${taskId} has been successfully removed`), { boxen(
padding: 1, chalk.red(
borderColor: 'green', `Failed to remove ${failedRemovals.length} task${failedRemovals.length > 1 ? 's' : ''}`
borderStyle: 'round' ) +
}) '\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(
@@ -1719,7 +1761,7 @@ function compareVersions(v1, v2) {
function displayUpgradeNotification(currentVersion, latestVersion) { function displayUpgradeNotification(currentVersion, latestVersion) {
const message = boxen( const message = boxen(
`${chalk.blue.bold('Update Available!')} ${chalk.dim(currentVersion)}${chalk.green(latestVersion)}\n\n` + `${chalk.blue.bold('Update Available!')} ${chalk.dim(currentVersion)}${chalk.green(latestVersion)}\n\n` +
`Run ${chalk.cyan('npm i task-master-ai@latest -g')} to update to the latest version with new features and bug fixes.`, `Run ${chalk.cyan('npm i task-master-ai@latest -g')} to update to the latest version with new features and bug fixes.`,
{ {
padding: 1, padding: 1,
margin: { top: 1, bottom: 1 }, margin: { top: 1, bottom: 1 },