log to file
This commit is contained in:
parent
c1c2f3c6f0
commit
24286a563f
@ -3,7 +3,7 @@ import React, { useState, useEffect } from "react";
|
|||||||
import { render, Box, Text, useApp, useInput } from "ink";
|
import { render, Box, Text, useApp, useInput } from "ink";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import { execSync, spawn } from "child_process";
|
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 { Tabs, Tab } from 'ink-tab';
|
||||||
import Scrollbar from "./scrollbar.js";
|
import Scrollbar from "./scrollbar.js";
|
||||||
|
|
||||||
@ -66,7 +66,7 @@ export default function TaskApp() {
|
|||||||
const [tasks, setTasks] = useState(loadTasks());
|
const [tasks, setTasks] = useState(loadTasks());
|
||||||
const [taskPath, setTaskPath] = useState([0]);
|
const [taskPath, setTaskPath] = useState([0]);
|
||||||
const [activeTabName, setActiveTabName] = useState(null);
|
const [activeTabName, setActiveTabName] = useState(null);
|
||||||
const [consoleLog, setConsole] = useState([]);
|
const [consoleLog, setConsole] = useState("");
|
||||||
const [cutTask, setCutTask] = useState(null);
|
const [cutTask, setCutTask] = useState(null);
|
||||||
const focusedIdx = last(taskPath)
|
const focusedIdx = last(taskPath)
|
||||||
const [visibleSubtasks, setVisibleSubtasks] = useState(currentSubtasks(tasks, taskPath));
|
const [visibleSubtasks, setVisibleSubtasks] = useState(currentSubtasks(tasks, taskPath));
|
||||||
@ -84,7 +84,8 @@ export default function TaskApp() {
|
|||||||
}
|
}
|
||||||
return e;
|
return e;
|
||||||
}).join(" ");
|
}).join(" ");
|
||||||
setConsole(prev => [...prev, x]);
|
addLog(x)
|
||||||
|
setConsole(readLog() || "");
|
||||||
}
|
}
|
||||||
const focusTask = (index) => {
|
const focusTask = (index) => {
|
||||||
log("Focus task:", 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>
|
</Box>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -2,58 +2,69 @@
|
|||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
if (!process.env.TD_TASK_DIR) {
|
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 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) => {
|
const ensureIds = (tasks) => {
|
||||||
return tasks.map(task => {
|
return tasks.map(task => {
|
||||||
if (!task.id) {
|
if (!task.id) {
|
||||||
task.id = randomId();
|
task.id = randomId();
|
||||||
}
|
}
|
||||||
if (task.subtasks && task.subtasks.length > 0) {
|
if (task.subtasks && task.subtasks.length > 0) {
|
||||||
task.subtasks = ensureIds(task.subtasks);
|
task.subtasks = ensureIds(task.subtasks);
|
||||||
}
|
}
|
||||||
return task;
|
return task;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export function loadTasks() {
|
export function loadTasks() {
|
||||||
try {
|
try {
|
||||||
return ensureIds(JSON.parse(fs.readFileSync(TASK_FILE, "utf8")));
|
return ensureIds(JSON.parse(fs.readFileSync(TASK_FILE, "utf8")));
|
||||||
} catch {
|
} catch {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export function saveTasks(tasks) {
|
export function saveTasks(tasks) {
|
||||||
const updatedTasks = ensureIds(tasks);
|
const updatedTasks = ensureIds(tasks);
|
||||||
fs.writeFileSync(TASK_FILE, JSON.stringify(updatedTasks, null, 2));
|
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";
|
import { execSync } from "child_process";
|
||||||
|
|
||||||
export function commitAndPushTasks(log) {
|
export function commitAndPushTasks(log) {
|
||||||
const currentDateTime = new Date().toISOString();
|
const currentDateTime = new Date().toISOString();
|
||||||
const commitMessage = `Update tasks.json - ${currentDateTime}`;
|
const commitMessage = `Update tasks.json - ${currentDateTime}`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
execSync(`git -C ${process.env.TD_TASK_DIR} add tasks.json`, { stdio: "pipe" });
|
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} commit -m "${commitMessage}"`, { stdio: "pipe" });
|
||||||
execSync(`git -C ${process.env.TD_TASK_DIR} push origin`, { stdio: "pipe" });
|
execSync(`git -C ${process.env.TD_TASK_DIR} push origin`, { stdio: "pipe" });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log("Failed to commit and push tasks.json:", error);
|
log("Failed to commit and push tasks.json:", error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export function pullTasks() {
|
export function pullTasks() {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
execSync(`git -C ${process.env.TD_TASK_DIR} restore tasks.json`, { stdio: "pipe" });
|
execSync(`git -C ${process.env.TD_TASK_DIR} restore tasks.json`, { stdio: "pipe" });
|
||||||
execSync(`git -C ${process.env.TD_TASK_DIR} pull`, { stdio: "pipe" });
|
execSync(`git -C ${process.env.TD_TASK_DIR} pull`, { stdio: "pipe" });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log("Failed to pull tasks.json:", error);
|
log("Failed to pull tasks.json:", error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user