import fs from "fs"; import { Task } from "../types/task"; import { Config } from "../types/config"; import dayjs from "dayjs"; export function complete(config: Config, profile: string, task: Task) { const profileConfig = config.profiles[profile]; if (profileConfig === undefined) { throw new Error(`Unknown profile ${profile}`); } if (!profileConfig.completion?.enable) { return; } if (task.recurrenceRule) { throw new Error('Recurrence tasks are not supported for now'); } const data = fs.readFileSync(task.sourceFile, 'utf8').split(/\n/); const output = []; for (const [number0, line] of data.entries()) { if (task.sourceLine === number0 + 1) { if (task.source !== line) { throw new Error(`Cannot complete task, file has been changed since last scan. Rememberred line: ${task.source}, current line: ${line}.`); } const taskStr = line.replace(/^-(\s+)\[.\]/, `-$1[${profileConfig.completion?.status ?? 'x'}]`); output.push(`${taskStr} ✅ ${dayjs().format("YYYY-MM-DD")}`); } else { output.push(line); } } fs.writeFileSync(task.sourceFile, output.join("\n")); }