feat: adds remove-task command + MCP implementation.
This commit is contained in:
@@ -3464,6 +3464,156 @@ Provide concrete examples, code snippets, or implementation details when relevan
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a task or subtask from the tasks file
|
||||
* @param {string} tasksPath - Path to the tasks file
|
||||
* @param {string|number} taskId - ID of task or subtask to remove (e.g., '5' or '5.2')
|
||||
* @returns {Object} Result object with success message and removed task info
|
||||
*/
|
||||
async function removeTask(tasksPath, taskId) {
|
||||
try {
|
||||
// Read the tasks file
|
||||
const data = readJSON(tasksPath);
|
||||
if (!data || !data.tasks) {
|
||||
throw new Error(`No valid tasks found in ${tasksPath}`);
|
||||
}
|
||||
|
||||
// Check if the task ID exists
|
||||
if (!taskExists(data.tasks, taskId)) {
|
||||
throw new Error(`Task with ID ${taskId} not found`);
|
||||
}
|
||||
|
||||
// Handle subtask removal (e.g., '5.2')
|
||||
if (typeof taskId === 'string' && taskId.includes('.')) {
|
||||
const [parentTaskId, subtaskId] = taskId.split('.').map(id => parseInt(id, 10));
|
||||
|
||||
// Find the parent task
|
||||
const parentTask = data.tasks.find(t => t.id === parentTaskId);
|
||||
if (!parentTask || !parentTask.subtasks) {
|
||||
throw new Error(`Parent task with ID ${parentTaskId} or its subtasks not found`);
|
||||
}
|
||||
|
||||
// Find the subtask to remove
|
||||
const subtaskIndex = parentTask.subtasks.findIndex(st => st.id === subtaskId);
|
||||
if (subtaskIndex === -1) {
|
||||
throw new Error(`Subtask with ID ${subtaskId} not found in parent task ${parentTaskId}`);
|
||||
}
|
||||
|
||||
// Store the subtask info before removal for the result
|
||||
const removedSubtask = parentTask.subtasks[subtaskIndex];
|
||||
|
||||
// Remove the subtask
|
||||
parentTask.subtasks.splice(subtaskIndex, 1);
|
||||
|
||||
// Remove references to this subtask in other subtasks' dependencies
|
||||
if (parentTask.subtasks && parentTask.subtasks.length > 0) {
|
||||
parentTask.subtasks.forEach(subtask => {
|
||||
if (subtask.dependencies && subtask.dependencies.includes(subtaskId)) {
|
||||
subtask.dependencies = subtask.dependencies.filter(depId => depId !== subtaskId);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Save the updated tasks
|
||||
writeJSON(tasksPath, data);
|
||||
|
||||
// Generate updated task files
|
||||
try {
|
||||
await generateTaskFiles(tasksPath, path.dirname(tasksPath));
|
||||
} catch (genError) {
|
||||
log('warn', `Successfully removed subtask but failed to regenerate task files: ${genError.message}`);
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: `Successfully removed subtask ${subtaskId} from task ${parentTaskId}`,
|
||||
removedTask: removedSubtask,
|
||||
parentTaskId: parentTaskId
|
||||
};
|
||||
}
|
||||
|
||||
// Handle main task removal
|
||||
const taskIdNum = parseInt(taskId, 10);
|
||||
const taskIndex = data.tasks.findIndex(t => t.id === taskIdNum);
|
||||
if (taskIndex === -1) {
|
||||
throw new Error(`Task with ID ${taskId} not found`);
|
||||
}
|
||||
|
||||
// Store the task info before removal for the result
|
||||
const removedTask = data.tasks[taskIndex];
|
||||
|
||||
// Remove the task
|
||||
data.tasks.splice(taskIndex, 1);
|
||||
|
||||
// Remove references to this task in other tasks' dependencies
|
||||
data.tasks.forEach(task => {
|
||||
if (task.dependencies && task.dependencies.includes(taskIdNum)) {
|
||||
task.dependencies = task.dependencies.filter(depId => depId !== taskIdNum);
|
||||
}
|
||||
});
|
||||
|
||||
// Save the updated tasks
|
||||
writeJSON(tasksPath, data);
|
||||
|
||||
// Delete the task file if it exists
|
||||
const taskFileName = path.join(path.dirname(tasksPath), `task_${taskIdNum.toString().padStart(3, '0')}.txt`);
|
||||
if (fs.existsSync(taskFileName)) {
|
||||
try {
|
||||
fs.unlinkSync(taskFileName);
|
||||
} catch (unlinkError) {
|
||||
log('warn', `Successfully removed task from tasks.json but failed to delete task file: ${unlinkError.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Generate updated task files
|
||||
try {
|
||||
await generateTaskFiles(tasksPath, path.dirname(tasksPath));
|
||||
} catch (genError) {
|
||||
log('warn', `Successfully removed task but failed to regenerate task files: ${genError.message}`);
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: `Successfully removed task ${taskId}`,
|
||||
removedTask: removedTask
|
||||
};
|
||||
} catch (error) {
|
||||
log('error', `Error removing task: ${error.message}`);
|
||||
throw {
|
||||
code: 'REMOVE_TASK_ERROR',
|
||||
message: error.message,
|
||||
details: error.stack
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a task with the given ID exists
|
||||
* @param {Array} tasks - Array of tasks to search
|
||||
* @param {string|number} taskId - ID of task or subtask to check
|
||||
* @returns {boolean} Whether the task exists
|
||||
*/
|
||||
function taskExists(tasks, taskId) {
|
||||
// Handle subtask IDs (e.g., "1.2")
|
||||
if (typeof taskId === 'string' && taskId.includes('.')) {
|
||||
const [parentIdStr, subtaskIdStr] = taskId.split('.');
|
||||
const parentId = parseInt(parentIdStr, 10);
|
||||
const subtaskId = parseInt(subtaskIdStr, 10);
|
||||
|
||||
// Find the parent task
|
||||
const parentTask = tasks.find(t => t.id === parentId);
|
||||
|
||||
// If parent exists, check if subtask exists
|
||||
return parentTask &&
|
||||
parentTask.subtasks &&
|
||||
parentTask.subtasks.some(st => st.id === subtaskId);
|
||||
}
|
||||
|
||||
// Handle regular task IDs
|
||||
const id = parseInt(taskId, 10);
|
||||
return tasks.some(t => t.id === id);
|
||||
}
|
||||
|
||||
// Export task manager functions
|
||||
export {
|
||||
parsePRD,
|
||||
@@ -3482,4 +3632,7 @@ export {
|
||||
removeSubtask,
|
||||
findNextTask,
|
||||
analyzeTaskComplexity,
|
||||
removeTask,
|
||||
findTaskById,
|
||||
taskExists,
|
||||
};
|
||||
Reference in New Issue
Block a user