refactor(cli): use taskService in task command

This commit is contained in:
chahinebrini 2026-06-25 12:22:37 +02:00
parent 4a8f5cbb8e
commit b4874afbe9

View File

@ -1,10 +1,6 @@
import { input, select } from '@inquirer/prompts'; import { input, select } from '@inquirer/prompts';
import { join } from 'path'; import { createTask, listTasks, getTask, claimTask, doneTask } from '../../core/services/taskService.js';
import { getEntityDir } from '../../core/paths.js'; import type { Task } from '../../core/schema.js';
import { getNextId } from '../../core/counter.js';
import { readEntity, writeEntity } from '../../core/files.js';
import { TaskSchema, type Task } from '../../core/schema.js';
import { Index } from '../../core/index.js';
export async function taskCreate(cwd: string, options: Partial<Task> = {}): Promise<void> { export async function taskCreate(cwd: string, options: Partial<Task> = {}): Promise<void> {
const title = options.title ?? await input({ message: 'Task title:' }); const title = options.title ?? await input({ message: 'Task title:' });
@ -19,84 +15,34 @@ export async function taskCreate(cwd: string, options: Partial<Task> = {}): Prom
}); });
const priority = options.priority ?? 'medium'; const priority = options.priority ?? 'medium';
const now = new Date().toISOString(); const task = createTask(cwd, { title, role, priority });
const task: Task = TaskSchema.parse({
id: getNextId(cwd, 'task'),
title,
description: options.description ?? '',
status: 'open',
priority,
role,
createdAt: now,
updatedAt: now,
});
const filePath = join(getEntityDir(cwd, 'tasks'), `${task.id}.md`);
writeEntity(filePath, task, `# ${task.title}\n\n${task.description}`);
const index = new Index(cwd);
index.upsert(toIndexEntry(task, filePath));
index.close();
console.log(`Created ${task.id}: ${task.title}`); console.log(`Created ${task.id}: ${task.title}`);
} }
export function taskList(cwd: string, filters?: { status?: string; role?: string }): void { export function taskList(cwd: string, filters?: { status?: string; role?: string }): void {
const index = new Index(cwd); const tasks = listTasks(cwd, filters);
const tasks = index.list('task', filters);
index.close();
if (tasks.length === 0) { if (tasks.length === 0) {
console.log('No tasks found.'); console.log('No tasks found.');
return; return;
} }
for (const t of tasks) { for (const t of tasks) {
console.log(`${t.id} [${t.status}] (${t.role ?? 'unassigned'}) ${t.title}`); console.log(`${t.id} [${t.status}] (${t.role ?? 'unassigned'}) ${t.title}`);
} }
} }
export function taskShow(cwd: string, id: string): void { export function taskShow(cwd: string, id: string): void {
const filePath = join(getEntityDir(cwd, 'tasks'), `${id}.md`); const { task, body } = getTask(cwd, id);
const { frontmatter, body } = readEntity(filePath); console.log(`# ${task.title}`);
console.log(`# ${frontmatter.title}`); console.log(`Status: ${task.status} | Role: ${task.role ?? '-'} | Priority: ${task.priority}`);
console.log(`Status: ${frontmatter.status} | Role: ${frontmatter.role ?? '-'} | Priority: ${frontmatter.priority}`);
console.log('\n' + body); console.log('\n' + body);
} }
export function taskClaim(cwd: string, id: string, agentName: string): void { export function taskClaim(cwd: string, id: string, agentName: string): void {
updateTask(cwd, id, { status: 'in_progress', assignedTo: agentName }); claimTask(cwd, id, agentName);
console.log(`${id} claimed by ${agentName}.`); console.log(`${id} claimed by ${agentName}.`);
} }
export function taskDone(cwd: string, id: string): void { export function taskDone(cwd: string, id: string): void {
updateTask(cwd, id, { status: 'done' }); doneTask(cwd, id);
console.log(`${id} marked as done.`); console.log(`${id} marked as done.`);
} }
function updateTask(cwd: string, id: string, patch: Partial<Task>): void {
const filePath = join(getEntityDir(cwd, 'tasks'), `${id}.md`);
const { frontmatter, body } = readEntity(filePath);
const task = TaskSchema.parse({ ...frontmatter, ...patch, updatedAt: new Date().toISOString() });
writeEntity(filePath, task, body);
const index = new Index(cwd);
index.upsert(toIndexEntry(task, filePath));
index.close();
}
function toIndexEntry(task: Task, filePath: string) {
return {
id: task.id,
type: 'task',
title: task.title,
content: `${task.description} ${task.tags.join(' ')}`,
filePath,
createdAt: task.createdAt,
updatedAt: task.updatedAt,
status: task.status,
role: task.role,
assignedTo: task.assignedTo,
tags: JSON.stringify(task.tags),
};
}