diff --git a/scripts/cars-import.ts b/scripts/cars-import.ts index 7067714..83a4a5e 100644 --- a/scripts/cars-import.ts +++ b/scripts/cars-import.ts @@ -4,6 +4,7 @@ // bun scripts/cars-import.ts --dry-run fetch + build only, print a summary // bun scripts/cars-import.ts --cache /tmp/cars reuse raw downloads from that folder // bun scripts/cars-import.ts --dry-run --out x.json also dump the built catalogue +// bun scripts/cars-import.ts --renumber reassign Dex numbers from #1 (oldest first) // // Sources (no API keys): // - fueleconomy.gov vehicles.csv: every US EPA-tested car since 1984 (make, base model, @@ -26,6 +27,8 @@ import { join } from 'node:path'; const args = process.argv.slice(2); const dryRun = args.includes('--dry-run'); +// --renumber: hand out Dex numbers from #1 again instead of keeping existing ones. +const renumber = args.includes('--renumber'); const cacheDir = args.includes('--cache') ? args[args.indexOf('--cache') + 1] : null; const outFile = args.includes('--out') ? args[args.indexOf('--out') + 1] : null; @@ -927,12 +930,12 @@ async function write(brands: ReturnType) { const existing = await tx<{ id: number; dex: number; brand_id: number; slug: string }[]>` select id, dex, brand_id, slug from cars.models`; const byKey = new Map(existing.map((e) => [`${e.brand_id}|${e.slug}`, e])); - let nextDex = Math.max(0, ...existing.map((e) => e.dex)) + 1; + let nextDex = renumber ? 1 : Math.max(0, ...existing.map((e) => e.dex)) + 1; const all = brands .flatMap((b) => b.models.map((m) => ({ b, m, brandId: brandIds.get(b.slug)! }))) .sort( (x, y) => - x.m.firstYear - y.m.firstYear || + (x.m.firstYear ?? 9999) - (y.m.firstYear ?? 9999) || x.b.name.localeCompare(y.b.name) || x.m.name.localeCompare(y.m.name) ); @@ -942,7 +945,7 @@ async function write(brands: ReturnType) { await tx`delete from cars.variants`; let variantCount = 0; for (const { m, brandId } of all) { - const prev = byKey.get(`${brandId}|${m.slug}`); + const prev = renumber ? undefined : byKey.get(`${brandId}|${m.slug}`); const dex = prev?.dex ?? nextDex++; const { variants, ...cols } = m; const row = { diff --git a/src/lib/cars/shared.ts b/src/lib/cars/shared.ts index 9b37ce2..9f698a1 100644 --- a/src/lib/cars/shared.ts +++ b/src/lib/cars/shared.ts @@ -24,8 +24,8 @@ export const BODIES = [ ] as const; export const DRIVES = ['fwd', 'rwd', 'awd', '4wd'] as const; export const SORTS = { - dex: 'dex #', popular: 'most common in NL', + dex: 'dex #', name: 'name', newest: 'newest', oldest: 'oldest', diff --git a/src/lib/server/cars.ts b/src/lib/server/cars.ts index 343e25f..2518e96 100644 --- a/src/lib/server/cars.ts +++ b/src/lib/server/cars.ts @@ -38,7 +38,7 @@ export function parseFilters(params: URLSearchParams): CarFilters { market: pick(params.get('market'), ['nl', 'us'] as const), from: year(params.get('from')), to: year(params.get('to')), - sort: sort && sort in SORTS ? (sort as CarSort) : 'dex', + sort: sort && sort in SORTS ? (sort as CarSort) : 'popular', page: Math.min(Math.max(1, Math.floor(Number(params.get('page')) || 1)), 1000) }; } diff --git a/src/routes/trackers/cars/+page.svelte b/src/routes/trackers/cars/+page.svelte index a27c77e..879b8f9 100644 --- a/src/routes/trackers/cars/+page.svelte +++ b/src/routes/trackers/cars/+page.svelte @@ -50,7 +50,7 @@ } const quick = [ - { label: '🔥 most common in NL', params: { sort: 'popular', market: 'nl' } }, + { label: '📖 dex order', params: { sort: 'dex' } }, { label: '⚡ electric', params: { fuel: 'electric', sort: 'popular' } }, { label: '🍃 hybrids', params: { fuel: 'hybrid', sort: 'popular' } }, { label: '🏎️ coupés', params: { body: 'coupe', sort: 'popular' } },