77 lines
2.0 KiB
JavaScript
77 lines
2.0 KiB
JavaScript
|
|
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 QUERY_FILE = path.join(process.env.TD_TASK_DIR, "query");
|
|
const LOG_FILE = path.join(process.env.TD_TASK_DIR, "log");
|
|
|
|
if (fs.existsSync(LOG_FILE)) {
|
|
fs.rmSync(LOG_FILE)
|
|
}
|
|
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 saveQueryFile(content) {
|
|
fs.writeFileSync(QUERY_FILE, content);
|
|
return QUERY_FILE;
|
|
};
|
|
export function saveTasks(tasks) {
|
|
const updatedTasks = ensureIds(tasks);
|
|
fs.writeFileSync(TASK_FILE, JSON.stringify(updatedTasks, null, 2));
|
|
};
|
|
|
|
export function addLog(str) {
|
|
fs.appendFileSync(LOG_FILE, `\n${str}`);
|
|
}
|
|
|
|
|
|
export function readLog() {
|
|
return fs.readFileSync(LOG_FILE, "utf8");
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|