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
+20 -19
+20 -19
View File
@@ -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<string[]> {
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<boolean> {
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<string | null> {
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);