log to file

This commit is contained in:
Kacper Marzecki 2025-03-16 22:38:44 +01:00
parent c1c2f3c6f0
commit 24286a563f
2 changed files with 48 additions and 36 deletions

View File

@ -3,7 +3,7 @@ import React, { useState, useEffect } from "react";
import { render, Box, Text, useApp, useInput } from "ink";
import fs from "fs";
import { execSync, spawn } from "child_process";
import { loadTasks, saveTasks, commitAndPushTasks, pullTasks } from "./tasks.js";
import { loadTasks, saveTasks, commitAndPushTasks, pullTasks, addLog, readLog } from "./tasks.js";
import { Tabs, Tab } from 'ink-tab';
import Scrollbar from "./scrollbar.js";
@ -66,7 +66,7 @@ export default function TaskApp() {
const [tasks, setTasks] = useState(loadTasks());
const [taskPath, setTaskPath] = useState([0]);
const [activeTabName, setActiveTabName] = useState(null);
const [consoleLog, setConsole] = useState([]);
const [consoleLog, setConsole] = useState("");
const [cutTask, setCutTask] = useState(null);
const focusedIdx = last(taskPath)
const [visibleSubtasks, setVisibleSubtasks] = useState(currentSubtasks(tasks, taskPath));
@ -84,7 +84,8 @@ export default function TaskApp() {
}
return e;
}).join(" ");
setConsole(prev => [...prev, x]);
addLog(x)
setConsole(readLog() || "");
}
const focusTask = (index) => {
log("Focus task:", index);
@ -390,7 +391,7 @@ export default function TaskApp() {
)}
{activeTabName === 'console' && consoleLog.map(it => <Text>{it}</Text>)}
{activeTabName === 'console' && consoleLog.split("\n").map(it => <Text>{it}</Text>)}
</Box>
);
};

View File

@ -2,58 +2,69 @@
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");
throw new Error("Environment variable TD_TASK_DIR is not defined");
}
const TASK_FILE = path.join(process.env.TD_TASK_DIR, "tasks.json");
const LOG_FILE = path.join(process.env.TD_TASK_DIR, "log");
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;
});
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 [];
}
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));
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}`;
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);
}
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);
}
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);
}
}