diff --git a/main.ts b/main.ts index d63e688..2f57241 100644 --- a/main.ts +++ b/main.ts @@ -21,6 +21,7 @@ import { import { obsidianTypstPDFExportSettings, DEFAULT_SETTINGS, ExportFormat } from './src/core/settings'; import { FALLBACK_FONTS, PLUGIN_DIRS } from './src/core/constants'; +import { DependencyChecker } from './src/core/DependencyChecker'; import { PandocTypstConverter } from './src/converters/PandocTypstConverter'; import { ExportConfigModal } from './src/modal/ExportConfigModal'; import { ExportConfig, ExportConfigModalSettings } from './src/modal/types'; @@ -706,64 +707,22 @@ export class obsidianTypstPDFExport extends Plugin { * Show dependency status modal */ async showDependencyStatus(): Promise { - const { exec } = require('child_process'); - const { promisify } = require('util'); - const execAsync = promisify(exec); + const dependencyResult = await DependencyChecker.checkAllDependencies( + this.settings.pandocPath, + this.settings.typstPath, + this.settings.executablePaths?.imagemagickPath + ); - let pandocVersion = 'Not found'; - let typstVersion = 'Not found'; - let imagemagickVersion = 'Not found'; - - // Check Pandoc - try { - const pandocPath = this.settings.pandocPath || 'pandoc'; - const { stdout } = await execAsync(`${pandocPath} --version`); - const match = stdout.match(/pandoc\s+([\d.]+)/); - if (match) { - pandocVersion = match[1]; - } - } catch (error) { - console.error('Pandoc check failed:', error); - } - - // Check Typst - try { - const typstPath = this.settings.typstPath || 'typst'; - const { stdout } = await execAsync(`${typstPath} --version`); - const match = stdout.match(/typst\s+([\d.]+)/); - if (match) { - typstVersion = match[1]; - } - } catch (error) { - console.error('Typst check failed:', error); - } - - // Check ImageMagick/convert - try { - const convertPath = this.settings.executablePaths.imagemagickPath || 'convert'; - const { stdout } = await execAsync(`${convertPath} --version`); - const match = stdout.match(/ImageMagick\s+([\d.-]+)/); - if (match) { - imagemagickVersion = match[1]; - } - } catch (error) { - console.error('ImageMagick check failed:', error); - } - - // Show results in a notice - const missingDeps = []; - if (pandocVersion === 'Not found') missingDeps.push('Pandoc'); - if (typstVersion === 'Not found') missingDeps.push('Typst'); - if (imagemagickVersion === 'Not found') missingDeps.push('ImageMagick'); + const formatVersion = (dep: any) => dep.isAvailable ? (dep.version || 'Available') : 'Not found'; const message = `Dependency Status: -Pandoc: ${pandocVersion} -Typst: ${typstVersion} -ImageMagick: ${imagemagickVersion} +Pandoc: ${formatVersion(dependencyResult.pandoc)} +Typst: ${formatVersion(dependencyResult.typst)} +ImageMagick: ${formatVersion(dependencyResult.imagemagick)} -${missingDeps.length > 0 ? - `Missing dependencies: ${missingDeps.join(', ')}. Please install them and check the paths in settings.` : - 'All dependencies found!'}`; +${dependencyResult.allAvailable + ? 'All dependencies found!' + : `Missing dependencies: ${dependencyResult.missingDependencies.join(', ')}. Please install them and check the paths in settings.`}`; new Notice(message, 12000); // Show for 12 seconds (longer due to more content) } @@ -771,40 +730,11 @@ ${missingDeps.length > 0 ? private async checkDependenciesAsync(): Promise { // Check dependencies silently on startup try { - const { execSync } = require('child_process'); - const path = require('path'); - - // Augment PATH - const homeDir = process.env.HOME || process.env.USERPROFILE; - const localBin = path.join(homeDir, '.local', 'bin'); - const cargoHome = process.env.CARGO_HOME || path.join(homeDir, '.cargo'); - const cargoBin = path.join(cargoHome, 'bin'); - - const augmentedPath = `${process.env.PATH}:${localBin}:${cargoBin}:/usr/local/bin:/opt/homebrew/bin`; - const env = { ...process.env, PATH: augmentedPath }; - - let missingDeps = []; - - // Check Pandoc - try { - execSync('pandoc --version', { encoding: 'utf8', env }); - } catch { - missingDeps.push('Pandoc'); - } - - // Check Typst - try { - execSync('typst --version', { encoding: 'utf8', env }); - } catch { - missingDeps.push('Typst'); - } - - // Check ImageMagick - try { - execSync('convert --version', { encoding: 'utf8', env }); - } catch { - missingDeps.push('ImageMagick'); - } + const missingDeps = DependencyChecker.checkDependenciesSync( + this.settings.pandocPath, + this.settings.typstPath, + this.settings.executablePaths?.imagemagickPath + ); // Only show notice if dependencies are missing if (missingDeps.length > 0) { diff --git a/src/core/DependencyChecker.ts b/src/core/DependencyChecker.ts new file mode 100644 index 0000000..1b09a2c --- /dev/null +++ b/src/core/DependencyChecker.ts @@ -0,0 +1,153 @@ +/** + * Dependency checking utilities for Obsidian Typst PDF Export plugin + */ + +import { DEPENDENCY_CONSTANTS } from './constants'; + +export interface DependencyInfo { + name: string; + version: string | null; + isAvailable: boolean; + executablePath: string; +} + +export interface DependencyCheckResult { + pandoc: DependencyInfo; + typst: DependencyInfo; + imagemagick: DependencyInfo; + allAvailable: boolean; + missingDependencies: string[]; +} + +export class DependencyChecker { + /** + * Get augmented PATH with common binary locations + */ + private static getAugmentedPath(): string { + const homeDir = process.env.HOME || process.env.USERPROFILE || ''; + const commonPaths = DEPENDENCY_CONSTANTS.COMMON_PATHS.map(p => homeDir + p); + return `${process.env.PATH}:${commonPaths.join(':')}`; + } + + /** + * Get augmented environment with extended PATH + */ + private static getAugmentedEnv(): NodeJS.ProcessEnv { + return { + ...process.env, + PATH: this.getAugmentedPath() + }; + } + + /** + * Check if a dependency is available and get its version + */ + private static async checkDependency( + name: string, + executablePath: string, + versionCommand: string, + versionRegex: RegExp + ): Promise { + const { exec } = require('child_process'); + const { promisify } = require('util'); + const execAsync = promisify(exec); + + try { + const { stdout } = await execAsync(`${executablePath} ${versionCommand}`, { + env: this.getAugmentedEnv() + }); + + const match = stdout.match(versionRegex); + const version = match ? match[1] : null; + + return { + name, + version, + isAvailable: true, + executablePath + }; + } catch (error) { + return { + name, + version: null, + isAvailable: false, + executablePath + }; + } + } + + /** + * Check if a dependency is available (synchronous version for startup) + */ + private static checkDependencySync(executablePath: string, versionCommand: string): boolean { + const { execSync } = require('child_process'); + + try { + execSync(`${executablePath} ${versionCommand}`, { + encoding: 'utf8', + env: this.getAugmentedEnv() + }); + return true; + } catch { + return false; + } + } + + /** + * Comprehensive dependency check (async) - returns detailed information + */ + public static async checkAllDependencies( + pandocPath?: string, + typstPath?: string, + imagemagickPath?: string + ): Promise { + const pandocExec = pandocPath || DEPENDENCY_CONSTANTS.DEFAULT_EXECUTABLES.pandoc; + const typstExec = typstPath || DEPENDENCY_CONSTANTS.DEFAULT_EXECUTABLES.typst; + const imagemagickExec = imagemagickPath || DEPENDENCY_CONSTANTS.DEFAULT_EXECUTABLES.imagemagick; + + const [pandocInfo, typstInfo, imagemagickInfo] = await Promise.all([ + this.checkDependency('Pandoc', pandocExec, '--version', /pandoc\s+([\d.]+)/), + this.checkDependency('Typst', typstExec, '--version', /typst\s+([\d.]+)/), + this.checkDependency('ImageMagick', imagemagickExec, '--version', /ImageMagick\s+([\d.-]+)/) + ]); + + const missingDependencies = [pandocInfo, typstInfo, imagemagickInfo] + .filter(dep => !dep.isAvailable) + .map(dep => dep.name); + + return { + pandoc: pandocInfo, + typst: typstInfo, + imagemagick: imagemagickInfo, + allAvailable: missingDependencies.length === 0, + missingDependencies + }; + } + + /** + * Quick dependency check (synchronous) - returns list of missing dependencies + */ + public static checkDependenciesSync( + pandocPath?: string, + typstPath?: string, + imagemagickPath?: string + ): string[] { + const pandocExec = pandocPath || DEPENDENCY_CONSTANTS.DEFAULT_EXECUTABLES.pandoc; + const typstExec = typstPath || DEPENDENCY_CONSTANTS.DEFAULT_EXECUTABLES.typst; + const imagemagickExec = imagemagickPath || DEPENDENCY_CONSTANTS.DEFAULT_EXECUTABLES.imagemagick; + + const missingDeps: string[] = []; + + if (!this.checkDependencySync(pandocExec, '--version')) { + missingDeps.push('Pandoc'); + } + if (!this.checkDependencySync(typstExec, '--version')) { + missingDeps.push('Typst'); + } + if (!this.checkDependencySync(imagemagickExec, '--version')) { + missingDeps.push('ImageMagick'); + } + + return missingDeps; + } +} \ No newline at end of file diff --git a/src/core/constants.ts b/src/core/constants.ts index c7eea9a..402d3e6 100644 --- a/src/core/constants.ts +++ b/src/core/constants.ts @@ -37,4 +37,28 @@ export const EXPORT_CONSTANTS = { SHORT: 5000, LONG: 12000 } +} as const; + +/** + * Dependency checking constants and utilities + */ +export const DEPENDENCY_CONSTANTS = { + /** + * Default executable names for dependencies + */ + DEFAULT_EXECUTABLES: { + pandoc: 'pandoc', + typst: 'typst', + imagemagick: 'convert' + }, + + /** + * Common paths to augment PATH with + */ + COMMON_PATHS: [ + '/.local/bin', + '/.cargo/bin', + '/usr/local/bin', + '/opt/homebrew/bin' + ] } as const; \ No newline at end of file