mirror of https://github.com/theNewDynamic/gohugo-theme-ananke.git

Patrick Kollitsch
yesterday 1832a681026ac3db6e62aa90cb5ed75886ea69ac
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
#!/usr/bin/env node
/**
 * Build the test fixture site against the *local* theme working tree.
 *
 * Hugo resolves a classic theme from `<themesDir>/<themeName>`. We create a
 * temporary themes directory containing a symlink `ananke -> <repo root>` so
 * the build exercises the current branch (including uncommitted changes)
 * without committing an absolute-path symlink. Output goes to
 * `tests/fixtures/site/public`, which the static server then serves.
 */
import { spawnSync } from "node:child_process";
import { mkdtempSync, rmSync, symlinkSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
 
const here = dirname(fileURLToPath(import.meta.url));
const repoRoot = resolve(here, "..", "..");
const siteDir = resolve(here, "..", "fixtures", "site");
const publicDir = join(siteDir, "public");
 
export function buildFixtureSite() {
  const themesDir = mkdtempSync(join(tmpdir(), "ananke-test-themes-"));
  try {
    symlinkSync(repoRoot, join(themesDir, "ananke"), "dir");
    rmSync(publicDir, { recursive: true, force: true });
    const result = spawnSync(
      "hugo",
      [
        "--source",
        siteDir,
        "--themesDir",
        themesDir,
        "--destination",
        publicDir,
        "--environment",
        "production",
        "--logLevel",
        "warn",
      ],
      { stdio: "inherit" },
    );
    if (result.status !== 0) {
      throw new Error(`hugo build failed with code ${result.status}`);
    }
  } finally {
    rmSync(themesDir, { recursive: true, force: true });
  }
  return publicDir;
}
 
// Allow running directly: `node tests/support/prepare-site.mjs`
if (import.meta.url === `file://${process.argv[1]}`) {
  buildFixtureSite();
}