diff --git a/debug/.gitignore b/debug/.gitignore new file mode 100644 index 0000000..2211df6 --- /dev/null +++ b/debug/.gitignore @@ -0,0 +1 @@ +*.txt diff --git a/scripts/debug.ts b/scripts/debug.ts index 5f43cf8..4e46445 100644 --- a/scripts/debug.ts +++ b/scripts/debug.ts @@ -3,8 +3,12 @@ * * Usage: * npm run debug -- --park greatadventure --date 2026-07-04 + * + * Output is printed to the terminal and saved to debug/{parkId}_{date}.txt */ +import fs from "fs"; +import path from "path"; import { openDb, getApiId } from "../lib/db"; import { PARKS } from "../lib/parks"; import { scrapeMonthRaw } from "../lib/scrapers/sixflags"; @@ -57,30 +61,38 @@ async function main() { process.exit(1); } - console.log(`\nPark : ${park.name} (${park.id})`); - console.log(`API ID : ${apiId}`); - console.log(`Date : ${dateStr}`); - console.log(`\nFetching ${year}-${String(month).padStart(2, "0")} from API...\n`); + // Collect all output so we can write it to a file as well + const lines: string[] = []; + const out = (...args: string[]) => { + const line = args.join(" "); + lines.push(line); + console.log(line); + }; + + out(`Park : ${park.name} (${park.id})`); + out(`API ID : ${apiId}`); + out(`Date : ${dateStr}`); + out(`Fetched : ${new Date().toISOString()}`); + out(""); + out(`Fetching ${year}-${String(month).padStart(2, "0")} from API...`); const raw = await scrapeMonthRaw(apiId, year, month); - // Find the specific day in the response const targetDate = `${String(month).padStart(2, "0")}/${String(day).padStart(2, "0")}/${year}`; const dayData = raw.dates.find((d) => d.date === targetDate); if (!dayData) { console.error(`Date ${dateStr} not found in API response.`); - console.log(`\nDates returned by API:`); - for (const d of raw.dates) console.log(` ${d.date}`); + console.error(`Dates returned by API: ${raw.dates.map((d) => d.date).join(", ")}`); process.exit(1); } - // ── Raw API response for this day ───────────────────────────────────────── - console.log("── Raw API response ─────────────────────────────────────────"); - console.log(JSON.stringify(dayData, null, 2)); + // ── Raw API response ─────────────────────────────────────────────────────── + out(""); + out("── Raw API response ─────────────────────────────────────────"); + out(JSON.stringify(dayData, null, 2)); - // ── Parsed result (what the scraper stores) ─────────────────────────────── - console.log("\n── Parsed result ────────────────────────────────────────────"); + // ── Parsed result ────────────────────────────────────────────────────────── const operating = dayData.operatings?.find((o) => o.operatingTypeName === "Park") ?? dayData.operatings?.[0]; @@ -91,18 +103,26 @@ async function main() { : undefined; const isOpen = !dayData.isParkClosed && hoursLabel !== undefined; - console.log(` isParkClosed : ${dayData.isParkClosed}`); - console.log(` operatings : ${dayData.operatings?.length ?? 0} entr${dayData.operatings?.length === 1 ? "y" : "ies"}`); + out(""); + out("── Parsed result ────────────────────────────────────────────"); + out(` isParkClosed : ${dayData.isParkClosed}`); + out(` operatings : ${dayData.operatings?.length ?? 0} entr${dayData.operatings?.length === 1 ? "y" : "ies"}`); if (operating) { - console.log(` selected : "${operating.operatingTypeName}" (${operating.items?.length ?? 0} item(s))`); + out(` selected : "${operating.operatingTypeName}" (${operating.items?.length ?? 0} item(s))`); if (item) { - console.log(` timeFrom : ${item.timeFrom} → ${fmt24(item.timeFrom)}`); - console.log(` timeTo : ${item.timeTo} → ${fmt24(item.timeTo)}`); + out(` timeFrom : ${item.timeFrom} → ${fmt24(item.timeFrom)}`); + out(` timeTo : ${item.timeTo} → ${fmt24(item.timeTo)}`); } } - console.log(` hoursLabel : ${hoursLabel ?? "(none)"}`); - console.log(` isOpen : ${isOpen}`); - console.log(); + out(` hoursLabel : ${hoursLabel ?? "(none)"}`); + out(` isOpen : ${isOpen}`); + + // ── Write to file ────────────────────────────────────────────────────────── + const debugDir = path.join(process.cwd(), "debug"); + fs.mkdirSync(debugDir, { recursive: true }); + const outPath = path.join(debugDir, `${parkId}_${dateStr}.txt`); + fs.writeFileSync(outPath, lines.join("\n") + "\n"); + console.log(`\nSaved to debug/${parkId}_${dateStr}.txt`); } main().catch((err) => {