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

87.5% Statements 7/8
50% Branches 1/2
100% Functions 1/1
87.5% Lines 7/8

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                                                                                    3x     3x     3x   3x         3x           3x   3x                
/**
 * Hugo Utils
 *
 * Utilities for creating Hugo sites with configuration files.
 */
 
import fs from 'fs-extra';
import path from 'path';
import formatProviderResolver from '../../utils/format-provider-resolver.js';
 
/**
 * Hugo configuration format (matches Hugo's supported formats)
 */
export type HugoConfigFormat = 'toml' | 'yaml' | 'json';
 
/**
 * Basic Hugo configuration structure
 */
export interface HugoConfig {
  baseURL: string;
  title: string;
  [key: string]: unknown; // Allow additional Hugo config properties
}
 
/**
 * HugoUtils provides utilities for creating and managing Hugo sites
 */
export class HugoUtils {
  /**
   * Create a new Hugo site directory with a configuration file
   *
   * @param directory - The directory path where the Hugo site will be created
   * @param title - The title for the Hugo site
   * @param configFormat - The configuration file format (toml, yaml, or json)
   * @returns Promise that resolves to true on success
   */
  async createSiteDir(
    directory: string,
    title: string,
    configFormat: HugoConfigFormat
  ): Promise<boolean> {
    // Ensure the directory exists
    await fs.ensureDir(directory);
 
    // Construct the config file path
    const hugoConfigFilePath = path.join(directory, `config.${configFormat}`);
 
    // Resolve the format provider for this file type
    const formatProvider = formatProviderResolver.resolveForFilePath(hugoConfigFilePath);
 
    Iif (!formatProvider) {
      throw new Error(`Unsupported config format: ${configFormat}`);
    }
 
    // Create the basic Hugo configuration
    const hconfig: HugoConfig = {
      baseURL: 'http://example.org',
      title: title,
    };
 
    // Write the configuration file
    await fs.writeFile(hugoConfigFilePath, formatProvider.dump(hconfig));
 
    return true;
  }
}
 
/**
 * Default singleton instance
 */
export default new HugoUtils();