fix(places): a rebuild writes its files before removing stale tiles, and keeps the build date when nothing changed

Co-authored-by: Manjunath Paparaju <[email protected]>
This commit is contained in:
Sameh KhamisandManjunath Paparaju committed 2026-11-18 22:34:59 -08:00
1 parent 30ec362c13
commit a2c86c0fed
4 files changed
+55 -4

No files matched your search

+20 -4
View File
@@ -39,6 +39,7 @@ import { mkdir, readdir, readFile, rm, writeFile } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import {
keepBuiltIfUnchanged,
placeTileFiles,
withoutCities,
withoutNearTwins,
@@ -68,6 +69,8 @@ const outDir = path.join(
: 'src/data/local_data/wikidata_cities',
);
/** A file's text, or undefined when there is none yet. */
const readIfThere = (file) => readFile(file, 'utf8').catch(() => undefined);
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const qid = (uri) => uri.slice(uri.lastIndexOf('/') + 1);
@@ -499,17 +502,30 @@ if (tilesMode) {
count: tileRows.length,
maxPopulation,
});
// Tiles from an earlier build that this one has no rows for go.
// The new files are all written before tiles from an earlier build that
// this one has no rows for go, so a build that stops part way never leaves
// an index naming a removed tile.
const indexFile = path.join(outDir, 'index.json');
files.set(
'index.json',
keepBuiltIfUnchanged(files.get('index.json'), await readIfThere(indexFile)),
);
for (const [name, text] of files)
await writeFile(path.join(outDir, name), text);
for (const name of await readdir(outDir))
if (/^-?\d+_-?\d+\.json$/.test(name) && !files.has(name))
await rm(path.join(outDir, name));
for (const [name, text] of files)
await writeFile(path.join(outDir, name), text);
console.log(
`Wrote ${tileRows.length} places in ${files.size - 2} tiles to ${path.relative(root, outDir)}`,
);
process.exit(0);
}
const file = path.join(outDir, 'cities.json');
await writeFile(file, `${JSON.stringify({ meta, rows })}\n`);
await writeFile(
file,
keepBuiltIfUnchanged(
`${JSON.stringify({ meta, rows })}\n`,
await readIfThere(file),
),
);
console.log(`Wrote ${rows.length} places to ${path.relative(root, file)}`);