All files / backend/src/site-sources site-source-factory.ts

85.71% Statements 6/7
50% Branches 1/2
100% Functions 3/3
85.71% Lines 6/7

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                                                                            9x                       11x 11x                     11x   11x   11x            
/**
 * Site Source Factory
 *
 * Factory for creating site source instances based on source configuration type.
 * Currently supports 'folder' type sources.
 */
 
import type { Workspace } from '@quiqr/types';
import type { PathHelper } from '../utils/path-helper.js';
import { FolderSiteSource } from './folder-site-source.js';
 
/**
 * Source configuration for a site
 */
export interface SourceConfig {
  type: string;
  path: string;
  key?: string;
  [key: string]: unknown;
}
 
/**
 * Generic site source interface
 * All site source implementations must implement this interface
 */
export interface SiteSource {
  listWorkspaces(): Promise<Workspace[]>;
  mountWorkspace(workspaceKey: string): Promise<void>;
  update?(): Promise<void>;
}
 
/**
 * SiteSourceFactory creates site source instances based on source configuration
 */
export class SiteSourceFactory {
  private pathHelper: PathHelper;
 
  constructor(pathHelper: PathHelper) {
    this.pathHelper = pathHelper;
  }
 
  /**
   * Get a site source instance for the given configuration
   *
   * @param key - The site key
   * @param config - The source configuration
   * @returns A site source instance
   * @throws Error if the source type is not implemented
   */
  get(key: string, config: SourceConfig): SiteSource {
    const Type = this.getType(config);
    return new Type({ ...config, key }, this.pathHelper);
  }
 
  /**
   * Get the site source class for the given configuration
   *
   * @param config - The source configuration
   * @returns The site source class constructor
   * @throws Error if the source type is not implemented
   */
  private getType(config: SourceConfig): new (config: SourceConfig, pathHelper: PathHelper) => SiteSource {
    const type = config.type.toLowerCase();
 
    if (type === 'folder') {
      // Use new ESM implementation
      return FolderSiteSource;
    } else E{
      throw new Error(`Site source (${config.type}) not implemented.`);
    }
  }
}