refactor: complete PdfToImageConverter.ts modularization

Extract complex PDF-to-image conversion logic into specialized modules:

- BinaryLocator.ts: pdf2img binary discovery and path resolution
- EnvironmentUtils.ts: PATH setup and environment management
- PdfCliExecutor.ts: CLI command execution and error handling
- FileDiscovery.ts: output file discovery and pattern matching

Reduces PdfToImageConverter.ts from 308 to 193 lines (37% reduction)
while preserving all functionality and error handling.

Improves maintainability by separating concerns:
- Binary location strategy isolated from conversion logic
- Environment setup centralized and reusable
- CLI execution abstracted with proper error handling
- File discovery logic handles pdf2img filename variations

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Jarvis committed 2025-09-10 13:10:23 +02:00
1 parent 3787703aab
commit 2ed18d2415
5 files changed
+537 -136

No files matched your search

+103
View File
@@ -0,0 +1,103 @@
/**
* Binary locator utilities for finding external tool binaries.
* Handles discovery of pdf2img and other CLI tools in the Obsidian environment.
*/
import * as path from 'path';
export interface BinaryLocation {
/** Full path to the binary */
binaryPath: string;
/** Whether the binary was found and is executable */
exists: boolean;
/** Error message if binary was not found */
error?: string;
}
export class BinaryLocator {
/**
* Find the pdf2img binary in the plugin's node_modules directory.
* Handles the complex Obsidian environment where __dirname points to electron.asar.
* @param plugin The plugin instance for accessing manifest and settings
* @returns Binary location info
*/
public static async findPdf2ImgBinary(plugin?: any): Promise<BinaryLocation> {
try {
// Get the plugin directory - need to handle Obsidian's environment
// In Obsidian, __dirname points to electron.asar, so we need to find the actual plugin path
let pluginDir: string;
// Try multiple strategies to find the plugin directory
const pluginDirName = plugin?.manifest?.dir || 'typst-pdf-export';
const configDir = plugin?.app.vault.configDir || '.obsidian';
const possiblePluginDirs = this.getPossiblePluginDirs(plugin, pluginDirName, configDir);
// Find the first directory that exists and has node_modules
pluginDir = possiblePluginDirs.find(dir => {
try {
const nodeModulesPath = path.join(dir, 'node_modules');
return require('fs').existsSync(nodeModulesPath);
} catch {
return false;
}
}) || possiblePluginDirs[0]; // Fallback to first option
const pdf2imgPath = path.join(pluginDir, 'node_modules', '.bin', 'pdf2img');
// Check if the binary exists
const exists = await this.validateBinaryExists(pdf2imgPath);
if (!exists) {
return {
binaryPath: pdf2imgPath,
exists: false,
error: `PDF2IMG binary not found at: ${pdf2imgPath}`
};
}
return {
binaryPath: pdf2imgPath,
exists: true
};
} catch (error: any) {
return {
binaryPath: '',
exists: false,
error: `Failed to locate pdf2img binary: ${error.message}`
};
}
}
/**
* Get possible plugin directory locations based on different Obsidian environments.
* @param plugin Plugin instance
* @param pluginDirName Name of the plugin directory
* @param configDir Obsidian config directory (.obsidian)
* @returns Array of possible plugin directory paths
*/
private static getPossiblePluginDirs(plugin: any, pluginDirName: string, configDir: string): string[] {
return [
// Strategy 1: From process.cwd() if it's in the vault
path.join(process.cwd(), configDir, 'plugins', pluginDirName),
// Strategy 2: Assuming we're running from vault root
path.join(configDir, 'plugins', pluginDirName),
// Strategy 3: From vault base path if plugin is available
...(plugin ? [path.join((plugin.app.vault.adapter as any).basePath, plugin.manifest.dir!)] : [])
];
}
/**
* Validate that a binary exists and is accessible.
* @param binaryPath Full path to the binary
* @returns True if binary exists, false otherwise
*/
private static async validateBinaryExists(binaryPath: string): Promise<boolean> {
try {
const fs2 = require('fs');
return fs2.existsSync(binaryPath);
} catch {
return false;
}
}
}