import fs from "fs"; import path from "path"; if (!process.env.TD_TASK_DIR) { throw new Error("Environment variable TD_TASK_DIR is not defined"); } const TASK_FILE = path.join(process.env.TD_TASK_DIR, "tasks.json"); const ensureIds = (tasks) => { return tasks.map(task => { if (!task.id) { task.id = randomId(); } if (task.subtasks && task.subtasks.length > 0) { task.subtasks = ensureIds(task.subtasks); } return task; }); }; export function loadTasks() { try { return ensureIds(JSON.parse(fs.readFileSync(TASK_FILE, "utf8"))); } catch { return []; } }; export function saveTasks(tasks) { const updatedTasks = ensureIds(tasks); fs.writeFileSync(TASK_FILE, JSON.stringify(updatedTasks, null, 2)); }; import { execSync } from "child_process"; export function commitAndPushTasks(log) { const currentDateTime = new Date().toISOString(); const commitMessage = `Update tasks.json - ${currentDateTime}`; try { execSync(`git -C ${process.env.TD_TASK_DIR} add tasks.json`, { stdio: "pipe" }); execSync(`git -C ${process.env.TD_TASK_DIR} commit -m "${commitMessage}"`, { stdio: "pipe" }); execSync(`git -C ${process.env.TD_TASK_DIR} push origin`, { stdio: "pipe" }); } catch (error) { log("Failed to commit and push tasks.json:", error); } } export function pullTasks() { try { execSync(`git -C ${process.env.TD_TASK_DIR} restore tasks.json`, { stdio: "pipe" }); execSync(`git -C ${process.env.TD_TASK_DIR} pull`, { stdio: "pipe" }); } catch (error) { log("Failed to pull tasks.json:", error); } }