From 4498281b7de27aa32c2ab18c19bdf4cb780862e3 Mon Sep 17 00:00:00 2001 From: Jarvis Date: Thu, 25 Sep 2025 12:07:31 +0200 Subject: [PATCH] refactor: migrate TemplateManager to use vault.adapter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Replaced basePath hack with PathUtils.getVaultPath() and getPluginDir() - Replaced fs.existsSync with vault.adapter.exists() via PathUtils.fileExists() - Replaced fs.readdirSync with vault.adapter.list() for directory listing - Replaced fs.readFileSync with vault.adapter.read() for template content - Replaced path.join with PathUtils.joinPath() for consistent normalization - Removed fs import and all direct filesystem operations - All template operations now use proper Obsidian API methods 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/templates/TemplateManager.ts | 39 ++++++++++++++++---------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/src/templates/TemplateManager.ts b/src/templates/TemplateManager.ts index 0854006..c19e0a5 100644 --- a/src/templates/TemplateManager.ts +++ b/src/templates/TemplateManager.ts @@ -4,19 +4,21 @@ */ import { obsidianTypstPDFExport } from '../../main'; +import { PathUtils } from '../core/PathUtils'; import * as path from 'path'; -import * as fs from 'fs'; export class TemplateManager { private plugin: obsidianTypstPDFExport; private templatesPath: string; - + private pathUtils: PathUtils; + constructor(plugin: obsidianTypstPDFExport) { this.plugin = plugin; + this.pathUtils = new PathUtils(plugin.app); // Templates are stored in the plugin directory - this.templatesPath = path.join( - (this.plugin.app.vault.adapter as unknown as { basePath: string }).basePath, - this.plugin.manifest.dir!, + this.templatesPath = this.pathUtils.joinPath( + this.pathUtils.getVaultPath(), + this.pathUtils.getPluginDir(this.plugin.manifest), 'templates' ); } @@ -27,24 +29,26 @@ export class TemplateManager { async getAvailableTemplates(): Promise { try { // Check if templates directory exists - if (!fs.existsSync(this.templatesPath)) { + const exists = await this.pathUtils.fileExists(this.templatesPath); + if (!exists) { console.warn('Templates directory does not exist:', this.templatesPath); return ['default.typ']; } - + // Read directory contents - const files = fs.readdirSync(this.templatesPath); - + const list = await this.plugin.app.vault.adapter.list(this.templatesPath); + const files = list.files.map(filePath => path.basename(filePath)); + // Filter for .typ and .pandoc.typ files const templates = files.filter(file => { return file.endsWith('.typ') || file.endsWith('.pandoc.typ'); }); - + // Always include default if not present if (!templates.includes('default.typ')) { templates.unshift('default.typ'); } - + return templates; } catch (error) { console.error('Error loading templates:', error); @@ -56,7 +60,7 @@ export class TemplateManager { * Get the full path to a template file */ getTemplatePath(templateName: string): string { - return path.join(this.templatesPath, templateName); + return this.pathUtils.joinPath(this.templatesPath, templateName); } /** @@ -64,11 +68,7 @@ export class TemplateManager { */ async templateExists(templateName: string): Promise { const templatePath = this.getTemplatePath(templateName); - try { - return fs.existsSync(templatePath); - } catch { - return false; - } + return await this.pathUtils.fileExists(templatePath); } /** @@ -77,8 +77,9 @@ export class TemplateManager { async loadTemplate(templateName: string): Promise { const templatePath = this.getTemplatePath(templateName); try { - if (fs.existsSync(templatePath)) { - return fs.readFileSync(templatePath, 'utf-8'); + const exists = await this.pathUtils.fileExists(templatePath); + if (exists) { + return await this.plugin.app.vault.adapter.read(templatePath); } } catch (error) { console.error('Error loading template:', error);