From ddac8bd0457615069ded684d62f18772f9d68e55 Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Wed, 3 Sep 2025 23:02:11 +0200 Subject: [PATCH] Cleans up temporary directories on startup/unload Ensures temporary directories are cleaned up when the plugin loads and unloads, preventing potential issues from leftover files. This addresses potential accumulation of temp files from previous sessions and on plugin disable. --- main.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/main.ts b/main.ts index b46c279..cb7ce1a 100644 --- a/main.ts +++ b/main.ts @@ -68,6 +68,9 @@ export class obsidianTypstPDFExport extends Plugin { // Add settings tab this.addSettingTab(new ObsidianTypstPDFExportSettingTab(this.app, this)); + // Clean up any leftover temp directories from previous sessions + this.cleanupStartupTempDirectories(); + // Check dependencies on startup (async, don't await) this.checkDependenciesAsync(); @@ -987,12 +990,39 @@ ${dependencyResult.allAvailable await this.saveData(this.settings); } + /** + * Clean up leftover temp directories from previous sessions + */ + private cleanupStartupTempDirectories(): void { + try { + const vaultPath = (this.app.vault.adapter as any).basePath; + const cleanupManager = TempDirectoryManager.create(vaultPath); + const result = cleanupManager.cleanupAllTempDirs(); + + if (this.settings.behavior.debugMode) { + console.log('Export: Startup cleanup completed', result); + } + } catch (error) { + console.warn('Export: Startup temp directory cleanup failed (non-critical):', error); + // Don't throw - this shouldn't prevent plugin from loading + } + } + onunload() { // Cancel any ongoing exports if (this.currentExportController) { this.currentExportController.abort(); } + // Clean up temp directories on plugin unload + try { + const vaultPath = (this.app.vault.adapter as any).basePath; + const cleanupManager = TempDirectoryManager.create(vaultPath); + cleanupManager.cleanupAllTempDirs(); + } catch (error) { + console.warn('Export: Failed to clean up temp directories during unload:', error); + } + console.log('Obsidian Typst PDF Export plugin unloaded'); } }