MCPcopy Index your code
hub / github.com/5e-bits/5e-database / _handleTranslationFileModified

Function _handleTranslationFileModified

scripts/update/processor.ts:422–475  ·  view source on GitHub ↗
(db: Db, filepath: string)

Source from the content-addressed store, hash-verified

420}
421
422async function _handleTranslationFileModified(db: Db, filepath: string): Promise<void> {
423 // enPath is read from disk (post-commit), so if the English source was also modified in the
424 // same commit this correctly validates against the updated schema — no special handling needed.
425 const ctx = await resolveTranslationContext(filepath);
426 if (!ctx) return;
427 const { lang, indexName, enMap } = ctx;
428
429 const currentData = await readFileContent(filepath);
430 const oldData = parseJsonArrayContent(await getOldFileContent(filepath), `HEAD~1:${filepath}`);
431
432 console.log(`\nProcessing Modified translation ${filepath}...`);
433
434 const collectionPrefix = getCollectionPrefix(filepath);
435 const translationCollection = db.collection(`${collectionPrefix}translations`);
436
437 const oldMap = buildIndexMap(oldData);
438 const newMap = buildIndexMap(currentData);
439
440 const ops: AnyBulkWriteOperation<Document>[] = [];
441
442 for (const [idx, entry] of newMap) {
443 const oldEntry = oldMap.get(idx);
444 if (!oldEntry || diff(oldEntry, entry)) {
445 const doc = buildTranslationDoc(entry as Record<string, unknown>, enMap, indexName, lang);
446 if (!doc) continue;
447 ops.push({
448 [MONGO_OP_UPDATE_ONE]: {
449 filter: {
450 source_collection: doc.source_collection,
451 source_index: doc.source_index,
452 lang: doc.lang,
453 },
454 update: { $set: doc },
455 upsert: true,
456 },
457 });
458 }
459 }
460
461 for (const idx of oldMap.keys()) {
462 if (!newMap.has(idx)) {
463 // source_collection matches indexName because buildTranslationDoc stores it that way.
464 ops.push({
465 [MONGO_OP_DELETE_ONE]: {
466 filter: { source_collection: indexName, source_index: idx, lang },
467 },
468 });
469 }
470 }
471
472 if (ops.length > 0) await translationCollection.bulkWrite(ops, { ordered: false });
473 console.log(` Processed ${ops.length} translation changes.`);
474 await _refreshLocaleStatsForLang(db, filepath, lang);
475}
476
477async function _handleTranslationFileDeleted(db: Db, filepath: string): Promise<void> {
478 const lang = getLocaleFromFilepath(filepath);

Callers 1

processFileUpdateFunction · 0.85

Calls 8

getOldFileContentFunction · 0.90
getCollectionPrefixFunction · 0.90
buildIndexMapFunction · 0.90
buildTranslationDocFunction · 0.90
readFileContentFunction · 0.85
parseJsonArrayContentFunction · 0.85

Tested by

no test coverage detected