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

97.61% Statements 41/42
87.5% Branches 21/24
100% Functions 9/9
97.56% Lines 40/41

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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205                                                                      103x 103x         103x       62x                         2x         6x 6x       6x                                   4x                                             4x       3x                 24x 24x   24x 48x 48x 6x       24x 6x               18x 18x 4x 4x   3x       3x                       15x 15x   15x 60x 60x 11x         15x 3x               12x 3x 3x 2x 2x 1x                     11x            
/**
 * Jekyll SSG Provider
 *
 * Implements SSGProvider interface for Jekyll static site generator.
 */
 
import type {
  SSGProvider,
  ProviderMetadata,
  SSGProviderDependencies,
  SSGBinaryManager,
  SSGDevServer,
  SSGBuilder,
  SSGConfigQuerier,
  SSGSiteCreationOptions,
  SSGDetectionResult,
  SSGServerConfig,
  SSGBuildConfig,
} from '../types.js';
import { JekyllDownloader } from './jekyll-downloader.js';
import { JekyllServer } from './jekyll-server.js';
import { JekyllBuilder } from './jekyll-builder.js';
import { JekyllUtils } from './jekyll-utils.js';
import fs from 'fs-extra';
import path from 'path';
 
/**
 * Jekyll Provider - Implements SSGProvider for Jekyll
 */
export class JekyllProvider implements SSGProvider {
  private dependencies: SSGProviderDependencies;
  private binaryManager: JekyllDownloader;
  private utils: JekyllUtils;
 
  constructor(dependencies: SSGProviderDependencies) {
    this.dependencies = dependencies;
    this.binaryManager = new JekyllDownloader({
      pathHelper: dependencies.pathHelper,
      outputConsole: dependencies.outputConsole,
      environmentInfo: dependencies.environmentInfo,
    });
    this.utils = new JekyllUtils();
  }
 
  getMetadata(): ProviderMetadata {
    return {
      type: 'jekyll',
      name: 'Jekyll',
      configFormats: ['yml', 'yaml'],
      requiresBinary: true, // Requires Ruby and gem installation
      supportsDevServer: true,
      supportsBuild: true,
      supportsConfigQuery: false, // YAML config parsing could be added later
      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 JekyllProvider');
    }
 
    return new JekyllServer(
      {
        workspacePath: config.workspacePath,
        version: config.version,
        config: config.configFile,
        port: config.port || 13131, // Use same port as Hugo and Eleventy
      },
      this.dependencies.pathHelper,
      this.dependencies.appConfig,
      this.dependencies.windowAdapter,
      this.dependencies.outputConsole,
      container,
      config.siteKey,
      config.workspaceKey
    );
  }
 
  createBuilder(config: SSGBuildConfig): SSGBuilder {
    return new JekyllBuilder(
      {
        workspacePath: config.workspacePath,
        version: config.version,
        destination: config.destination,
        config: config.configFile,
        baseUrl: config.baseUrl,
      },
      this.dependencies.pathHelper
    );
  }
 
  createConfigQuerier(
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    workspacePath: string,
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    version: string,
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    configFile?: string
  ): SSGConfigQuerier | null {
    // Jekyll uses YAML config files which could be parsed
    // For now, return null to keep it simple
    // This can be implemented later if needed
    return null;
  }
 
  async createSite(options: SSGSiteCreationOptions): Promise<void> {
    await this.utils.createSiteDir(
      options.directory,
      options.title,
      (options.configFormat as 'yaml' | 'yml') || 'yml'
    );
  }
 
  async detectSite(directory: string): Promise<SSGDetectionResult> {
    // Check for Jekyll config files
    const configFiles = ['_config.yml', '_config.yaml'];
    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 Gemfile with Jekyll dependency
    const gemfilePath = path.join(directory, 'Gemfile');
    if (await fs.pathExists(gemfilePath)) {
      try {
        const gemfileContent = await fs.readFile(gemfilePath, 'utf-8');
        // Look for jekyll gem declaration
        Eif (
          gemfileContent.includes("gem 'jekyll'") ||
          gemfileContent.includes('gem "jekyll"')
        ) {
          return {
            isDetected: true,
            confidence: 'high',
            metadata: { source: 'Gemfile' },
          };
        }
      } catch {
        // Ignore read errors
      }
    }
 
    // Check for common Jekyll directories
    const jekyllMarkers = ['_posts', '_layouts', '_includes', '_site'];
    let markerCount = 0;
 
    for (const marker of jekyllMarkers) {
      const markerPath = path.join(directory, marker);
      if (await fs.pathExists(markerPath)) {
        markerCount++;
      }
    }
 
    // If we have 2 or more Jekyll marker directories, it's likely a Jekyll site
    if (markerCount >= 2) {
      return {
        isDetected: true,
        confidence: 'medium',
        metadata: { markers: markerCount },
      };
    }
 
    // Check if there's at least one marker directory and some .md files
    if (markerCount >= 1) {
      try {
        const files = await fs.readdir(directory);
        const hasMdFiles = files.some((file) => file.endsWith('.md'));
        if (hasMdFiles) {
          return {
            isDetected: true,
            confidence: 'low',
            metadata: { markers: markerCount, hasMdFiles: true },
          };
        }
      } catch {
        // Ignore read errors
      }
    }
 
    return {
      isDetected: false,
      confidence: 'low',
    };
  }
}