added history

This commit is contained in:
Patryk Koreń
2026-01-27 21:27:36 +01:00
parent bb2b31bb61
commit cbf39d2bae
5 changed files with 44 additions and 4 deletions

31
src/util/history.ts Normal file
View File

@@ -0,0 +1,31 @@
import path from "node:path";
import fs from "node:fs";
import { HISTORY_DIR_PATH } from "../main";
export async function add_to_history(history: HistoryObject) {
const today = new Date();
const day = String(today.getDate()).padStart(2, '0');
const month = String(today.getMonth() + 1).padStart(2, '0');
const year = today.getFullYear();
const file_name = `${day}-${month}-${year}.json`;
const file_path = path.join(HISTORY_DIR_PATH, file_name)
if (!fs.existsSync(HISTORY_DIR_PATH)) {
fs.mkdirSync(HISTORY_DIR_PATH);
}
let data = [];
try {
const content = fs.readFileSync(file_path, 'utf8');
data = JSON.parse(content);
} catch (err) {
data = [];
}
data.push(history)
fs.writeFileSync(file_path, JSON.stringify(data, null, 2));
}