All files / backend/src/ssg-providers provider-factory.ts

17.64% Statements 3/17
100% Branches 0/0
18.18% Functions 2/11
17.64% Lines 3/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101                                      9x   9x             9x                                                                                                                                                
/**
 * Provider Factory
 *
 * Factory for creating SSG provider instances and components.
 * Similar to SyncFactory pattern.
 */
 
import type { SSGProvider, SSGProviderDependencies, SSGServerConfig, SSGBuildConfig } from './types.js';
import type { AppContainer } from '../config/container.js';
import { ProviderRegistry } from './provider-registry.js';
 
/**
 * Provider Factory - Main entry point for working with SSG providers
 */
export class ProviderFactory {
  private registry: ProviderRegistry;
  private initPromise: Promise<void>;
 
  constructor(dependencies: SSGProviderDependencies) {
    this.registry = new ProviderRegistry(dependencies);
    // Register built-in providers (Hugo, etc.) - async to handle dynamic imports
    this.initPromise = this.registry.registerBuiltInProviders();
  }
 
  /**
   * Set container reference after construction (to break circular dependency)
   */
  setContainer(container: AppContainer): void {
    this.registry.setContainer(container);
  }
 
  /**
   * Ensure providers are initialized
   * Called internally before provider access
   */
  private async ensureInitialized(): Promise<void> {
    await this.initPromise;
  }
 
  /**
   * Get provider instance by type
   */
  async getProvider(ssgType: string): Promise<SSGProvider> {
    await this.ensureInitialized();
    return this.registry.getProvider(ssgType);
  }
 
  /**
   * Get provider registry for advanced operations
   */
  getRegistry(): ProviderRegistry {
    return this.registry;
  }
 
  /**
   * Create a dev server for the given SSG type
   */
  async createDevServer(ssgType: string, config: SSGServerConfig) {
    const provider = await this.getProvider(ssgType);
    return provider.createDevServer(config);
  }
 
  /**
   * Create a builder for the given SSG type
   */
  async createBuilder(ssgType: string, config: SSGBuildConfig) {
    const provider = await this.getProvider(ssgType);
    return provider.createBuilder(config);
  }
 
  /**
   * Get binary manager for the given SSG type (if applicable)
   */
  async getBinaryManager(ssgType: string) {
    const provider = await this.getProvider(ssgType);
    return provider.getBinaryManager();
  }
 
  /**
   * Auto-detect SSG type from workspace
   */
  async detectSSGType(workspacePath: string): Promise<string | null> {
    return this.registry.detectSSGType(workspacePath);
  }
 
  /**
   * Get all available provider metadata
   */
  getAllProviderMetadata() {
    return this.registry.getAllProviderMetadata();
  }
 
  /**
   * Check if a provider type is available
   */
  async hasProvider(ssgType: string): Promise<boolean> {
    await this.ensureInitialized();
    return this.registry.hasProvider(ssgType);
  }
}