refactor: migrate embeddedTemplates.ts to use PathUtils and vault.adapter
- Replace fs operations with vault.adapter methods - Update constructor to require App parameter - Make all methods async for vault.adapter compatibility - Update PluginLifecycle.ts to match new async API - Remove direct fs/path module dependencies - Build and lint pass successfully Safety commit 16/16 - Path migration complete!
This commit is contained in:
2 files changed
+36
-36
No files matched your search
@@ -28,11 +28,11 @@ export class PluginLifecycle {
|
|||||||
// Initialize embedded template manager
|
// Initialize embedded template manager
|
||||||
const vaultPath = this.pathUtils.getVaultPath();
|
const vaultPath = this.pathUtils.getVaultPath();
|
||||||
const pluginDir = this.pathUtils.joinPath(vaultPath, this.pathUtils.getPluginDir(this.plugin.manifest));
|
const pluginDir = this.pathUtils.joinPath(vaultPath, this.pathUtils.getPluginDir(this.plugin.manifest));
|
||||||
this.plugin.embeddedTemplateManager = new EmbeddedTemplateManager(pluginDir);
|
this.plugin.embeddedTemplateManager = new EmbeddedTemplateManager(pluginDir, this.plugin.app);
|
||||||
|
|
||||||
// Extract any missing templates from embedded data
|
// Extract any missing templates from embedded data
|
||||||
try {
|
try {
|
||||||
const extractionResult = this.plugin.embeddedTemplateManager.extractAllMissingTemplates();
|
const extractionResult = await this.plugin.embeddedTemplateManager.extractAllMissingTemplates();
|
||||||
if (extractionResult.failed.length > 0) {
|
if (extractionResult.failed.length > 0) {
|
||||||
console.warn(`Failed to extract ${extractionResult.failed.length} templates:`, extractionResult.failed);
|
console.warn(`Failed to extract ${extractionResult.failed.length} templates:`, extractionResult.failed);
|
||||||
ExportErrorHandler.showTemplateError(`Failed to extract some templates. Plugin may not work correctly.`);
|
ExportErrorHandler.showTemplateError(`Failed to extract some templates. Plugin may not work correctly.`);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { extractTemplate, getAllTemplateNames } from 'template-embed:templates';
|
import { extractTemplate, getAllTemplateNames } from 'template-embed:templates';
|
||||||
import * as fs from 'fs';
|
import { PathUtils } from '../core/PathUtils';
|
||||||
import * as path from 'path';
|
import type { App } from 'obsidian';
|
||||||
|
|
||||||
export interface TemplateInfo {
|
export interface TemplateInfo {
|
||||||
name: string;
|
name: string;
|
||||||
@@ -14,23 +14,27 @@ export interface TemplateInfo {
|
|||||||
export class EmbeddedTemplateManager {
|
export class EmbeddedTemplateManager {
|
||||||
private pluginDir: string;
|
private pluginDir: string;
|
||||||
private templatesDir: string;
|
private templatesDir: string;
|
||||||
|
private pathUtils: PathUtils;
|
||||||
|
private app: App;
|
||||||
|
|
||||||
constructor(pluginDir: string) {
|
constructor(pluginDir: string, app: App) {
|
||||||
this.pluginDir = pluginDir;
|
this.pluginDir = pluginDir;
|
||||||
this.templatesDir = path.join(pluginDir, 'templates');
|
this.app = app;
|
||||||
|
this.pathUtils = new PathUtils(app);
|
||||||
|
this.templatesDir = this.pathUtils.joinPath(pluginDir, 'templates');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get information about all templates
|
* Get information about all templates
|
||||||
*/
|
*/
|
||||||
getTemplateInfo(): TemplateInfo[] {
|
async getTemplateInfo(): Promise<TemplateInfo[]> {
|
||||||
const embeddedNames = getAllTemplateNames();
|
const embeddedNames = getAllTemplateNames();
|
||||||
const templateInfo: TemplateInfo[] = [];
|
const templateInfo: TemplateInfo[] = [];
|
||||||
|
|
||||||
for (const name of embeddedNames) {
|
for (const name of embeddedNames) {
|
||||||
const filePath = path.join(this.templatesDir, name);
|
const filePath = this.pathUtils.joinPath(this.templatesDir, name);
|
||||||
const exists = fs.existsSync(filePath);
|
const exists = await this.pathUtils.fileExists(filePath);
|
||||||
|
|
||||||
templateInfo.push({
|
templateInfo.push({
|
||||||
name,
|
name,
|
||||||
exists,
|
exists,
|
||||||
@@ -44,26 +48,24 @@ export class EmbeddedTemplateManager {
|
|||||||
/**
|
/**
|
||||||
* Extract a single template if it doesn't exist
|
* Extract a single template if it doesn't exist
|
||||||
*/
|
*/
|
||||||
extractTemplateIfNeeded(templateName: string): boolean {
|
async extractTemplateIfNeeded(templateName: string): Promise<boolean> {
|
||||||
const filePath = path.join(this.templatesDir, templateName);
|
const filePath = this.pathUtils.joinPath(this.templatesDir, templateName);
|
||||||
|
|
||||||
// Check if template already exists
|
// Check if template already exists
|
||||||
if (fs.existsSync(filePath)) {
|
if (await this.pathUtils.fileExists(filePath)) {
|
||||||
return false; // No extraction needed
|
return false; // No extraction needed
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Ensure templates directory exists
|
// Ensure templates directory exists
|
||||||
if (!fs.existsSync(this.templatesDir)) {
|
await this.pathUtils.ensureDir(this.templatesDir);
|
||||||
fs.mkdirSync(this.templatesDir, { recursive: true });
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract template content
|
// Extract template content
|
||||||
const content = extractTemplate(templateName, filePath);
|
const content = extractTemplate(templateName, filePath);
|
||||||
|
|
||||||
// Write template to disk
|
// Write template to disk using vault.adapter
|
||||||
fs.writeFileSync(filePath, content, 'utf8');
|
await this.app.vault.adapter.write(filePath, content);
|
||||||
|
|
||||||
return true; // Extraction successful
|
return true; // Extraction successful
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Failed to extract template ${templateName}:`, error);
|
console.error(`Failed to extract template ${templateName}:`, error);
|
||||||
@@ -74,15 +76,15 @@ export class EmbeddedTemplateManager {
|
|||||||
/**
|
/**
|
||||||
* Extract all missing templates
|
* Extract all missing templates
|
||||||
*/
|
*/
|
||||||
extractAllMissingTemplates(): { extracted: string[], failed: string[] } {
|
async extractAllMissingTemplates(): Promise<{ extracted: string[], failed: string[] }> {
|
||||||
const extracted: string[] = [];
|
const extracted: string[] = [];
|
||||||
const failed: string[] = [];
|
const failed: string[] = [];
|
||||||
|
|
||||||
const templateInfo = this.getTemplateInfo();
|
const templateInfo = await this.getTemplateInfo();
|
||||||
|
|
||||||
for (const info of templateInfo) {
|
for (const info of templateInfo) {
|
||||||
if (info.needsExtraction) {
|
if (info.needsExtraction) {
|
||||||
const success = this.extractTemplateIfNeeded(info.name);
|
const success = await this.extractTemplateIfNeeded(info.name);
|
||||||
if (success) {
|
if (success) {
|
||||||
extracted.push(info.name);
|
extracted.push(info.name);
|
||||||
} else {
|
} else {
|
||||||
@@ -97,23 +99,21 @@ export class EmbeddedTemplateManager {
|
|||||||
/**
|
/**
|
||||||
* Force extract all templates (overwrites existing ones)
|
* Force extract all templates (overwrites existing ones)
|
||||||
*/
|
*/
|
||||||
forceExtractAllTemplates(): { extracted: string[], failed: string[] } {
|
async forceExtractAllTemplates(): Promise<{ extracted: string[], failed: string[] }> {
|
||||||
const extracted: string[] = [];
|
const extracted: string[] = [];
|
||||||
const failed: string[] = [];
|
const failed: string[] = [];
|
||||||
|
|
||||||
const embeddedNames = getAllTemplateNames();
|
const embeddedNames = getAllTemplateNames();
|
||||||
|
|
||||||
// Ensure templates directory exists
|
// Ensure templates directory exists
|
||||||
if (!fs.existsSync(this.templatesDir)) {
|
await this.pathUtils.ensureDir(this.templatesDir);
|
||||||
fs.mkdirSync(this.templatesDir, { recursive: true });
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const templateName of embeddedNames) {
|
for (const templateName of embeddedNames) {
|
||||||
try {
|
try {
|
||||||
const filePath = path.join(this.templatesDir, templateName);
|
const filePath = this.pathUtils.joinPath(this.templatesDir, templateName);
|
||||||
const content = extractTemplate(templateName, filePath);
|
const content = extractTemplate(templateName, filePath);
|
||||||
|
|
||||||
fs.writeFileSync(filePath, content, 'utf8');
|
await this.app.vault.adapter.write(filePath, content);
|
||||||
extracted.push(templateName);
|
extracted.push(templateName);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Failed to force extract template ${templateName}:`, error);
|
console.error(`Failed to force extract template ${templateName}:`, error);
|
||||||
|
|||||||
Reference in new issue
Block a user