refactor: extract PluginLifecycle from main.ts

Extract plugin initialization and cleanup logic into dedicated
PluginLifecycle class to reduce complexity of main.ts file.

- Create src/plugin/PluginLifecycle.ts (~100 lines)
- Move onload/onunload initialization logic
- Move template extraction and component setup
- Move dependency checking and font caching
- Update main.ts to delegate to lifecycle manager
- Change methods to public visibility where needed

Part of comprehensive refactoring plan to break down large files
(main.ts: 1792 lines → 6 focused modules). Step 1.1 complete.

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Jarvis committed 2025-09-08 19:14:11 +02:00
1 parent ebdfea0a33
commit 991a9ae0ec
2 files changed
+107 -68

No files matched your search

+13 -68
View File
@@ -35,15 +35,17 @@ import { TemplateManager } from './src/templates/TemplateManager';
import { EmbeddedTemplateManager } from './src/templates/embeddedTemplates';
import { FolderSuggest } from './src/ui/components/FolderSuggest';
import { SUPPORTED_PAPER_SIZES } from './src/utils/paperSizeMapper';
import { PluginLifecycle } from './src/plugin/PluginLifecycle';
import * as path from 'path';
import * as fs from 'fs';
export class obsidianTypstPDFExport extends Plugin {
settings: obsidianTypstPDFExportSettings;
private converter: PandocTypstConverter;
converter: PandocTypstConverter;
templateManager: TemplateManager;
private embeddedTemplateManager: EmbeddedTemplateManager;
private currentExportController: AbortController | null = null;
embeddedTemplateManager: EmbeddedTemplateManager;
currentExportController: AbortController | null = null;
private lifecycle: PluginLifecycle;
// Type predicate to filter for markdown TFiles
private isMarkdownFile(file: TAbstractFile): file is TFile {
@@ -51,31 +53,11 @@ export class obsidianTypstPDFExport extends Plugin {
}
async onload() {
await this.loadSettings();
// Initialize lifecycle manager
this.lifecycle = new PluginLifecycle(this);
// Initialize embedded template manager
const vaultPath = (this.app.vault.adapter as any).basePath;
const pluginDir = path.join(vaultPath, this.manifest.dir!);
this.embeddedTemplateManager = new EmbeddedTemplateManager(pluginDir);
// Extract any missing templates from embedded data
try {
const extractionResult = this.embeddedTemplateManager.extractAllMissingTemplates();
if (extractionResult.failed.length > 0) {
console.warn(`Failed to extract ${extractionResult.failed.length} templates:`, extractionResult.failed);
new Notice(`Warning: Failed to extract some templates. Plugin may not work correctly.`);
}
} catch (error) {
console.error('Error during template extraction:', error);
new Notice(`Error extracting templates: ${error.message}`);
}
// Initialize components with executable paths from settings
this.converter = new PandocTypstConverter(this, {
pandocPath: this.settings.pandocPath,
typstPath: this.settings.typstPath
});
this.templateManager = new TemplateManager(this);
// Initialize core plugin functionality
await this.lifecycle.initialize();
// Register custom icon
addIcon('typst-pdf-export', `<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" xml:space="preserve" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:2">
@@ -96,15 +78,6 @@ export class obsidianTypstPDFExport extends Plugin {
// Add settings tab
this.addSettingTab(new ObsidianTypstPDFExportSettingTab(this.app, this));
// Clean up any leftover temp directories from previous sessions
this.cleanupStartupTempDirectories();
// Check dependencies on startup (async, don't await)
this.checkDependenciesAsync();
// Cache available fonts (async, don't await)
this.cacheAvailableFonts();
}
/**
@@ -146,7 +119,7 @@ export class obsidianTypstPDFExport extends Plugin {
/**
* Cache available fonts from typst to a file for quick access
*/
private async cacheAvailableFonts(): Promise<void> {
async cacheAvailableFonts(): Promise<void> {
try {
const { spawn } = require('child_process');
@@ -741,7 +714,7 @@ ${dependencyResult.allAvailable
new Notice(message, 12000); // Show for 12 seconds (longer due to more content)
}
private async checkDependenciesAsync(): Promise<void> {
async checkDependenciesAsync(): Promise<void> {
// Check dependencies silently on startup
try {
const missingDeps = DependencyChecker.checkDependenciesSync(
@@ -1324,38 +1297,10 @@ ${dependencyResult.allAvailable
}
}
/**
* Clean up leftover temp directories from previous sessions
*/
private cleanupStartupTempDirectories(): void {
try {
const vaultPath = (this.app.vault.adapter as any).basePath;
const cleanupManager = TempDirectoryManager.create(vaultPath, this.app.vault.configDir);
const result = cleanupManager.cleanupAllTempDirs();
if (this.settings.behavior.debugMode) {
}
} catch (error) {
console.warn('Export: Startup temp directory cleanup failed (non-critical):', error);
// Don't throw - this shouldn't prevent plugin from loading
}
}
onunload() {
// Cancel any ongoing exports
if (this.currentExportController) {
this.currentExportController.abort();
}
// Clean up temp directories on plugin unload
try {
const vaultPath = (this.app.vault.adapter as any).basePath;
const cleanupManager = TempDirectoryManager.create(vaultPath, this.app.vault.configDir);
cleanupManager.cleanupAllTempDirs();
} catch (error) {
console.warn('Export: Failed to clean up temp directories during unload:', error);
}
// Use lifecycle manager for cleanup
this.lifecycle.cleanup();
}
}
+94
View File
@@ -0,0 +1,94 @@
/**
* Plugin Lifecycle Management
* Handles plugin loading, unloading, initialization, and cleanup
*/
import { Notice } from 'obsidian';
import type { obsidianTypstPDFExport } from '../../main';
import { PandocTypstConverter } from '../converters/PandocTypstConverter';
import { TemplateManager } from '../templates/TemplateManager';
import { EmbeddedTemplateManager } from '../templates/embeddedTemplates';
import { TempDirectoryManager } from '../core/TempDirectoryManager';
import * as path from 'path';
export class PluginLifecycle {
constructor(private plugin: obsidianTypstPDFExport) {}
/**
* Initialize the plugin during onload
*/
async initialize(): Promise<void> {
// Load settings first
await this.plugin.loadSettings();
// Initialize embedded template manager
const vaultPath = (this.plugin.app.vault.adapter as any).basePath;
const pluginDir = path.join(vaultPath, this.plugin.manifest.dir!);
this.plugin.embeddedTemplateManager = new EmbeddedTemplateManager(pluginDir);
// Extract any missing templates from embedded data
try {
const extractionResult = this.plugin.embeddedTemplateManager.extractAllMissingTemplates();
if (extractionResult.failed.length > 0) {
console.warn(`Failed to extract ${extractionResult.failed.length} templates:`, extractionResult.failed);
new Notice(`Warning: Failed to extract some templates. Plugin may not work correctly.`);
}
} catch (error) {
console.error('Error during template extraction:', error);
new Notice(`Error extracting templates: ${error.message}`);
}
// Initialize components with executable paths from settings
this.plugin.converter = new PandocTypstConverter(this.plugin, {
pandocPath: this.plugin.settings.pandocPath,
typstPath: this.plugin.settings.typstPath
});
this.plugin.templateManager = new TemplateManager(this.plugin);
// Clean up any leftover temp directories from previous sessions
this.cleanupStartupTempDirectories();
// Check dependencies on startup (async, don't await)
this.plugin.checkDependenciesAsync();
// Cache available fonts (async, don't await)
this.plugin.cacheAvailableFonts();
}
/**
* Clean up resources during onunload
*/
cleanup(): void {
// Cancel any ongoing exports
if (this.plugin.currentExportController) {
this.plugin.currentExportController.abort();
}
// Clean up temp directories on plugin unload
try {
const vaultPath = (this.plugin.app.vault.adapter as any).basePath;
const cleanupManager = TempDirectoryManager.create(vaultPath, this.plugin.app.vault.configDir);
cleanupManager.cleanupAllTempDirs();
} catch (error) {
console.warn('Export: Failed to clean up temp directories during unload:', error);
}
}
/**
* Clean up leftover temp directories from previous sessions
*/
private cleanupStartupTempDirectories(): void {
try {
const vaultPath = (this.plugin.app.vault.adapter as any).basePath;
const cleanupManager = TempDirectoryManager.create(vaultPath, this.plugin.app.vault.configDir);
const result = cleanupManager.cleanupAllTempDirs();
if (this.plugin.settings.behavior.debugMode) {
// Debug logging was here but empty in original
}
} catch (error) {
console.warn('Export: Startup temp directory cleanup failed (non-critical):', error);
// Don't throw - this shouldn't prevent plugin from loading
}
}
}