31 lines
1009 B
JavaScript
Executable File
31 lines
1009 B
JavaScript
Executable File
const fs = require('fs');
|
|
const path = require('path');
|
|
const { HISTORY_FILE, HISTORY_LIMIT } = require('./config');
|
|
|
|
function loadHistory() {
|
|
try {
|
|
if (fs.existsSync(HISTORY_FILE)) return JSON.parse(fs.readFileSync(HISTORY_FILE, 'utf-8'));
|
|
} catch {}
|
|
return [];
|
|
}
|
|
|
|
function saveHistory(history) {
|
|
fs.mkdirSync(path.dirname(HISTORY_FILE), { recursive: true });
|
|
fs.writeFileSync(HISTORY_FILE, JSON.stringify(history, null, 2), 'utf-8');
|
|
}
|
|
|
|
function addHistory(entry) {
|
|
const history = loadHistory();
|
|
history.unshift({ ...entry, timestamp: new Date().toISOString() });
|
|
if (history.length > HISTORY_LIMIT) history.length = HISTORY_LIMIT;
|
|
saveHistory(history);
|
|
}
|
|
|
|
function formatTime(isoStr) {
|
|
const date = new Date(isoStr);
|
|
const pad = value => String(value).padStart(2, '0');
|
|
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}`;
|
|
}
|
|
|
|
module.exports = { loadHistory, saveHistory, addHistory, formatTime };
|