added history
This commit is contained in:
31
src/util/history.ts
Normal file
31
src/util/history.ts
Normal 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));
|
||||
}
|
||||
Reference in New Issue
Block a user