This commit is contained in:
sebseb7
2025-11-23 09:40:12 +01:00
parent bdc6a2e07d
commit 4c4ef37e40

View File

@@ -14,10 +14,31 @@ class CategorySyncer extends EventEmitter {
this.queuedSync = false; this.queuedSync = false;
this.cacheDir = process.env.CACHE_LOCATION || '.'; this.cacheDir = process.env.CACHE_LOCATION || '.';
this.lastTreeString = null; this.lastTreeString = null;
this.lastTemplateString = null;
// Load existing template if it exists
this._loadExistingTemplate();
CategorySyncer.instance = this; CategorySyncer.instance = this;
} }
async _loadExistingTemplate() {
try {
const templatePath = path.join(this.cacheDir, 'categories_translation_template.txt');
this.lastTemplateString = await fs.readFile(templatePath, 'utf-8');
} catch (err) {
// File doesn't exist yet, that's fine
}
try {
const treePath = path.join(this.cacheDir, 'category_tree.json');
const treeContent = await fs.readFile(treePath, 'utf-8');
this.lastTreeString = treeContent;
} catch (err) {
// File doesn't exist yet, that's fine
}
}
async triggerSync() { async triggerSync() {
if (this.isSyncing) { if (this.isSyncing) {
if (this.queuedSync) { if (this.queuedSync) {
@@ -104,24 +125,31 @@ class CategorySyncer extends EventEmitter {
// Deep copy tree for unpruned version (before pruning modifies it) // Deep copy tree for unpruned version (before pruning modifies it)
const unprunedTree = JSON.parse(JSON.stringify(tree)); const unprunedTree = JSON.parse(JSON.stringify(tree));
// Generate translation template BEFORE pruning (to include all categories)
const translationTemplate = this._buildTranslationTemplate(tree);
const templateString = this._formatTranslationTemplate(translationTemplate);
// Now prune for the main tree
tree = this._pruneTree(tree);
// Ensure directory exists // Ensure directory exists
await fs.mkdir(this.cacheDir, { recursive: true }); await fs.mkdir(this.cacheDir, { recursive: true });
// Compare pruned tree
const treeString = JSON.stringify(tree, null, 2); const treeString = JSON.stringify(tree, null, 2);
const changed = this.lastTreeString !== treeString; const changed = this.lastTreeString !== treeString;
if (changed) { if (changed) {
// Generate translation template BEFORE pruning (to include all categories) // Save template if it changed
const translationTemplate = this._buildTranslationTemplate(tree); if (this.lastTemplateString !== templateString) {
const templatePath = path.join(this.cacheDir, 'categories_translation_template.txt'); const templatePath = path.join(this.cacheDir, 'categories_translation_template.txt');
await fs.writeFile(templatePath, this._formatTranslationTemplate(translationTemplate)); await fs.writeFile(templatePath, templateString);
console.log(`💾 Translation template saved to ${templatePath}`); console.log(`💾 Translation template saved to ${templatePath}`);
this.lastTemplateString = templateString;
// Now prune for the main tree }
tree = this._pruneTree(tree);
const filePath = path.join(this.cacheDir, 'category_tree.json'); const filePath = path.join(this.cacheDir, 'category_tree.json');
await fs.writeFile(filePath, JSON.stringify(tree, null, 2)); await fs.writeFile(filePath, treeString);
console.log(`💾 Category tree saved to ${filePath}`); console.log(`💾 Category tree saved to ${filePath}`);
this.lastTreeString = treeString; this.lastTreeString = treeString;