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

97.22% Statements 35/36
95% Branches 19/20
100% Functions 8/8
97.22% Lines 35/36

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                                                                      100x 100x         100x       62x                         2x         6x 6x       6x                                   4x                                           4x       2x                 21x             21x   21x 84x 84x 8x       21x 5x               16x 16x 6x 6x   4x     6x 3x                       13x 13x   13x 39x 39x 8x       13x 3x             10x            
/**
 * Eleventy SSG Provider
 *
 * Implements SSGProvider interface for Eleventy static site generator.
 */
 
import type {
  SSGProvider,
  ProviderMetadata,
  SSGProviderDependencies,
  SSGBinaryManager,
  SSGDevServer,
  SSGBuilder,
  SSGConfigQuerier,
  SSGSiteCreationOptions,
  SSGDetectionResult,
  SSGServerConfig,
  SSGBuildConfig,
} from '../types.js';
import { EleventyDownloader } from './eleventy-downloader.js';
import { EleventyServer } from './eleventy-server.js';
import { EleventyBuilder } from './eleventy-builder.js';
import { EleventyUtils } from './eleventy-utils.js';
import fs from 'fs-extra';
import path from 'path';
 
/**
 * Eleventy Provider - Implements SSGProvider for Eleventy
 */
export class EleventyProvider implements SSGProvider {
  private dependencies: SSGProviderDependencies;
  private binaryManager: EleventyDownloader;
  private utils: EleventyUtils;
 
  constructor(dependencies: SSGProviderDependencies) {
    this.dependencies = dependencies;
    this.binaryManager = new EleventyDownloader({
      pathHelper: dependencies.pathHelper,
      outputConsole: dependencies.outputConsole,
      environmentInfo: dependencies.environmentInfo,
    });
    this.utils = new EleventyUtils();
  }
 
  getMetadata(): ProviderMetadata {
    return {
      type: 'eleventy',
      name: 'Eleventy',
      configFormats: ['js', 'json'],
      requiresBinary: true, // Requires npm package
      supportsDevServer: true,
      supportsBuild: true,
      supportsConfigQuery: false, // JS config files are hard to parse reliably
      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 EleventyProvider');
    }
 
    return new EleventyServer(
      {
        workspacePath: config.workspacePath,
        version: config.version,
        config: config.configFile,
        port: config.port || 13131, // Use same port as Hugo for consistency
      },
      this.dependencies.pathHelper,
      this.dependencies.appConfig,
      this.dependencies.windowAdapter,
      this.dependencies.outputConsole,
      container,
      config.siteKey,
      config.workspaceKey
    );
  }
 
  createBuilder(config: SSGBuildConfig): SSGBuilder {
    return new EleventyBuilder(
      {
        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 {
    // Eleventy uses JavaScript config files which are hard to parse safely
    // Return null to indicate config querying is not supported
    return null;
  }
 
  async createSite(options: SSGSiteCreationOptions): Promise<void> {
    await this.utils.createSiteDir(
      options.directory,
      options.title,
      options.configFormat as 'js' | 'json'
    );
  }
 
  async detectSite(directory: string): Promise<SSGDetectionResult> {
    // Check for Eleventy config files
    const configFiles = [
      '.eleventy.js',
      'eleventy.config.js',
      'eleventy.config.cjs',
      '.eleventy.cjs',
    ];
 
    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 package.json with Eleventy dependency
    const packageJsonPath = path.join(directory, 'package.json');
    if (await fs.pathExists(packageJsonPath)) {
      try {
        const packageJson = await fs.readJson(packageJsonPath);
        const hasDependency =
          (packageJson.dependencies && packageJson.dependencies['@11ty/eleventy']) ||
          (packageJson.devDependencies && packageJson.devDependencies['@11ty/eleventy']);
 
        if (hasDependency) {
          return {
            isDetected: true,
            confidence: 'medium',
            metadata: { source: 'package.json' },
          };
        }
      } catch {
        // Ignore JSON parse errors
      }
    }
 
    // Check for common Eleventy directories
    const eleventyMarkers = ['_site', '_includes', '_data'];
    let markerCount = 0;
 
    for (const marker of eleventyMarkers) {
      const markerPath = path.join(directory, marker);
      if (await fs.pathExists(markerPath)) {
        markerCount++;
      }
    }
 
    if (markerCount >= 2) {
      return {
        isDetected: true,
        confidence: 'low',
        metadata: { markers: markerCount },
      };
    }
 
    return {
      isDetected: false,
      confidence: 'low',
    };
  }
}