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
+45
-38
No files matched your search
@@ -178,8 +178,8 @@ export class PandocTypstConverter {
|
|||||||
if (this.pandocOptions.vaultBasePath) {
|
if (this.pandocOptions.vaultBasePath) {
|
||||||
// Use TempDirectoryManager for plugin temp directories
|
// Use TempDirectoryManager for plugin temp directories
|
||||||
const configDir = this.plugin?.app?.vault?.configDir || '.obsidian';
|
const configDir = this.plugin?.app?.vault?.configDir || '.obsidian';
|
||||||
const tempManager = TempDirectoryManager.create(this.pandocOptions.vaultBasePath, configDir);
|
const tempManager = TempDirectoryManager.create(this.pandocOptions.vaultBasePath, configDir, undefined, this.plugin?.app);
|
||||||
this.tempDir = tempManager.ensureTempDir('pandoc');
|
this.tempDir = await tempManager.ensureTempDir('pandoc');
|
||||||
} else {
|
} else {
|
||||||
// Fallback to system temp if plugin folder not available
|
// Fallback to system temp if plugin folder not available
|
||||||
this.tempDir = await fsPromises.mkdtemp(path.join(os.tmpdir(), 'obsidian-typst-'));
|
this.tempDir = await fsPromises.mkdtemp(path.join(os.tmpdir(), 'obsidian-typst-'));
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
/**
|
// ABOUTME: Manages temporary directories for export operations
|
||||||
* Manages temporary directories for export operations
|
// ABOUTME: Uses normalizePath for cross-platform path operations
|
||||||
*/
|
|
||||||
|
|
||||||
import { PLUGIN_DIRS } from './constants';
|
import { PLUGIN_DIRS } from './constants';
|
||||||
|
import { normalizePath } from 'obsidian';
|
||||||
|
import type { App } from 'obsidian';
|
||||||
|
|
||||||
export interface TempDirectoryOptions {
|
export interface TempDirectoryOptions {
|
||||||
/** Base path for the vault */
|
/** Base path for the vault */
|
||||||
@@ -11,26 +12,28 @@ export interface TempDirectoryOptions {
|
|||||||
configDir: string;
|
configDir: string;
|
||||||
/** Plugin directory name (defaults to 'typst-pdf-export') */
|
/** Plugin directory name (defaults to 'typst-pdf-export') */
|
||||||
pluginName?: string;
|
pluginName?: string;
|
||||||
|
/** App instance for vault.adapter operations */
|
||||||
|
app: App;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class TempDirectoryManager {
|
export class TempDirectoryManager {
|
||||||
private readonly fs = require('fs');
|
|
||||||
private readonly path = require('path');
|
|
||||||
private readonly vaultPath: string;
|
private readonly vaultPath: string;
|
||||||
private readonly configDir: string;
|
private readonly configDir: string;
|
||||||
private readonly pluginName: string;
|
private readonly pluginName: string;
|
||||||
|
private readonly app: App;
|
||||||
|
|
||||||
constructor(options: TempDirectoryOptions) {
|
constructor(options: TempDirectoryOptions) {
|
||||||
this.vaultPath = options.vaultPath;
|
this.vaultPath = options.vaultPath;
|
||||||
this.configDir = options.configDir;
|
this.configDir = options.configDir;
|
||||||
this.pluginName = options.pluginName || 'typst-pdf-export';
|
this.pluginName = options.pluginName || 'typst-pdf-export';
|
||||||
|
this.app = options.app;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the plugin directory path
|
* Get the plugin directory path
|
||||||
*/
|
*/
|
||||||
private getPluginDir(): string {
|
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,17 +41,18 @@ export class TempDirectoryManager {
|
|||||||
*/
|
*/
|
||||||
public getTempDir(type: 'images' | 'pandoc'): string {
|
public getTempDir(type: 'images' | 'pandoc'): string {
|
||||||
const dirName = type === 'images' ? PLUGIN_DIRS.TEMP_IMAGES : PLUGIN_DIRS.TEMP_PANDOC;
|
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
|
* 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);
|
const tempDir = this.getTempDir(type);
|
||||||
|
|
||||||
if (!this.fs.existsSync(tempDir)) {
|
const exists = await this.app.vault.adapter.exists(tempDir);
|
||||||
this.fs.mkdirSync(tempDir, { recursive: true });
|
if (!exists) {
|
||||||
|
await this.app.vault.adapter.mkdir(tempDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
return tempDir;
|
return tempDir;
|
||||||
@@ -57,40 +61,40 @@ export class TempDirectoryManager {
|
|||||||
/**
|
/**
|
||||||
* Clean up a specific temp directory
|
* 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);
|
const tempDir = this.getTempDir(type);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (this.fs.existsSync(tempDir)) {
|
const exists = await this.app.vault.adapter.exists(tempDir);
|
||||||
const files = this.fs.readdirSync(tempDir);
|
if (exists) {
|
||||||
for (const file of files) {
|
const list = await this.app.vault.adapter.list(tempDir);
|
||||||
this.fs.unlinkSync(this.path.join(tempDir, file));
|
// 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) {
|
} catch (error) {
|
||||||
console.warn(`Export: Failed to clean up ${type === 'images' ? 'temp-images' : 'temp-pandoc'} directory:`, error);
|
console.warn(`Export: Failed to clean up ${type === 'images' ? 'temp-images' : 'temp-pandoc'} directory:`, error);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clean up all temp directories
|
* Clean up all temp directories
|
||||||
*/
|
*/
|
||||||
public cleanupAllTempDirs(): { images: boolean; pandoc: boolean } {
|
public async cleanupAllTempDirs(): Promise<{ images: boolean; pandoc: boolean }> {
|
||||||
return {
|
return {
|
||||||
images: this.cleanupTempDir('images'),
|
images: await this.cleanupTempDir('images'),
|
||||||
pandoc: this.cleanupTempDir('pandoc')
|
pandoc: await this.cleanupTempDir('pandoc')
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Static helper to create manager from vault path and configDir
|
* Static helper to create manager from vault path and configDir
|
||||||
*/
|
*/
|
||||||
public static create(vaultPath: string, configDir: string, pluginName?: string): TempDirectoryManager {
|
public static create(vaultPath: string, configDir: string, pluginName: string | undefined, app: App): TempDirectoryManager {
|
||||||
return new TempDirectoryManager({ vaultPath, configDir, pluginName });
|
return new TempDirectoryManager({ vaultPath, configDir, pluginName, app });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -98,10 +102,8 @@ export class TempDirectoryManager {
|
|||||||
*/
|
*/
|
||||||
public isPluginTempDir(dirPath: string): boolean {
|
public isPluginTempDir(dirPath: string): boolean {
|
||||||
const pluginDir = this.getPluginDir();
|
const pluginDir = this.getPluginDir();
|
||||||
const normalizedPath = this.path.resolve(dirPath);
|
// Simple string comparison for temp directory validation
|
||||||
const normalizedPluginDir = this.path.resolve(pluginDir);
|
return dirPath.startsWith(pluginDir) &&
|
||||||
|
|
||||||
return normalizedPath.startsWith(normalizedPluginDir) &&
|
|
||||||
(dirPath.includes(PLUGIN_DIRS.TEMP_IMAGES) || dirPath.includes(PLUGIN_DIRS.TEMP_PANDOC));
|
(dirPath.includes(PLUGIN_DIRS.TEMP_IMAGES) || dirPath.includes(PLUGIN_DIRS.TEMP_PANDOC));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -140,8 +140,8 @@ export class ExportOrchestrator {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// Create temp directory for conversion
|
// Create temp directory for conversion
|
||||||
const tempManager = new TempDirectoryManager({ vaultPath: vaultPath, configDir: this.plugin.app.vault.configDir });
|
const tempManager = new TempDirectoryManager({ vaultPath: vaultPath, configDir: this.plugin.app.vault.configDir, app: this.plugin.app });
|
||||||
const tempDir = tempManager.ensureTempDir('pandoc');
|
const tempDir = await tempManager.ensureTempDir('pandoc');
|
||||||
|
|
||||||
// Load file content
|
// Load file content
|
||||||
const content = await this.plugin.app.vault.read(file);
|
const content = await this.plugin.app.vault.read(file);
|
||||||
@@ -245,8 +245,8 @@ export class ExportOrchestrator {
|
|||||||
|
|
||||||
// Clean up temporary directories
|
// Clean up temporary directories
|
||||||
try {
|
try {
|
||||||
const cleanupManager = TempDirectoryManager.create(vaultPath, this.plugin.app.vault.configDir);
|
const cleanupManager = TempDirectoryManager.create(vaultPath, this.plugin.app.vault.configDir, undefined, this.plugin.app);
|
||||||
cleanupManager.cleanupAllTempDirs();
|
await cleanupManager.cleanupAllTempDirs();
|
||||||
} catch (cleanupError) {
|
} catch (cleanupError) {
|
||||||
console.warn('Export: Failed to clean up temporary directories:', cleanupError);
|
console.warn('Export: Failed to clean up temporary directories:', cleanupError);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,24 +64,28 @@ export class PluginLifecycle {
|
|||||||
this.plugin.currentExportController.abort();
|
this.plugin.currentExportController.abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean up temp directories on plugin unload
|
// Clean up temp directories on plugin unload (fire-and-forget async)
|
||||||
|
(async () => {
|
||||||
try {
|
try {
|
||||||
const vaultPath = (this.plugin.app.vault.adapter as unknown as { basePath: string }).basePath;
|
const vaultPath = (this.plugin.app.vault.adapter as unknown as { basePath: string }).basePath;
|
||||||
const cleanupManager = TempDirectoryManager.create(vaultPath, this.plugin.app.vault.configDir);
|
const cleanupManager = TempDirectoryManager.create(vaultPath, this.plugin.app.vault.configDir, undefined, this.plugin.app);
|
||||||
cleanupManager.cleanupAllTempDirs();
|
await cleanupManager.cleanupAllTempDirs();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn('Export: Failed to clean up temp directories during unload:', error);
|
console.warn('Export: Failed to clean up temp directories during unload:', error);
|
||||||
}
|
}
|
||||||
|
})();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clean up leftover temp directories from previous sessions
|
* Clean up leftover temp directories from previous sessions
|
||||||
*/
|
*/
|
||||||
private cleanupStartupTempDirectories(): void {
|
private cleanupStartupTempDirectories(): void {
|
||||||
|
// Clean up temp directories (fire-and-forget async)
|
||||||
|
(async () => {
|
||||||
try {
|
try {
|
||||||
const vaultPath = (this.plugin.app.vault.adapter as unknown as { basePath: string }).basePath;
|
const vaultPath = (this.plugin.app.vault.adapter as unknown as { basePath: string }).basePath;
|
||||||
const cleanupManager = TempDirectoryManager.create(vaultPath, this.plugin.app.vault.configDir);
|
const cleanupManager = TempDirectoryManager.create(vaultPath, this.plugin.app.vault.configDir, undefined, this.plugin.app);
|
||||||
const _result = cleanupManager.cleanupAllTempDirs();
|
const _result = await cleanupManager.cleanupAllTempDirs();
|
||||||
|
|
||||||
if (this.plugin.settings.behavior.debugMode) {
|
if (this.plugin.settings.behavior.debugMode) {
|
||||||
// Debug logging was here but empty in original
|
// Debug logging was here but empty in original
|
||||||
@@ -90,5 +94,6 @@ export class PluginLifecycle {
|
|||||||
console.warn('Export: Startup temp directory cleanup failed (non-critical):', error);
|
console.warn('Export: Startup temp directory cleanup failed (non-critical):', error);
|
||||||
// Don't throw - this shouldn't prevent plugin from loading
|
// Don't throw - this shouldn't prevent plugin from loading
|
||||||
}
|
}
|
||||||
|
})();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user