Array of log file information with name and last modified date
export function getAvailableLogFiles(): {
name: string;
mtime: number;
displayName: string;
}[] {
try {
if (!fs.existsSync(logsPath)) {
fs.mkdirSync(logsPath, { recursive: true });
return [
{
name: "latest.log",
mtime: Date.now(),
displayName: "Current Session",
},
];
}
// Get all log files including latest.log
const files = fs
.readdirSync(logsPath)
.filter((file) => file.endsWith(".log"))
.map((file) => {
const stats = fs.statSync(path.join(logsPath, file));
const date = new Date(stats.mtime);
// Create a friendly display name
const displayName =
file === "latest.log"
? "Current Session"
: `${date.toLocaleDateString()} ${date.toLocaleTimeString()}`;
return {
name: file,
mtime: stats.mtime.getTime(),
displayName,
};
})
.sort((a, b) => b.mtime - a.mtime); // Sort newest first
return files;
} catch (error) {
console.error("Error getting available log files:", error);
return [
{ name: "latest.log", mtime: Date.now(), displayName: "Current Session" },
];
}
}
Gets a list of available log files