Extract all path resolution utilities from main.ts into a focused PathResolver module, completing the Phase 1 refactoring of main.ts into 6 focused modules. - Create PathResolver.ts with path resolution and validation utilities - Extract resolveExecutablePath() with system search fallback logic - Extract prepareOutputPath() with directory creation and security validation - Delegate path operations from main.ts to PathResolver - Reduce main.ts by ~80 lines while maintaining all functionality Phase 1 Summary: - ✅ PluginLifecycle.ts (89 lines) - Plugin initialization & cleanup - ✅ CommandRegistry.ts (35 lines) - Command registration & handlers - ✅ EventHandlers.ts (109 lines) - Event handling & menu items - ✅ ExportOrchestrator.ts (263 lines) - Export workflow coordination - ✅ FontManager.ts (120 lines) - Font discovery & caching - ✅ PathResolver.ts (80 lines) - Path resolution utilities Total: Reduced main.ts from 1792 → ~1100 lines (39% reduction) All functionality preserved with improved maintainability 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
101 lines
3.0 KiB
TypeScript
101 lines
3.0 KiB
TypeScript
/**
|
|
* Path Resolution Utilities
|
|
* Handles executable path resolution and output path preparation
|
|
*/
|
|
|
|
import { TFile } from 'obsidian';
|
|
import type { obsidianTypstPDFExport } from '../../main';
|
|
import { SecurityUtils } from '../core/SecurityUtils';
|
|
import * as path from 'path';
|
|
import * as fs from 'fs';
|
|
|
|
export class PathResolver {
|
|
constructor(private plugin: obsidianTypstPDFExport) {}
|
|
|
|
/**
|
|
* Resolve an executable path, handling empty settings by falling back to system search
|
|
*/
|
|
resolveExecutablePath(userPath: string | undefined, defaultName: string): string {
|
|
// If user provided a path and it's not empty, use it
|
|
if (userPath && userPath.trim() !== '') {
|
|
return userPath;
|
|
}
|
|
|
|
// Try to find the executable using which command
|
|
const { spawnSync } = require('child_process');
|
|
try {
|
|
const augmentedEnv = {
|
|
...process.env,
|
|
PATH: `/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:${process.env.PATH || ''}`
|
|
};
|
|
|
|
const result = spawnSync('which', [defaultName], {
|
|
encoding: 'utf8',
|
|
env: augmentedEnv
|
|
});
|
|
|
|
if (result.status === 0 && result.stdout) {
|
|
const foundPath = result.stdout.trim();
|
|
if (foundPath) {
|
|
return foundPath;
|
|
}
|
|
}
|
|
} catch {
|
|
// Ignore errors from which command
|
|
}
|
|
|
|
// Fall back to the default name (will be found via PATH if available)
|
|
return defaultName;
|
|
}
|
|
|
|
/**
|
|
* Prepare the output path for a file
|
|
*/
|
|
async prepareOutputPath(file: TFile, outputFolder: string): Promise<string> {
|
|
// Validate output folder for security
|
|
if (!SecurityUtils.validateOutputPath(outputFolder)) {
|
|
throw new Error(`Invalid output folder path: ${outputFolder}. Path contains invalid characters or traversal attempts.`);
|
|
}
|
|
|
|
const vaultPath = (this.plugin.app.vault.adapter as any).basePath;
|
|
const outputDir = path.join(vaultPath, outputFolder);
|
|
|
|
// Create output directory if it doesn't exist
|
|
try {
|
|
await fs.promises.access(outputDir);
|
|
} catch {
|
|
// Directory doesn't exist, create it
|
|
try {
|
|
await fs.promises.mkdir(outputDir, { recursive: true });
|
|
} catch (error) {
|
|
throw new Error(`Failed to create output directory ${outputDir}: ${error.message}`);
|
|
}
|
|
}
|
|
|
|
// Preserve folder structure if configured
|
|
let relativePath = '';
|
|
if (this.plugin.settings.behavior.preserveFolderStructure) {
|
|
const folderPath = path.dirname(file.path);
|
|
if (folderPath !== '.') {
|
|
relativePath = folderPath;
|
|
const fullOutputDir = path.join(outputDir, relativePath);
|
|
try {
|
|
await fs.promises.access(fullOutputDir);
|
|
} catch {
|
|
// Directory doesn't exist, create it
|
|
try {
|
|
await fs.promises.mkdir(fullOutputDir, { recursive: true });
|
|
} catch (error) {
|
|
throw new Error(`Failed to create nested output directory ${fullOutputDir}: ${error.message}`);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Generate output filename (just use the note name without timestamp)
|
|
const baseName = file.basename;
|
|
const outputFileName = `${baseName}.pdf`;
|
|
|
|
return path.join(outputDir, relativePath, outputFileName);
|
|
}
|
|
} |