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}`);
}
// Read the tasks file
// Read the tasks file and complexity report
const data = readJSON(tasksPath);
const complexityReport = readComplexityReport();
if (!data || !data.tasks) {
throw new Error(
`No valid tasks found in ${tasksPath}. The file may be corrupted or have an invalid format.`
);
}
// Find the specific task to update
const taskToUpdate = data.tasks.find((task) => task.id === taskId);
// Find the specific task to update, passing complexity report
const taskToUpdate = findTaskById(data.tasks, taskId, complexityReport);
if (!taskToUpdate) {
throw new Error(
`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
const isMcpMode = !!options?.mcpLog;
// Read complexity report once
const complexityReport = readComplexityReport();
log('info', `Reading tasks from ${tasksPath}...`);
const data = readJSON(tasksPath);
@@ -1402,9 +1406,9 @@ function generateTaskFiles(tasksPath, outputDir, options = {}) {
content += `# Title: ${task.title}\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) {
content += `# Dependencies: ${formatDependenciesWithStatus(task.dependencies, data.tasks, false)}\n`;
content += `# Dependencies: ${formatDependenciesWithStatus(task.dependencies, data.tasks, false, complexityReport)}\n`;
} else {
content += '# Dependencies: None\n';
}
@@ -1562,7 +1566,8 @@ async function setTaskStatus(tasksPath, taskIdInput, newStatus, options = {}) {
// Display success message - only in CLI mode
if (!isMcpMode) {
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;
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: true,
@@ -2998,6 +3006,9 @@ function clearSubtasks(tasksPath, taskIds) {
process.exit(1);
}
// Read complexity report once
const complexityReport = readComplexityReport();
console.log(
boxen(chalk.white.bold('Clearing Subtasks'), {
padding: 1,
@@ -3029,7 +3040,8 @@ function clearSubtasks(tasksPath, taskIds) {
return;
}
const task = data.tasks.find((t) => t.id === id);
// Pass complexityReport to findTaskById
const task = findTaskById(data.tasks, id, complexityReport);
if (!task) {
log('error', `Task ${id} not found`);
return;

View File

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

View File

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