import { appendFileSync, existsSync } from 'fs'; const filename = 'logs.csv', fileExist = existsSync(filename); console.log(filename, fileExist); if (!fileExist) appendFileSync(filename, ['Date', 'Method', 'Domain', 'Path', 'Status'].join(',') + '\n', 'utf8'); export const log = (method: string, response: Response) => { const { url, status } = response, { host, pathname } = new URL(url); appendFileSync(filename, [isoDateInTimeZone(), method, host, pathname, status].join(',') + '\n', 'utf8'); }; function isoDateInTimeZone(timeZone = 'America/Buenos_Aires', date = new Date()) { const parts = new Intl.DateTimeFormat("en-CA", { timeZone, year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit", fractionalSecondDigits: 3, hour12: false, timeZoneName: "shortOffset", }).formatToParts(date), map = Object.fromEntries(parts.map(p => [p.type, p.value])), offset = map.timeZoneName.replace("GMT", ""); return `${map.year}-${map.month}-${map.day}T` + `${map.hour}:${map.minute}:${map.second}.` + `${map.fractionalSecond}${offset}`; }