From 4c4ef37e40389b136356f4bd6ad7d8291fc5e0b9 Mon Sep 17 00:00:00 2001 From: sebseb7 Date: Sun, 23 Nov 2025 09:40:12 +0100 Subject: [PATCH] fix --- category-syncer.js | 46 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/category-syncer.js b/category-syncer.js index 2e1bba3..a3e4251 100644 --- a/category-syncer.js +++ b/category-syncer.js @@ -14,10 +14,31 @@ class CategorySyncer extends EventEmitter { this.queuedSync = false; this.cacheDir = process.env.CACHE_LOCATION || '.'; this.lastTreeString = null; + this.lastTemplateString = null; + + // Load existing template if it exists + this._loadExistingTemplate(); 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() { if (this.isSyncing) { if (this.queuedSync) { @@ -104,24 +125,31 @@ class CategorySyncer extends EventEmitter { // Deep copy tree for unpruned version (before pruning modifies it) 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 await fs.mkdir(this.cacheDir, { recursive: true }); + // Compare pruned tree const treeString = JSON.stringify(tree, null, 2); const changed = this.lastTreeString !== treeString; if (changed) { - // Generate translation template BEFORE pruning (to include all categories) - const translationTemplate = this._buildTranslationTemplate(tree); - const templatePath = path.join(this.cacheDir, 'categories_translation_template.txt'); - await fs.writeFile(templatePath, this._formatTranslationTemplate(translationTemplate)); - console.log(`💾 Translation template saved to ${templatePath}`); - - // Now prune for the main tree - tree = this._pruneTree(tree); + // Save template if it changed + if (this.lastTemplateString !== templateString) { + const templatePath = path.join(this.cacheDir, 'categories_translation_template.txt'); + await fs.writeFile(templatePath, templateString); + console.log(`💾 Translation template saved to ${templatePath}`); + this.lastTemplateString = templateString; + } 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}`); this.lastTreeString = treeString;