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

100% Statements 12/12
100% Branches 6/6
100% Functions 2/2
100% Lines 12/12

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                      8x                                         17x 17x             13x   13x 2x     13x 2x     13x   13x 1x     12x              
/**
 * Hugo Builder
 *
 * Handles building Hugo sites.
 */
 
import fs from 'fs-extra';
import { execFile } from 'child_process';
import { promisify } from 'util';
import type { PathHelper } from '../../utils/path-helper.js';
 
const execFileAsync = promisify(execFile);
 
/**
 * Hugo build configuration
 */
export interface HugoBuildConfig {
  workspacePath: string;
  hugover: string;
  destination: string;
  config?: string;
  baseUrl?: string;
}
 
/**
 * HugoBuilder - Builds Hugo sites
 */
export class HugoBuilder {
  private config: HugoBuildConfig;
  private pathHelper: PathHelper;
 
  constructor(config: HugoBuildConfig, pathHelper: PathHelper) {
    this.config = config;
    this.pathHelper = pathHelper;
  }
 
  /**
   * Build the Hugo site
   */
  async build(): Promise<void> {
    const hugoArgs = ['--destination', this.config.destination];
 
    if (this.config.config) {
      hugoArgs.push('--config', this.config.config);
    }
 
    if (this.config.baseUrl) {
      hugoArgs.push('--baseURL', this.config.baseUrl);
    }
 
    const exec = this.pathHelper.getSSGBinForVer('hugo', this.config.hugover);
 
    if (!fs.existsSync(exec)) {
      throw new Error(`Could not find hugo executable for version ${this.config.hugover}.`);
    }
 
    await execFileAsync(exec, hugoArgs, {
      cwd: this.config.workspacePath,
      windowsHide: true,
      timeout: 60000, // 1 minute
    });
  }
}