refactor: complete TypeScript type safety improvements
- Replace all 'any' types with proper TypeScript types throughout codebase - Fix processedResult parameters to use PreprocessingResult interface - Update UI modal types: app parameter (App), pluginDefaults (obsidianTypstPDFExportSettings) - Fix dropdown components to use DropdownComponent from Obsidian API - Replace plugin 'any' types with obsidianTypstPDFExport throughout converters - Fix margins parameter to use proper interface structure - Update fs parameter to use proper Node.js fs module type - Improve error handling with proper unknown type and type guards - Remove unused PdfEmbedProcessor and clean up imports Achieves complete type safety with zero remaining 'any' types across src/ directory. Provides better IDE support, compile-time error detection, and maintainability. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
31 files changed
+204
-580
No files matched your search
@@ -2,7 +2,6 @@
|
||||
* Dependency checking utilities for Obsidian Typst PDF Export plugin
|
||||
*/
|
||||
|
||||
import { Notice } from 'obsidian';
|
||||
import { DEPENDENCY_CONSTANTS } from './constants';
|
||||
import { ExportErrorHandler } from './ExportErrorHandler';
|
||||
import { ExecutableChecker, DependencyInfo } from './ExecutableChecker';
|
||||
@@ -32,16 +31,12 @@ export class DependencyChecker {
|
||||
imagemagickPath?: string,
|
||||
additionalPaths: string[] = []
|
||||
): Promise<DependencyCheckResult> {
|
||||
console.log(`[DependencyChecker] checkAllDependencies called with:`, { pandocPath, typstPath, imagemagickPath, additionalPaths });
|
||||
|
||||
// Resolve actual executable paths, handling empty settings
|
||||
const [pandocExec, typstExec, imagemagickExec] = await Promise.all([
|
||||
ExecutableChecker.resolveExecutablePath(pandocPath, DEPENDENCY_CONSTANTS.DEFAULT_EXECUTABLES.pandoc, additionalPaths),
|
||||
ExecutableChecker.resolveExecutablePath(typstPath, DEPENDENCY_CONSTANTS.DEFAULT_EXECUTABLES.typst, additionalPaths),
|
||||
ExecutableChecker.resolveExecutablePath(imagemagickPath, DEPENDENCY_CONSTANTS.DEFAULT_EXECUTABLES.imagemagick, additionalPaths)
|
||||
]);
|
||||
|
||||
console.log(`[DependencyChecker] Resolved paths:`, { pandocExec, typstExec, imagemagickExec });
|
||||
|
||||
const [pandocInfo, typstInfo, imagemagickInfo] = await Promise.all([
|
||||
ExecutableChecker.checkDependency('Pandoc', pandocExec, '--version', /pandoc\s+([\d.]+)/),
|
||||
@@ -71,14 +66,10 @@ export class DependencyChecker {
|
||||
imagemagickPath?: string,
|
||||
additionalPaths: string[] = []
|
||||
): string[] {
|
||||
console.log(`[DependencyChecker] checkDependenciesSync called with:`, { pandocPath, typstPath, imagemagickPath, additionalPaths });
|
||||
|
||||
// Resolve actual executable paths, handling empty settings
|
||||
const pandocExec = ExecutableChecker.resolveExecutablePathSync(pandocPath, DEPENDENCY_CONSTANTS.DEFAULT_EXECUTABLES.pandoc, additionalPaths);
|
||||
const typstExec = ExecutableChecker.resolveExecutablePathSync(typstPath, DEPENDENCY_CONSTANTS.DEFAULT_EXECUTABLES.typst, additionalPaths);
|
||||
const imagemagickExec = ExecutableChecker.resolveExecutablePathSync(imagemagickPath, DEPENDENCY_CONSTANTS.DEFAULT_EXECUTABLES.imagemagick, additionalPaths);
|
||||
|
||||
console.log(`[DependencyChecker] Resolved paths SYNC:`, { pandocExec, typstExec, imagemagickExec });
|
||||
|
||||
const missingDeps: string[] = [];
|
||||
|
||||
@@ -122,7 +113,7 @@ ${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)
|
||||
ExportErrorHandler.showProgressNotice(message, 12000); // Show for 12 seconds (longer due to more content)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -145,7 +136,7 @@ ${dependencyResult.allAvailable
|
||||
|
||||
// Only show notice if dependencies are missing
|
||||
if (missingDeps.length > 0) {
|
||||
new Notice(
|
||||
ExportErrorHandler.showProgressNotice(
|
||||
`Typst PDF Export: Missing dependencies: ${missingDeps.join(', ')}. ` +
|
||||
`Run "Check Dependencies" command for details.`,
|
||||
8000
|
||||
|
||||
@@ -48,13 +48,11 @@ export class ExecutableChecker {
|
||||
* Try to find an executable using which command
|
||||
*/
|
||||
private static async findExecutableWithWhich(executableName: string, additionalPaths: string[] = []): Promise<string | null> {
|
||||
console.log(`[ExecutableChecker] findExecutableWithWhich: searching for "${executableName}"`);
|
||||
const { spawn } = require('child_process');
|
||||
|
||||
try {
|
||||
const stdout = await new Promise<string>((resolve, reject) => {
|
||||
const env = this.getAugmentedEnv(additionalPaths);
|
||||
console.log(`[ExecutableChecker] findExecutableWithWhich: using PATH="${env.PATH}"`);
|
||||
|
||||
const whichProcess = spawn('which', [executableName], {
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
@@ -73,7 +71,6 @@ export class ExecutableChecker {
|
||||
});
|
||||
|
||||
whichProcess.on('close', (code: number | null) => {
|
||||
console.log(`[ExecutableChecker] which ${executableName} exited with code ${code}, stdout: "${output.trim()}", stderr: "${error.trim()}"`);
|
||||
if (code === 0) {
|
||||
resolve(output);
|
||||
} else {
|
||||
@@ -82,16 +79,13 @@ export class ExecutableChecker {
|
||||
});
|
||||
|
||||
whichProcess.on('error', (err: Error) => {
|
||||
console.log(`[ExecutableChecker] which ${executableName} spawn error:`, err.message);
|
||||
reject(new Error(`Failed to spawn which process: ${err.message}`));
|
||||
});
|
||||
});
|
||||
|
||||
const foundPath = stdout.trim();
|
||||
console.log(`[ExecutableChecker] findExecutableWithWhich result: "${foundPath}"`);
|
||||
return foundPath || null;
|
||||
} catch (error) {
|
||||
console.log(`[ExecutableChecker] findExecutableWithWhich caught error:`, error.message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -100,27 +94,21 @@ export class ExecutableChecker {
|
||||
* Resolve executable path - async version
|
||||
*/
|
||||
public static async resolveExecutablePath(userPath: string | undefined, defaultName: string, additionalPaths: string[] = []): Promise<string> {
|
||||
console.log(`[ExecutableChecker] ASYNC Resolving path for ${defaultName}: userPath="${userPath}"`);
|
||||
|
||||
// If user provided a full path (contains /), use it as-is
|
||||
if (userPath && userPath.trim() !== '' && userPath.includes('/')) {
|
||||
console.log(`[ExecutableChecker] ASYNC Using user-provided full path: ${userPath}`);
|
||||
return userPath;
|
||||
}
|
||||
|
||||
// If user provided just an executable name or empty path, resolve it
|
||||
const searchName = (userPath && userPath.trim() !== '') ? userPath.trim() : defaultName;
|
||||
console.log(`[ExecutableChecker] ASYNC Searching for: ${searchName}`);
|
||||
|
||||
// Try to find the executable using which command
|
||||
const foundPath = await this.findExecutableWithWhich(searchName, additionalPaths);
|
||||
if (foundPath) {
|
||||
console.log(`[ExecutableChecker] ASYNC Found via which: ${foundPath}`);
|
||||
return foundPath;
|
||||
}
|
||||
|
||||
// Fall back to the search name (will be found via PATH if available)
|
||||
console.log(`[ExecutableChecker] ASYNC Falling back to search name: ${searchName}`);
|
||||
return searchName;
|
||||
}
|
||||
|
||||
@@ -128,17 +116,13 @@ export class ExecutableChecker {
|
||||
* Resolve executable path - sync version
|
||||
*/
|
||||
public static resolveExecutablePathSync(userPath: string | undefined, defaultName: string, additionalPaths: string[] = []): string {
|
||||
console.log(`[ExecutableChecker] SYNC Resolving path for ${defaultName}: userPath="${userPath}"`);
|
||||
|
||||
// If user provided a full path (contains /), use it as-is
|
||||
if (userPath && userPath.trim() !== '' && userPath.includes('/')) {
|
||||
console.log(`[ExecutableChecker] SYNC Using user-provided full path: ${userPath}`);
|
||||
return userPath;
|
||||
}
|
||||
|
||||
// If user provided just an executable name or empty path, resolve it
|
||||
const searchName = (userPath && userPath.trim() !== '') ? userPath.trim() : defaultName;
|
||||
console.log(`[ExecutableChecker] SYNC Searching for: ${searchName}`);
|
||||
|
||||
// Try to find the executable using which command synchronously
|
||||
const { spawnSync } = require('child_process');
|
||||
@@ -151,7 +135,6 @@ export class ExecutableChecker {
|
||||
if (result.status === 0 && result.stdout) {
|
||||
const foundPath = result.stdout.trim();
|
||||
if (foundPath) {
|
||||
console.log(`[ExecutableChecker] SYNC Found via which: ${foundPath}`);
|
||||
return foundPath;
|
||||
}
|
||||
}
|
||||
@@ -160,7 +143,6 @@ export class ExecutableChecker {
|
||||
}
|
||||
|
||||
// Fall back to the search name (will be found via PATH if available)
|
||||
console.log(`[ExecutableChecker] SYNC Falling back to search name: ${searchName}`);
|
||||
return searchName;
|
||||
}
|
||||
|
||||
|
||||
@@ -168,4 +168,59 @@ export class ExportErrorHandler {
|
||||
public static showCancellationNotice(): void {
|
||||
new Notice('Export cancelled');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show settings validation error
|
||||
*/
|
||||
public static showValidationError(field: string, validationMessage: string): void {
|
||||
new Notice(`Invalid ${field}: ${validationMessage}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show template loading error
|
||||
*/
|
||||
public static showTemplateError(error: Error | string): void {
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
new Notice(`Failed to load available templates: ${errorMessage}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show font caching error
|
||||
*/
|
||||
public static showFontError(error: Error | string, showFallback: boolean = true): void {
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
new Notice(`Font caching failed: ${errorMessage}. Using fallback fonts.`, 5000);
|
||||
|
||||
if (showFallback) {
|
||||
new Notice('Font list may be incomplete. Check debug mode for details.', 3000);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show export success notice
|
||||
*/
|
||||
public static showExportSuccess(outputPath: string): void {
|
||||
new Notice(`PDF exported successfully to ${outputPath}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show file not found warning
|
||||
*/
|
||||
public static showFileNotFoundWarning(context: string): void {
|
||||
new Notice(`Please open a ${context} file to export`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show no files warning
|
||||
*/
|
||||
public static showNoFilesWarning(context: string = 'files'): void {
|
||||
new Notice(`No ${context} to export`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show settings reset confirmation
|
||||
*/
|
||||
public static showSettingsReset(): void {
|
||||
new Notice('Settings reset to defaults');
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user