|
|
|
@@ -11,7 +11,7 @@
|
|
|
|
|
* - Error handling
|
|
|
|
|
*
|
|
|
|
|
* The converter supports both file-based and content-based conversions, automatically
|
|
|
|
|
* managing temporary directories and cleanup on process termination.
|
|
|
|
|
* managing temporary directories with cleanup via the plugin lifecycle.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { PandocOptions, TypstSettings, ConversionResult, ProgressCallback } from './converterTypes';
|
|
|
|
@@ -33,7 +33,7 @@ import { PandocExecutor } from './pandoc/PandocExecutor';
|
|
|
|
|
* Key features:
|
|
|
|
|
* - File and content-based conversion support
|
|
|
|
|
* - Automatic temporary directory management
|
|
|
|
|
* - Process cleanup on termination (exit, SIGINT, SIGTERM, exceptions)
|
|
|
|
|
* - Cleanup via plugin lifecycle (onunload)
|
|
|
|
|
* - Progress callback support for UI updates
|
|
|
|
|
* - Configurable Pandoc and Typst settings
|
|
|
|
|
*
|
|
|
|
@@ -41,8 +41,8 @@ import { PandocExecutor } from './pandoc/PandocExecutor';
|
|
|
|
|
* 1. Constructor initializes command builder and executor
|
|
|
|
|
* 2. Conversion methods create temp directories and build commands
|
|
|
|
|
* 3. Executor runs Pandoc with Typst engine
|
|
|
|
|
* 4. Cleanup automatically removes temporary files
|
|
|
|
|
* 5. dispose() for manual cleanup when done
|
|
|
|
|
* 4. Plugin unload calls dispose() to remove temporary files
|
|
|
|
|
* 5. dispose() can also be called manually for cleanup when done
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* ```typescript
|
|
|
|
@@ -75,15 +75,14 @@ export class PandocTypstConverter {
|
|
|
|
|
private plugin: obsidianTypstPDFExport;
|
|
|
|
|
private commandBuilder: PandocCommandBuilder;
|
|
|
|
|
private pandocExecutor: PandocExecutor;
|
|
|
|
|
private processCleanupHandler: (() => void) | null = null;
|
|
|
|
|
private isDisposed = false;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new PandocTypstConverter instance with configuration.
|
|
|
|
|
*
|
|
|
|
|
* Initializes the converter with the plugin instance and configuration options
|
|
|
|
|
* for both Pandoc and Typst. Sets up cleanup handlers to ensure temporary
|
|
|
|
|
* files are removed on process termination.
|
|
|
|
|
* for both Pandoc and Typst. Temporary files are cleaned up via dispose()
|
|
|
|
|
* which is called during plugin unload.
|
|
|
|
|
*
|
|
|
|
|
* @param plugin - The main plugin instance for accessing app and settings
|
|
|
|
|
* @param pandocOptions - Configuration options for Pandoc conversion (template, variables, etc.)
|
|
|
|
@@ -116,8 +115,6 @@ export class PandocTypstConverter {
|
|
|
|
|
this.plugin = plugin;
|
|
|
|
|
this.commandBuilder = new PandocCommandBuilder(plugin);
|
|
|
|
|
this.pandocExecutor = new PandocExecutor(plugin);
|
|
|
|
|
// Set up cleanup handlers for process termination
|
|
|
|
|
this.setupCleanup();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@@ -282,38 +279,6 @@ export class PandocTypstConverter {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets up automatic cleanup handlers for process termination events.
|
|
|
|
|
*
|
|
|
|
|
* Registers the cleanup method to run on various process termination events
|
|
|
|
|
* to ensure temporary files are removed even if the process exits unexpectedly.
|
|
|
|
|
* Handlers are registered for:
|
|
|
|
|
* - Normal exit
|
|
|
|
|
* - SIGINT (Ctrl+C)
|
|
|
|
|
* - SIGTERM (kill command)
|
|
|
|
|
*
|
|
|
|
|
* Note: Handlers are stored and removed on dispose() to prevent listener leaks.
|
|
|
|
|
* The uncaughtException handler is NOT registered to avoid swallowing errors.
|
|
|
|
|
*
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
private setupCleanup(): void {
|
|
|
|
|
// Store handler reference so we can remove it later
|
|
|
|
|
this.processCleanupHandler = () => {
|
|
|
|
|
// Only cleanup if not already disposed to avoid double-cleanup
|
|
|
|
|
if (!this.isDisposed) {
|
|
|
|
|
void this.cleanup();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Register cleanup handlers - these will be removed on dispose()
|
|
|
|
|
process.on('exit', this.processCleanupHandler);
|
|
|
|
|
process.on('SIGINT', this.processCleanupHandler);
|
|
|
|
|
process.on('SIGTERM', this.processCleanupHandler);
|
|
|
|
|
// Note: uncaughtException handler removed to avoid swallowing errors
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cleans up temporary files and directories.
|
|
|
|
|
*
|
|
|
|
@@ -321,7 +286,7 @@ export class PandocTypstConverter {
|
|
|
|
|
* directory created by the converter. Cleanup failures are logged but don't throw
|
|
|
|
|
* to avoid interfering with normal operation or other cleanup operations.
|
|
|
|
|
*
|
|
|
|
|
* Called automatically on process termination or manually via dispose().
|
|
|
|
|
* Called via dispose() during plugin unload.
|
|
|
|
|
*
|
|
|
|
|
* @private
|
|
|
|
|
* @returns Promise that resolves when cleanup is complete
|
|
|
|
@@ -413,14 +378,6 @@ export class PandocTypstConverter {
|
|
|
|
|
|
|
|
|
|
this.isDisposed = true;
|
|
|
|
|
|
|
|
|
|
// Remove process event listeners to prevent memory leaks
|
|
|
|
|
if (this.processCleanupHandler) {
|
|
|
|
|
process.off('exit', this.processCleanupHandler);
|
|
|
|
|
process.off('SIGINT', this.processCleanupHandler);
|
|
|
|
|
process.off('SIGTERM', this.processCleanupHandler);
|
|
|
|
|
this.processCleanupHandler = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Cleanup temporary files and resources
|
|
|
|
|
await this.cleanup();
|
|
|
|
|
}
|
|
|
|
|