UNPKG

@docfy/ember-vite

Version:

Vite plugin for Docfy Ember integration with @embroider/vite

64 lines (63 loc) 2.13 kB
import path from 'path'; import fs from 'fs'; import debugFactory from 'debug'; const debug = debugFactory('@docfy/ember-vite:file-manager'); export class FileManager { config; context; constructor(config, context) { this.config = config; this.context = context; } isDevMode() { return this.config.command === 'serve'; } writeFiles(files) { // Always write to disk so Embroider can process the files // Never emit as final assets - these are intermediate files this.writeFilesToDisk(files); } writeJsonToPublic(data, fileName = 'docfy-urls.json') { const content = JSON.stringify(data); if (this.isDevMode()) { const publicDir = path.join(process.cwd(), 'public'); if (!fs.existsSync(publicDir)) { fs.mkdirSync(publicDir, { recursive: true }); } fs.writeFileSync(path.join(publicDir, fileName), content); debug('Wrote JSON to public folder (dev)', { fileName }); } else { this.context?.emitFile({ type: 'asset', fileName, source: content, }); debug('Emitted JSON asset (build)', { fileName }); } } writeFilesToDisk(files) { files.forEach(file => { const fullPath = path.join(process.cwd(), file.path); const dir = path.dirname(fullPath); if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { recursive: true }); } fs.writeFileSync(fullPath, file.content); debug('Wrote file to disk', { path: file.path }); }); } emitStaticAssets(staticAssets) { if (!this.context || this.isDevMode()) { return; } staticAssets.forEach(asset => { this.context.emitFile({ type: 'asset', fileName: asset.toPath, source: fs.readFileSync(asset.fromPath), }); }); debug('Emitted static assets', { count: staticAssets.length }); } }