refactor: migrate TempDirectoryManager to use vault.adapter exclusively
- Updated TempDirectoryManager to use vault.adapter methods exclusively - Made all temp directory operations async (ensureTempDir, cleanupTempDir, cleanupAllTempDirs) - Removed unnecessary fs fallback since App instance is always available in Obsidian plugins - Updated calling code in PandocTypstConverter and ExportOrchestrator to handle async - Updated PluginLifecycle cleanup methods to use fire-and-forget async pattern - All path operations now use normalizePath for cross-platform compatibility - Simplified interface - App parameter is now required as it should be 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
4 files changed
+62
-55
No files matched your search
@@ -178,8 +178,8 @@ export class PandocTypstConverter {
|
||||
if (this.pandocOptions.vaultBasePath) {
|
||||
// Use TempDirectoryManager for plugin temp directories
|
||||
const configDir = this.plugin?.app?.vault?.configDir || '.obsidian';
|
||||
const tempManager = TempDirectoryManager.create(this.pandocOptions.vaultBasePath, configDir);
|
||||
this.tempDir = tempManager.ensureTempDir('pandoc');
|
||||
const tempManager = TempDirectoryManager.create(this.pandocOptions.vaultBasePath, configDir, undefined, this.plugin?.app);
|
||||
this.tempDir = await tempManager.ensureTempDir('pandoc');
|
||||
} else {
|
||||
// Fallback to system temp if plugin folder not available
|
||||
this.tempDir = await fsPromises.mkdtemp(path.join(os.tmpdir(), 'obsidian-typst-'));
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
/**
|
||||
* Manages temporary directories for export operations
|
||||
*/
|
||||
// ABOUTME: Manages temporary directories for export operations
|
||||
// ABOUTME: Uses normalizePath for cross-platform path operations
|
||||
|
||||
import { PLUGIN_DIRS } from './constants';
|
||||
import { normalizePath } from 'obsidian';
|
||||
import type { App } from 'obsidian';
|
||||
|
||||
export interface TempDirectoryOptions {
|
||||
/** Base path for the vault */
|
||||
@@ -11,26 +12,28 @@ export interface TempDirectoryOptions {
|
||||
configDir: string;
|
||||
/** Plugin directory name (defaults to 'typst-pdf-export') */
|
||||
pluginName?: string;
|
||||
/** App instance for vault.adapter operations */
|
||||
app: App;
|
||||
}
|
||||
|
||||
export class TempDirectoryManager {
|
||||
private readonly fs = require('fs');
|
||||
private readonly path = require('path');
|
||||
private readonly vaultPath: string;
|
||||
private readonly configDir: string;
|
||||
private readonly pluginName: string;
|
||||
private readonly app: App;
|
||||
|
||||
constructor(options: TempDirectoryOptions) {
|
||||
this.vaultPath = options.vaultPath;
|
||||
this.configDir = options.configDir;
|
||||
this.pluginName = options.pluginName || 'typst-pdf-export';
|
||||
this.app = options.app;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the plugin directory path
|
||||
*/
|
||||
private getPluginDir(): string {
|
||||
return this.path.join(this.vaultPath, this.configDir, 'plugins', this.pluginName);
|
||||
return normalizePath([this.vaultPath, this.configDir, 'plugins', this.pluginName].join('/'));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -38,59 +41,60 @@ export class TempDirectoryManager {
|
||||
*/
|
||||
public getTempDir(type: 'images' | 'pandoc'): string {
|
||||
const dirName = type === 'images' ? PLUGIN_DIRS.TEMP_IMAGES : PLUGIN_DIRS.TEMP_PANDOC;
|
||||
return this.path.join(this.getPluginDir(), dirName);
|
||||
return normalizePath([this.getPluginDir(), dirName].join('/'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure a temp directory exists, creating it if necessary
|
||||
*/
|
||||
public ensureTempDir(type: 'images' | 'pandoc'): string {
|
||||
public async ensureTempDir(type: 'images' | 'pandoc'): Promise<string> {
|
||||
const tempDir = this.getTempDir(type);
|
||||
|
||||
if (!this.fs.existsSync(tempDir)) {
|
||||
this.fs.mkdirSync(tempDir, { recursive: true });
|
||||
|
||||
const exists = await this.app.vault.adapter.exists(tempDir);
|
||||
if (!exists) {
|
||||
await this.app.vault.adapter.mkdir(tempDir);
|
||||
}
|
||||
|
||||
|
||||
return tempDir;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up a specific temp directory
|
||||
*/
|
||||
public cleanupTempDir(type: 'images' | 'pandoc'): boolean {
|
||||
public async cleanupTempDir(type: 'images' | 'pandoc'): Promise<boolean> {
|
||||
const tempDir = this.getTempDir(type);
|
||||
|
||||
|
||||
try {
|
||||
if (this.fs.existsSync(tempDir)) {
|
||||
const files = this.fs.readdirSync(tempDir);
|
||||
for (const file of files) {
|
||||
this.fs.unlinkSync(this.path.join(tempDir, file));
|
||||
const exists = await this.app.vault.adapter.exists(tempDir);
|
||||
if (exists) {
|
||||
const list = await this.app.vault.adapter.list(tempDir);
|
||||
// Remove only files, not subdirectories (matching current pattern)
|
||||
for (const file of list.files) {
|
||||
await this.app.vault.adapter.remove(file);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.warn(`Export: Failed to clean up ${type === 'images' ? 'temp-images' : 'temp-pandoc'} directory:`, error);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up all temp directories
|
||||
*/
|
||||
public cleanupAllTempDirs(): { images: boolean; pandoc: boolean } {
|
||||
public async cleanupAllTempDirs(): Promise<{ images: boolean; pandoc: boolean }> {
|
||||
return {
|
||||
images: this.cleanupTempDir('images'),
|
||||
pandoc: this.cleanupTempDir('pandoc')
|
||||
images: await this.cleanupTempDir('images'),
|
||||
pandoc: await this.cleanupTempDir('pandoc')
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Static helper to create manager from vault path and configDir
|
||||
*/
|
||||
public static create(vaultPath: string, configDir: string, pluginName?: string): TempDirectoryManager {
|
||||
return new TempDirectoryManager({ vaultPath, configDir, pluginName });
|
||||
public static create(vaultPath: string, configDir: string, pluginName: string | undefined, app: App): TempDirectoryManager {
|
||||
return new TempDirectoryManager({ vaultPath, configDir, pluginName, app });
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -98,10 +102,8 @@ export class TempDirectoryManager {
|
||||
*/
|
||||
public isPluginTempDir(dirPath: string): boolean {
|
||||
const pluginDir = this.getPluginDir();
|
||||
const normalizedPath = this.path.resolve(dirPath);
|
||||
const normalizedPluginDir = this.path.resolve(pluginDir);
|
||||
|
||||
return normalizedPath.startsWith(normalizedPluginDir) &&
|
||||
// Simple string comparison for temp directory validation
|
||||
return dirPath.startsWith(pluginDir) &&
|
||||
(dirPath.includes(PLUGIN_DIRS.TEMP_IMAGES) || dirPath.includes(PLUGIN_DIRS.TEMP_PANDOC));
|
||||
}
|
||||
}
|
||||
@@ -140,8 +140,8 @@ export class ExportOrchestrator {
|
||||
|
||||
try {
|
||||
// Create temp directory for conversion
|
||||
const tempManager = new TempDirectoryManager({ vaultPath: vaultPath, configDir: this.plugin.app.vault.configDir });
|
||||
const tempDir = tempManager.ensureTempDir('pandoc');
|
||||
const tempManager = new TempDirectoryManager({ vaultPath: vaultPath, configDir: this.plugin.app.vault.configDir, app: this.plugin.app });
|
||||
const tempDir = await tempManager.ensureTempDir('pandoc');
|
||||
|
||||
// Load file content
|
||||
const content = await this.plugin.app.vault.read(file);
|
||||
@@ -245,8 +245,8 @@ export class ExportOrchestrator {
|
||||
|
||||
// Clean up temporary directories
|
||||
try {
|
||||
const cleanupManager = TempDirectoryManager.create(vaultPath, this.plugin.app.vault.configDir);
|
||||
cleanupManager.cleanupAllTempDirs();
|
||||
const cleanupManager = TempDirectoryManager.create(vaultPath, this.plugin.app.vault.configDir, undefined, this.plugin.app);
|
||||
await cleanupManager.cleanupAllTempDirs();
|
||||
} catch (cleanupError) {
|
||||
console.warn('Export: Failed to clean up temporary directories:', cleanupError);
|
||||
}
|
||||
|
||||
@@ -64,31 +64,36 @@ export class PluginLifecycle {
|
||||
this.plugin.currentExportController.abort();
|
||||
}
|
||||
|
||||
// Clean up temp directories on plugin unload
|
||||
try {
|
||||
const vaultPath = (this.plugin.app.vault.adapter as unknown as { basePath: string }).basePath;
|
||||
const cleanupManager = TempDirectoryManager.create(vaultPath, this.plugin.app.vault.configDir);
|
||||
cleanupManager.cleanupAllTempDirs();
|
||||
} catch (error) {
|
||||
console.warn('Export: Failed to clean up temp directories during unload:', error);
|
||||
}
|
||||
// Clean up temp directories on plugin unload (fire-and-forget async)
|
||||
(async () => {
|
||||
try {
|
||||
const vaultPath = (this.plugin.app.vault.adapter as unknown as { basePath: string }).basePath;
|
||||
const cleanupManager = TempDirectoryManager.create(vaultPath, this.plugin.app.vault.configDir, undefined, this.plugin.app);
|
||||
await cleanupManager.cleanupAllTempDirs();
|
||||
} catch (error) {
|
||||
console.warn('Export: Failed to clean up temp directories during unload:', error);
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up leftover temp directories from previous sessions
|
||||
*/
|
||||
private cleanupStartupTempDirectories(): void {
|
||||
try {
|
||||
const vaultPath = (this.plugin.app.vault.adapter as unknown as { basePath: string }).basePath;
|
||||
const cleanupManager = TempDirectoryManager.create(vaultPath, this.plugin.app.vault.configDir);
|
||||
const _result = cleanupManager.cleanupAllTempDirs();
|
||||
|
||||
if (this.plugin.settings.behavior.debugMode) {
|
||||
// Debug logging was here but empty in original
|
||||
// Clean up temp directories (fire-and-forget async)
|
||||
(async () => {
|
||||
try {
|
||||
const vaultPath = (this.plugin.app.vault.adapter as unknown as { basePath: string }).basePath;
|
||||
const cleanupManager = TempDirectoryManager.create(vaultPath, this.plugin.app.vault.configDir, undefined, this.plugin.app);
|
||||
const _result = await cleanupManager.cleanupAllTempDirs();
|
||||
|
||||
if (this.plugin.settings.behavior.debugMode) {
|
||||
// Debug logging was here but empty in original
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('Export: Startup temp directory cleanup failed (non-critical):', error);
|
||||
// Don't throw - this shouldn't prevent plugin from loading
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('Export: Startup temp directory cleanup failed (non-critical):', error);
|
||||
// Don't throw - this shouldn't prevent plugin from loading
|
||||
}
|
||||
})();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user