feat: updated handling for findTaskById to take complexityReport as input

This commit is contained in:
Shrey Paharia
2025-04-19 13:09:14 +05:30
parent 8911bf4d49
commit e045a5268c
3 changed files with 47 additions and 19 deletions

View File

@@ -766,16 +766,17 @@ async function updateTaskById(
throw new Error(`Tasks file not found at path: ${tasksPath}`); throw new Error(`Tasks file not found at path: ${tasksPath}`);
} }
// Read the tasks file // Read the tasks file and complexity report
const data = readJSON(tasksPath); const data = readJSON(tasksPath);
const complexityReport = readComplexityReport();
if (!data || !data.tasks) { if (!data || !data.tasks) {
throw new Error( throw new Error(
`No valid tasks found in ${tasksPath}. The file may be corrupted or have an invalid format.` `No valid tasks found in ${tasksPath}. The file may be corrupted or have an invalid format.`
); );
} }
// Find the specific task to update // Find the specific task to update, passing complexity report
const taskToUpdate = data.tasks.find((task) => task.id === taskId); const taskToUpdate = findTaskById(data.tasks, taskId, complexityReport);
if (!taskToUpdate) { if (!taskToUpdate) {
throw new Error( throw new Error(
`Task with ID ${taskId} not found. Please verify the task ID and try again.` `Task with ID ${taskId} not found. Please verify the task ID and try again.`
@@ -1368,6 +1369,9 @@ function generateTaskFiles(tasksPath, outputDir, options = {}) {
// Determine if we're in MCP mode by checking for mcpLog // Determine if we're in MCP mode by checking for mcpLog
const isMcpMode = !!options?.mcpLog; const isMcpMode = !!options?.mcpLog;
// Read complexity report once
const complexityReport = readComplexityReport();
log('info', `Reading tasks from ${tasksPath}...`); log('info', `Reading tasks from ${tasksPath}...`);
const data = readJSON(tasksPath); const data = readJSON(tasksPath);
@@ -1402,9 +1406,9 @@ function generateTaskFiles(tasksPath, outputDir, options = {}) {
content += `# Title: ${task.title}\n`; content += `# Title: ${task.title}\n`;
content += `# Status: ${task.status || 'pending'}\n`; content += `# Status: ${task.status || 'pending'}\n`;
// Format dependencies with their status // Format dependencies with their status, passing complexity report
if (task.dependencies && task.dependencies.length > 0) { if (task.dependencies && task.dependencies.length > 0) {
content += `# Dependencies: ${formatDependenciesWithStatus(task.dependencies, data.tasks, false)}\n`; content += `# Dependencies: ${formatDependenciesWithStatus(task.dependencies, data.tasks, false, complexityReport)}\n`;
} else { } else {
content += '# Dependencies: None\n'; content += '# Dependencies: None\n';
} }
@@ -1562,7 +1566,8 @@ async function setTaskStatus(tasksPath, taskIdInput, newStatus, options = {}) {
// Display success message - only in CLI mode // Display success message - only in CLI mode
if (!isMcpMode) { if (!isMcpMode) {
for (const id of updatedTasks) { for (const id of updatedTasks) {
const task = findTaskById(data.tasks, id); // Pass complexityReport to findTaskById
const task = findTaskById(data.tasks, id, complexityReport);
const taskName = task ? task.title : id; const taskName = task ? task.title : id;
console.log( console.log(
@@ -1577,6 +1582,9 @@ async function setTaskStatus(tasksPath, taskIdInput, newStatus, options = {}) {
} }
} }
// Read complexity report once
const complexityReport = readComplexityReport();
// Return success value for programmatic use // Return success value for programmatic use
return { return {
success: true, success: true,
@@ -2998,6 +3006,9 @@ function clearSubtasks(tasksPath, taskIds) {
process.exit(1); process.exit(1);
} }
// Read complexity report once
const complexityReport = readComplexityReport();
console.log( console.log(
boxen(chalk.white.bold('Clearing Subtasks'), { boxen(chalk.white.bold('Clearing Subtasks'), {
padding: 1, padding: 1,
@@ -3029,7 +3040,8 @@ function clearSubtasks(tasksPath, taskIds) {
return; return;
} }
const task = data.tasks.find((t) => t.id === id); // Pass complexityReport to findTaskById
const task = findTaskById(data.tasks, id, complexityReport);
if (!task) { if (!task) {
log('error', `Task ${id} not found`); log('error', `Task ${id} not found`);
return; return;

View File

@@ -270,12 +270,14 @@ function getStatusWithColor(status, forTable = false) {
* @param {Array} dependencies - Array of dependency IDs * @param {Array} dependencies - Array of dependency IDs
* @param {Array} allTasks - Array of all tasks * @param {Array} allTasks - Array of all tasks
* @param {boolean} forConsole - Whether the output is for console display * @param {boolean} forConsole - Whether the output is for console display
* @param {Object|null} complexityReport - Optional pre-loaded complexity report
* @returns {string} Formatted dependencies string * @returns {string} Formatted dependencies string
*/ */
function formatDependenciesWithStatus( function formatDependenciesWithStatus(
dependencies, dependencies,
allTasks, allTasks,
forConsole = false forConsole = false,
complexityReport = null // Add complexityReport parameter
) { ) {
if ( if (
!dependencies || !dependencies ||
@@ -338,8 +340,8 @@ function formatDependenciesWithStatus(
const numericDepId = const numericDepId =
typeof depId === 'string' ? parseInt(depId, 10) : depId; typeof depId === 'string' ? parseInt(depId, 10) : depId;
// Look up the task using the numeric ID // Look up the task using the numeric ID, passing the complexity report
const depTask = findTaskById(allTasks, numericDepId); const depTask = findTaskById(allTasks, numericDepId, complexityReport);
if (!depTask) { if (!depTask) {
return forConsole return forConsole
@@ -680,6 +682,9 @@ async function displayNextTask(tasksPath) {
process.exit(1); process.exit(1);
} }
// Read complexity report once
const complexityReport = readComplexityReport();
// Find the next task // Find the next task
const nextTask = findNextTask(data.tasks); const nextTask = findNextTask(data.tasks);
@@ -747,7 +752,12 @@ async function displayNextTask(tasksPath) {
], ],
[ [
chalk.cyan.bold('Dependencies:'), chalk.cyan.bold('Dependencies:'),
formatDependenciesWithStatus(nextTask.dependencies, data.tasks, true) formatDependenciesWithStatus(
nextTask.dependencies,
data.tasks,
true,
complexityReport
) // Pass complexityReport
], ],
[chalk.cyan.bold('Description:'), nextTask.description] [chalk.cyan.bold('Description:'), nextTask.description]
); );
@@ -929,8 +939,11 @@ async function displayTaskById(tasksPath, taskId) {
process.exit(1); process.exit(1);
} }
// Find the task by ID // Read complexity report once
const task = findTaskById(data.tasks, taskId); const complexityReport = readComplexityReport();
// Find the task by ID, passing the complexity report
const task = findTaskById(data.tasks, taskId, complexityReport);
if (!task) { if (!task) {
console.log( console.log(
@@ -1157,7 +1170,12 @@ async function displayTaskById(tasksPath, taskId) {
[chalk.cyan.bold('Priority:'), priorityColor(task.priority || 'medium')], [chalk.cyan.bold('Priority:'), priorityColor(task.priority || 'medium')],
[ [
chalk.cyan.bold('Dependencies:'), chalk.cyan.bold('Dependencies:'),
formatDependenciesWithStatus(task.dependencies, data.tasks, true) formatDependenciesWithStatus(
task.dependencies,
data.tasks,
true,
complexityReport
) // Pass complexityReport
], ],
[chalk.cyan.bold('Description:'), task.description] [chalk.cyan.bold('Description:'), task.description]
); );

View File

@@ -240,9 +240,10 @@ function formatTaskId(id) {
* Finds a task by ID in the tasks array * Finds a task by ID in the tasks array
* @param {Array} tasks - The tasks array * @param {Array} tasks - The tasks array
* @param {string|number} taskId - The task ID to find * @param {string|number} taskId - The task ID to find
* @param {Object|null} complexityReport - Optional pre-loaded complexity report
* @returns {Object|null} The task object or null if not found * @returns {Object|null} The task object or null if not found
*/ */
function findTaskById(tasks, taskId) { function findTaskById(tasks, taskId, complexityReport = null) {
if (!taskId || !tasks || !Array.isArray(tasks)) { if (!taskId || !tasks || !Array.isArray(tasks)) {
return null; return null;
} }
@@ -277,10 +278,7 @@ function findTaskById(tasks, taskId) {
} }
// If we found a task, check for complexity data // If we found a task, check for complexity data
if (taskResult) { if (taskResult && complexityReport) {
// Try to read complexity report
const complexityReport = readComplexityReport();
if (complexityReport && complexityReport.complexityAnalysis) { if (complexityReport && complexityReport.complexityAnalysis) {
// For a main task, look for a direct match // For a main task, look for a direct match
if (!taskResult.isSubtask) { if (!taskResult.isSubtask) {