fix
This commit is contained in:
@@ -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));
|
||||
|
||||
// Ensure directory exists
|
||||
await fs.mkdir(this.cacheDir, { recursive: true });
|
||||
|
||||
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}`);
|
||||
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) {
|
||||
// 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;
|
||||
|
||||
Reference in New Issue
Block a user