refactor: migrate TemplateManager to use vault.adapter

- 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 <noreply@anthropic.com>
This commit is contained in:
Jarvis committed 2025-09-25 12:07:31 +02:00
1 parent d4d5401293
commit 4498281b7d
1 file changed
+15 -14
+15 -14
View File
@@ -4,19 +4,21 @@
*/ */
import { obsidianTypstPDFExport } from '../../main'; import { obsidianTypstPDFExport } from '../../main';
import { PathUtils } from '../core/PathUtils';
import * as path from 'path'; import * as path from 'path';
import * as fs from 'fs';
export class TemplateManager { export class TemplateManager {
private plugin: obsidianTypstPDFExport; private plugin: obsidianTypstPDFExport;
private templatesPath: string; private templatesPath: string;
private pathUtils: PathUtils;
constructor(plugin: obsidianTypstPDFExport) { constructor(plugin: obsidianTypstPDFExport) {
this.plugin = plugin; this.plugin = plugin;
this.pathUtils = new PathUtils(plugin.app);
// Templates are stored in the plugin directory // Templates are stored in the plugin directory
this.templatesPath = path.join( this.templatesPath = this.pathUtils.joinPath(
(this.plugin.app.vault.adapter as unknown as { basePath: string }).basePath, this.pathUtils.getVaultPath(),
this.plugin.manifest.dir!, this.pathUtils.getPluginDir(this.plugin.manifest),
'templates' 'templates'
); );
} }
@@ -27,13 +29,15 @@ export class TemplateManager {
async getAvailableTemplates(): Promise<string[]> { async getAvailableTemplates(): Promise<string[]> {
try { try {
// Check if templates directory exists // 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); console.warn('Templates directory does not exist:', this.templatesPath);
return ['default.typ']; return ['default.typ'];
} }
// Read directory contents // 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 // Filter for .typ and .pandoc.typ files
const templates = files.filter(file => { const templates = files.filter(file => {
@@ -56,7 +60,7 @@ export class TemplateManager {
* Get the full path to a template file * Get the full path to a template file
*/ */
getTemplatePath(templateName: string): string { 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<boolean> { async templateExists(templateName: string): Promise<boolean> {
const templatePath = this.getTemplatePath(templateName); const templatePath = this.getTemplatePath(templateName);
try { return await this.pathUtils.fileExists(templatePath);
return fs.existsSync(templatePath);
} catch {
return false;
}
} }
/** /**
@@ -77,8 +77,9 @@ export class TemplateManager {
async loadTemplate(templateName: string): Promise<string | null> { async loadTemplate(templateName: string): Promise<string | null> {
const templatePath = this.getTemplatePath(templateName); const templatePath = this.getTemplatePath(templateName);
try { try {
if (fs.existsSync(templatePath)) { const exists = await this.pathUtils.fileExists(templatePath);
return fs.readFileSync(templatePath, 'utf-8'); if (exists) {
return await this.plugin.app.vault.adapter.read(templatePath);
} }
} catch (error) { } catch (error) {
console.error('Error loading template:', error); console.error('Error loading template:', error);