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:
2 files changed
+107
-68
No files matched your search
@@ -35,15 +35,17 @@ import { TemplateManager } from './src/templates/TemplateManager';
|
|||||||
import { EmbeddedTemplateManager } from './src/templates/embeddedTemplates';
|
import { EmbeddedTemplateManager } from './src/templates/embeddedTemplates';
|
||||||
import { FolderSuggest } from './src/ui/components/FolderSuggest';
|
import { FolderSuggest } from './src/ui/components/FolderSuggest';
|
||||||
import { SUPPORTED_PAPER_SIZES } from './src/utils/paperSizeMapper';
|
import { SUPPORTED_PAPER_SIZES } from './src/utils/paperSizeMapper';
|
||||||
|
import { PluginLifecycle } from './src/plugin/PluginLifecycle';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
|
|
||||||
export class obsidianTypstPDFExport extends Plugin {
|
export class obsidianTypstPDFExport extends Plugin {
|
||||||
settings: obsidianTypstPDFExportSettings;
|
settings: obsidianTypstPDFExportSettings;
|
||||||
private converter: PandocTypstConverter;
|
converter: PandocTypstConverter;
|
||||||
templateManager: TemplateManager;
|
templateManager: TemplateManager;
|
||||||
private embeddedTemplateManager: EmbeddedTemplateManager;
|
embeddedTemplateManager: EmbeddedTemplateManager;
|
||||||
private currentExportController: AbortController | null = null;
|
currentExportController: AbortController | null = null;
|
||||||
|
private lifecycle: PluginLifecycle;
|
||||||
|
|
||||||
// Type predicate to filter for markdown TFiles
|
// Type predicate to filter for markdown TFiles
|
||||||
private isMarkdownFile(file: TAbstractFile): file is TFile {
|
private isMarkdownFile(file: TAbstractFile): file is TFile {
|
||||||
@@ -51,31 +53,11 @@ export class obsidianTypstPDFExport extends Plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async onload() {
|
async onload() {
|
||||||
await this.loadSettings();
|
// Initialize lifecycle manager
|
||||||
|
this.lifecycle = new PluginLifecycle(this);
|
||||||
|
|
||||||
// Initialize embedded template manager
|
// Initialize core plugin functionality
|
||||||
const vaultPath = (this.app.vault.adapter as any).basePath;
|
await this.lifecycle.initialize();
|
||||||
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);
|
|
||||||
|
|
||||||
// Register custom icon
|
// 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">
|
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
|
// Add settings tab
|
||||||
this.addSettingTab(new ObsidianTypstPDFExportSettingTab(this.app, this));
|
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
|
* Cache available fonts from typst to a file for quick access
|
||||||
*/
|
*/
|
||||||
private async cacheAvailableFonts(): Promise<void> {
|
async cacheAvailableFonts(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const { spawn } = require('child_process');
|
const { spawn } = require('child_process');
|
||||||
|
|
||||||
@@ -741,7 +714,7 @@ ${dependencyResult.allAvailable
|
|||||||
new Notice(message, 12000); // Show for 12 seconds (longer due to more content)
|
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
|
// Check dependencies silently on startup
|
||||||
try {
|
try {
|
||||||
const missingDeps = DependencyChecker.checkDependenciesSync(
|
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() {
|
onunload() {
|
||||||
// Cancel any ongoing exports
|
// Use lifecycle manager for cleanup
|
||||||
if (this.currentExportController) {
|
this.lifecycle.cleanup();
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user