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

100% Statements 42/42
100% Branches 10/10
100% Functions 13/13
100% Lines 40/40

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                                                        19x 19x             9x 9x   9x 2x     7x 5x   7x 14x   5x 2x     3x 3x             6x 6x   6x 1x     5x 5x 4x 4x   1x               6x 4x                   5x             12x 12x   12x 12x   12x 18x     12x 2x     12x 11x 2x   9x       12x 1x          
/**
 * Hugo Config
 *
 * Queries Hugo configuration using the `hugo config` command.
 */
 
import { spawn } from 'child_process';
import fs from 'fs-extra';
import type { PathHelper } from '../../utils/path-helper.js';
import type { SSGConfigQuerier, SSGSiteConfig } from '../types.js';
 
/**
 * Site configuration for Hugo operations
 */
export interface QSiteConfig {
  workspacePath: string;
  hugover: string;
  config?: string;
}
 
/**
 * HugoConfig - Queries Hugo configuration
 */
export class HugoConfig implements SSGConfigQuerier {
  private qSiteConfig: QSiteConfig;
  private pathHelper: PathHelper;
 
  constructor(qSiteConfig: QSiteConfig, pathHelper: PathHelper) {
    this.qSiteConfig = qSiteConfig;
    this.pathHelper = pathHelper;
  }
 
  /**
   * Get Hugo config mounts as a parsed object
   */
  async configMountsAsObject(): Promise<unknown[]> {
    const { workspacePath, hugover } = this.qSiteConfig;
    const exec = this.pathHelper.getSSGBinForVer('hugo', hugover);
 
    if (!fs.existsSync(exec)) {
      return [];
    }
 
    const output = await this.spawnHugo(exec, ['config', 'mounts'], workspacePath);
    const lines = output.split('\n');
 
    const startIdx = lines.findIndex((element) => element.startsWith('{'));
    const endIdx = lines.findIndex((element) => element.startsWith('}'));
 
    if (startIdx === -1 || endIdx === -1) {
      return [];
    }
 
    const retstring = lines.slice(startIdx, endIdx + 1).join('');
    return JSON.parse(retstring);
  }
 
  /**
   * Get Hugo config as lines
   */
  async configLines(): Promise<string[]> {
    const { workspacePath, hugover } = this.qSiteConfig;
    const exec = this.pathHelper.getSSGBinForVer('hugo', hugover);
 
    if (!fs.existsSync(exec)) {
      return [];
    }
 
    try {
      const output = await this.spawnHugo(exec, ['config'], workspacePath);
      const lines = output.split('\n');
      return lines;
    } catch {
      return [];
    }
  }
 
  /**
   * SSGConfigQuerier interface implementation: Get config as object
   */
  async getConfig(): Promise<SSGSiteConfig> {
    const mounts = await this.configMountsAsObject();
    return {
      config: {},  // Hugo doesn't expose full config as JSON easily
      mounts: mounts,
    };
  }
 
  /**
   * SSGConfigQuerier interface implementation: Get config as lines
   */
  async getConfigLines(): Promise<string[]> {
    return this.configLines();
  }
 
  /**
   * Helper to spawn Hugo command and capture output
   */
  private spawnHugo(exec: string, args: string[], cwd: string): Promise<string> {
    return new Promise((resolve, reject) => {
      const proc = spawn(exec, args, { cwd });
 
      let stdout = '';
      let stderr = '';
 
      proc.stdout.on('data', (data) => {
        stdout += data.toString();
      });
 
      proc.stderr.on('data', (data) => {
        stderr += data.toString();
      });
 
      proc.on('close', (code) => {
        if (code !== 0) {
          reject(new Error(`Hugo command failed with code ${code}: ${stderr}`));
        } else {
          resolve(stdout);
        }
      });
 
      proc.on('error', (err) => {
        reject(err);
      });
    });
  }
}