Enhances path handling and file resolution #5
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
|
||||
|
⚠️ Potential issue | 🔴 Critical adapter.list/remove must use vault-relative too Same issue during cleanup; operate with vault-relative path to list and remove files correctly. 📝 Committable suggestion
🤖 Prompt for AI Agents_⚠️ Potential issue_ | _🔴 Critical_
**adapter.list/remove must use vault-relative too**
Same issue during cleanup; operate with vault-relative path to list and remove files correctly.
```diff
- const exists = await this.app.vault.adapter.exists(tempDir);
- if (exists) {
- const list = await this.app.vault.adapter.list(tempDir);
+ const rel = tempDir.startsWith(this.vaultPath)
+ ? tempDir.substring(this.vaultPath.length).replace(/^[/\\]+/, '')
+ : tempDir;
+ const exists = await this.app.vault.adapter.exists(rel);
+ if (exists) {
+ const list = await this.app.vault.adapter.list(rel);
// Remove only files, not subdirectories (matching current pattern)
for (const file of list.files) {
await this.app.vault.adapter.remove(file);
}
}
```
<!-- suggestion_start -->
<details>
<summary>📝 Committable suggestion</summary>
> ‼️ **IMPORTANT**
> Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
```suggestion
const rel = tempDir.startsWith(this.vaultPath)
? tempDir.substring(this.vaultPath.length).replace(/^[/\\]+/, '')
: tempDir;
const exists = await this.app.vault.adapter.exists(rel);
if (exists) {
const list = await this.app.vault.adapter.list(rel);
// Remove only files, not subdirectories (matching current pattern)
for (const file of list.files) {
await this.app.vault.adapter.remove(file);
}
}
return true;
```
</details>
<!-- suggestion_end -->
<details>
<summary>🤖 Prompt for AI Agents</summary>
```
In src/core/TempDirectoryManager.ts around lines 68-76, the cleanup currently
calls adapter.list and adapter.remove with paths that may be absolute; change
both calls to use vault-relative paths: pass a vault-relative tempDir into
adapter.list, then when iterating list.files map each entry to its
vault-relative path before calling adapter.remove so the adapter operates on
vault-relative paths consistently (i.e., compute/normalize vault-relative paths
for list and remove and use those).
```
</details>
<!-- fingerprinting:phantom:medusa:chinchilla -->
<!-- This is an auto-generated comment by CodeRabbit -->
|
||||
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();
|
||||
|
⚠️ Potential issue | 🟠 Major Ensure cleanup targets the correct temp directories. Also provide 📝 Committable suggestion
🤖 Prompt for AI Agents_⚠️ Potential issue_ | _🟠 Major_
**Ensure cleanup targets the correct temp directories.**
Also provide `pluginName` to `create(...)` for consistent cleanup.
```diff
- const cleanupManager = TempDirectoryManager.create(vaultPath, this.plugin.app.vault.configDir, undefined, this.plugin.app);
+ const cleanupManager = TempDirectoryManager.create(
+ vaultPath,
+ this.plugin.app.vault.configDir,
+ this.plugin.manifest.id,
+ this.plugin.app
+ );
await cleanupManager.cleanupAllTempDirs();
```
<!-- suggestion_start -->
<details>
<summary>📝 Committable suggestion</summary>
> ‼️ **IMPORTANT**
> Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
```suggestion
const cleanupManager = TempDirectoryManager.create(
vaultPath,
this.plugin.app.vault.configDir,
this.plugin.manifest.id,
this.plugin.app
);
await cleanupManager.cleanupAllTempDirs();
```
</details>
<!-- suggestion_end -->
<details>
<summary>🤖 Prompt for AI Agents</summary>
```
In src/plugin/ExportOrchestrator.ts around lines 252 to 254, the
TempDirectoryManager.create call must target the correct plugin-specific temp
directories and include the pluginName for consistent cleanup; update the call
to pass the plugin's name (e.g. this.plugin.manifest.name or this.pluginName) as
the third argument instead of undefined so it becomes
TempDirectoryManager.create(vaultPath, this.plugin.app.vault.configDir,
<pluginName>, this.plugin.app), then keep awaiting
cleanupManager.cleanupAllTempDirs() to remove only that plugin's temp dirs.
```
</details>
<!-- fingerprinting:phantom:poseidon:chinchilla -->
<!-- This is an auto-generated comment by CodeRabbit -->
✅ Addressed in commits a129380 to 21ed9e1
|
||||
} 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
|
||||
}
|
||||
})();
|
||||
}
|
||||
}
|
||||
⚠️ Potential issue | 🔴 Critical
adapter.exists/mkdir need vault-relative path (don’t pass absolute tempDir)
tempDiris absolute; convert to vault-relative before calling adapter methods to avoid path mismatches.🤖 Prompt for AI Agents