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

96.55% Statements 28/29
90% Branches 9/10
100% Functions 8/8
96.55% Lines 28/29

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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177                                                                        103x 103x         103x       62x                         3x         3x 3x       3x                                 3x                                 3x                     3x                 20x                     20x   20x 160x 160x 10x       20x 9x               11x 11x   11x 44x 44x 8x       11x 2x             9x            
/**
 * Hugo SSG Provider
 *
 * Wraps existing Hugo implementation to fit the SSGProvider interface.
 */
 
import type {
  SSGProvider,
  ProviderMetadata,
  SSGProviderDependencies,
  SSGBinaryManager,
  SSGDevServer,
  SSGBuilder,
  SSGConfigQuerier,
  SSGSiteCreationOptions,
  SSGDetectionResult,
  SSGServerConfig,
  SSGBuildConfig,
} from '../types.js';
import { HugoDownloader } from './hugo-downloader.js';
import { HugoServer } from './hugo-server.js';
import { HugoBuilder } from './hugo-builder.js';
import { HugoConfig } from './hugo-config.js';
import { HugoUtils } from './hugo-utils.js';
import fs from 'fs-extra';
import path from 'path';
 
/**
 * Hugo Provider - Implements SSGProvider for Hugo
 */
export class HugoProvider implements SSGProvider {
  private dependencies: SSGProviderDependencies;
  private binaryManager: HugoDownloader;
  private utils: HugoUtils;
 
  constructor(dependencies: SSGProviderDependencies) {
    this.dependencies = dependencies;
    this.binaryManager = new HugoDownloader({
      pathHelper: dependencies.pathHelper,
      outputConsole: dependencies.outputConsole,
      environmentInfo: dependencies.environmentInfo,
    });
    this.utils = new HugoUtils();
  }
 
  getMetadata(): ProviderMetadata {
    return {
      type: 'hugo',
      name: 'Hugo',
      configFormats: ['toml', 'yaml', 'json'],
      requiresBinary: true,
      supportsDevServer: true,
      supportsBuild: true,
      supportsConfigQuery: true,
      version: '1.0.0',
    };
  }
 
  getBinaryManager(): SSGBinaryManager {
    return this.binaryManager;
  }
 
  createDevServer(config: SSGServerConfig): SSGDevServer {
    // Container may not be set yet during initialization
    const container = this.dependencies.container;
    Iif (!container) {
      throw new Error('Container not initialized in HugoProvider');
    }
 
    return new HugoServer(
      {
        workspacePath: config.workspacePath,
        hugover: config.version,
        config: config.configFile,
      },
      this.dependencies.pathHelper,
      this.dependencies.appConfig,
      this.dependencies.windowAdapter,
      this.dependencies.outputConsole,
      container,
      config.siteKey,
      config.workspaceKey
    );
  }
 
  createBuilder(config: SSGBuildConfig): SSGBuilder {
    return new HugoBuilder(
      {
        workspacePath: config.workspacePath,
        hugover: config.version,
        destination: config.destination,
        config: config.configFile,
        baseUrl: config.baseUrl,
      },
      this.dependencies.pathHelper
    );
  }
 
  createConfigQuerier(
    workspacePath: string,
    version: string,
    configFile?: string
  ): SSGConfigQuerier {
    return new HugoConfig(
      {
        workspacePath,
        hugover: version,
        config: configFile,
      },
      this.dependencies.pathHelper
    );
  }
 
  async createSite(options: SSGSiteCreationOptions): Promise<void> {
    await this.utils.createSiteDir(
      options.directory,
      options.title,
      options.configFormat as 'toml' | 'yaml' | 'json'
    );
  }
 
  async detectSite(directory: string): Promise<SSGDetectionResult> {
    // Check for Hugo config files
    const configFiles = [
      'config.toml',
      'config.yaml',
      'config.yml',
      'config.json',
      'hugo.toml',
      'hugo.yaml',
      'hugo.yml',
      'hugo.json',
    ];
 
    const foundConfigs: string[] = [];
 
    for (const configFile of configFiles) {
      const configPath = path.join(directory, configFile);
      if (await fs.pathExists(configPath)) {
        foundConfigs.push(configFile);
      }
    }
 
    if (foundConfigs.length > 0) {
      return {
        isDetected: true,
        confidence: 'high',
        configFiles: foundConfigs,
      };
    }
 
    // Check for Hugo-specific directories
    const hugoMarkers = ['archetypes', 'layouts', 'static', 'themes'];
    let markerCount = 0;
 
    for (const marker of hugoMarkers) {
      const markerPath = path.join(directory, marker);
      if (await fs.pathExists(markerPath)) {
        markerCount++;
      }
    }
 
    if (markerCount >= 2) {
      return {
        isDetected: true,
        confidence: 'medium',
        metadata: { markers: markerCount },
      };
    }
 
    return {
      isDetected: false,
      confidence: 'low',
    };
  }
}