UNPKG

@docfy/ember-vite

Version:

Vite plugin for Docfy Ember integration with @embroider/vite

84 lines (83 loc) 2.93 kB
import Docfy from '@docfy/core'; import { generatePage } from './template-generator.js'; import { getDocfySourceFiles } from './utils.js'; import debugFactory from 'debug'; const debug = debugFactory('@docfy/ember-vite:processor'); export class DocfyProcessor { config; docfyConfig; docfyInstance; fileManager; currentResult = null; constructor(config, docfyConfig, fileManager) { this.config = config; this.docfyConfig = docfyConfig; this.docfyInstance = new Docfy(docfyConfig); this.fileManager = fileManager; } async processAll() { debug('Processing all markdown files...'); try { const result = await this.docfyInstance.run(this.docfyConfig.sources); this.currentResult = result; debug('Markdown processing completed', { contentCount: result.content.length, staticAssetsCount: result.staticAssets.length, }); this.generateTemplates(result); this.handleAssets(result); return result; } catch (error) { debug('Error processing markdown files', { error }); throw error; } } async processChangedFile(filePath) { debug('Processing changed file', { filePath }); try { const result = await this.docfyInstance.run(this.docfyConfig.sources); this.currentResult = result; const changedPage = result.content.find((page) => { return page.vFile.path === filePath; }); if (changedPage) { debug('Found changed page', { url: changedPage.meta.url }); this.generateTemplateForPage(changedPage); } this.handleAssets(result); return result; } catch (error) { debug('Error processing changed file', { filePath, error }); throw error; } } async getSourceFiles() { return getDocfySourceFiles(this.docfyConfig, process.cwd()); } getCurrentResult() { return this.currentResult; } generateTemplates(result) { debug('Generating templates', { pageCount: result.content.length }); result.content.forEach((page) => { this.generateTemplateForPage(page); }); } generateTemplateForPage(page) { const filesToGenerate = generatePage(page); debug('Generated files for page', { url: page.meta.url, fileCount: filesToGenerate.length, }); this.fileManager.writeFiles(filesToGenerate); } handleAssets(result) { const urls = result.content.map((page) => page.meta.url); this.fileManager.writeJsonToPublic(urls); if (!this.fileManager.isDevMode()) { this.fileManager.emitStaticAssets(result.staticAssets); } } }