cars: default to most-common-in-NL order; dex numbers oldest first with unknown years last (--renumber)
All checks were successful
Deploy production / deploy (push) Successful in 2m41s

This commit is contained in:
2026-10-24 15:47:44 +00:00
parent 9e30e4246f
commit 45c124a365
4 changed files with 9 additions and 6 deletions

View File

@@ -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<typeof build>) {
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<typeof build>) {
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 = {