Skip to content

Commit 527f4d2

Browse files
committed
feat(tasks): add unsafe but fast option to delete dependencies
1 parent d3d23d7 commit 527f4d2

3 files changed

Lines changed: 70 additions & 5 deletions

File tree

src/core/taskmaster/TaskMaster.ts

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,7 @@ export class TaskMaster {
977977
subtaskId: number,
978978
parentId: number,
979979
): Promise<void> {
980-
await this.deleteAllDepsFromTaskAsync(subtaskId.toString());
980+
await this.deleteAllDepsSafelyFromTaskAsync(subtaskId.toString());
981981
await this._executeCommandAsync(
982982
`Converting task ${subtaskId} to subtask of ${parentId}...`,
983983
`Task ${subtaskId} converted to subtask successfully!`,
@@ -995,6 +995,7 @@ export class TaskMaster {
995995
public async convertSubtaskToTaskAsync(
996996
hierarchicalId: string,
997997
): Promise<void> {
998+
await this.deleteAllDepsSafelyFromTaskAsync(hierarchicalId);
998999
await this._executeCommandAsync(
9991000
`Converting subtask ${hierarchicalId} to task...`,
10001001
`Subtask ${hierarchicalId} converted to task successfully!`,
@@ -1098,7 +1099,7 @@ export class TaskMaster {
10981099
* @description Clears all dependencies for the specified task or subtask.
10991100
* @param taskId The task ID or hierarchical ID of the subtask
11001101
*/
1101-
public async deleteAllDepsFromTaskAsync(taskId: string): Promise<void> {
1102+
public async deleteAllDepsSafelyFromTaskAsync(taskId: string): Promise<void> {
11021103
const tasks = await this.getTasksContentAsync();
11031104
const dependencyIds = await this._getAllDependenciesAsync(tasks, taskId);
11041105

@@ -1126,6 +1127,58 @@ export class TaskMaster {
11261127
}
11271128
}
11281129

1130+
// TODO: done
1131+
/**
1132+
* @description Deletes all dependencies for the specified task or subtask without using external commands.
1133+
* This is a faster but less safe method that directly modifies the tasks.json file.
1134+
* @param taskId The task ID or hierarchical ID of the subtask
1135+
*/
1136+
public async deleteAllDepsUnsafeFromTaskAsync(taskId: string): Promise<void> {
1137+
const oraOptions = {
1138+
text: `Clearing dependencies from task ${taskId}...`,
1139+
successText: chalk.bgGreen(
1140+
`Dependencies cleared from task ${taskId} successfully!`,
1141+
),
1142+
failText: chalk.bgRed(`Failed to clear dependencies from task ${taskId}`),
1143+
};
1144+
1145+
await oraPromise(async () => {
1146+
const tasks = await this.getTasksContentAsync();
1147+
1148+
if (!taskId.includes(".")) {
1149+
// It's a main task
1150+
const idNum = Number.parseInt(taskId, 10);
1151+
const task = tasks.master.tasks.find((t) => t.id === idNum);
1152+
if (!task) {
1153+
throw new Error(`Task not found: ${taskId}`);
1154+
}
1155+
task.dependencies = [];
1156+
} else {
1157+
// It's a subtask
1158+
const parts = taskId.split(".");
1159+
const parentId = Number.parseInt(parts[0], 10);
1160+
const subtaskIndex = Number.parseInt(parts[1], 10) - 1;
1161+
const parentTask = tasks.master.tasks.find((t) => t.id === parentId);
1162+
if (!parentTask) {
1163+
throw new Error(`Parent task not found for subtask: ${taskId}`);
1164+
}
1165+
1166+
if (
1167+
!parentTask.subtasks ||
1168+
subtaskIndex < 0 ||
1169+
subtaskIndex >= parentTask.subtasks.length
1170+
) {
1171+
throw new Error(`Subtask not found: ${taskId}`);
1172+
}
1173+
1174+
const subtask = parentTask.subtasks[subtaskIndex];
1175+
subtask.dependencies = [];
1176+
}
1177+
1178+
await writeFile(this._tasksFilePath, JSON.stringify(tasks, null, 2));
1179+
}, oraOptions);
1180+
}
1181+
11291182
// ==============================================
11301183
// Dependencies Methods
11311184
// ==============================================

src/core/taskmaster/exec.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,18 @@ export async function tmaiManageAsync() {
320320
await tmai.listAsync(tasks, TASKS_STATUSES.join(","), true, true);
321321
break;
322322
}
323-
case "tmai-deletealldepsfromtask": {
323+
case "tmai-deletealldepssafelyfromtask": {
324324
await tmai.listAsync(tasks, TASKS_STATUSES.join(","), true, true);
325325
const taskId = await askHybridTaskIdAsync(mainIDs, subtasksIDs);
326-
await tmai.deleteAllDepsFromTaskAsync(taskId);
326+
await tmai.deleteAllDepsSafelyFromTaskAsync(taskId);
327+
tasks = await tmai.getTasksContentAsync();
328+
await tmai.listAsync(tasks, TASKS_STATUSES.join(","), true, true);
329+
break;
330+
}
331+
case "tmai-deletealldepsunsafefromtask": {
332+
await tmai.listAsync(tasks, TASKS_STATUSES.join(","), true, true);
333+
const taskId = await askHybridTaskIdAsync(mainIDs, subtasksIDs);
334+
await tmai.deleteAllDepsUnsafeFromTaskAsync(taskId);
327335
tasks = await tmai.getTasksContentAsync();
328336
await tmai.listAsync(tasks, TASKS_STATUSES.join(","), true, true);
329337
break;

src/prompt.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,11 @@ export const tmaiDeleteTasksMenu_prompt = [
288288
},
289289
{
290290
name: `${emoji.get("wastebasket")} Delete all dependencies from a task`,
291-
value: "tmai-deletealldepsfromtask",
291+
value: "tmai-deletealldepssafelyfromtask",
292+
},
293+
{
294+
name: `${emoji.get("wastebasket")} Delete all dependencies from a task (unsafe but fast)`,
295+
value: "tmai-deletealldepsunsafefromtask",
292296
},
293297
],
294298
},

0 commit comments

Comments
 (0)