All files / backend/src/config app-state.ts

44% Statements 11/25
100% Branches 0/0
14.28% Functions 1/7
44% Lines 11/25

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                                                                                                                                            9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x                                                                                                                                
/**
 * Application Runtime State
 *
 * Replaces global.currentSiteKey, global.currentWorkspaceKey, etc.
 * Manages runtime state that is not persisted to disk.
 */
 
import { ChildProcess } from "child_process";
 
/**
 * AppState manages runtime application state (non-persistent)
 * This is state that changes during runtime but is not saved to config
 */
export class AppState {
  /**
   * Currently active site key
   */
  currentSiteKey: string | undefined;
 
  /**
   * Currently active workspace key
   */
  currentWorkspaceKey: string | undefined;
 
  /**
   * Current site path (mounted workspace path)
   */
  currentSitePath: string | undefined;
 
  /**
   * Current Hugo server process (if running)
   */
  currentServerProcess: ChildProcess | undefined;
 
  /**
   * Whether a Hugo server is currently running
   */
  isServerRunning: boolean;
 
  /**
   * Current Hugo server port
   */
  serverPort: number | undefined;
 
  /**
   * Current build directory
   */
  currentBuildDir: string | undefined;
 
  /**
   * Current base URL for the site
   */
  currentBaseUrl: string | undefined;
 
  /**
   * Current form node path (for form state management)
   */
  currentFormNodePath: string | undefined;
 
  /**
   * Current form accordion index (for form state management)
   */
  currentFormAccordionIndex: number | undefined;
 
  /**
   * Path of form that should be reloaded
   */
  currentFormShouldReload: string | undefined;
 
  constructor() {
    this.currentSiteKey = undefined;
    this.currentWorkspaceKey = undefined;
    this.currentSitePath = undefined;
    this.currentServerProcess = undefined;
    this.isServerRunning = false;
    this.serverPort = undefined;
    this.currentBuildDir = undefined;
    this.currentBaseUrl = undefined;
    this.currentFormNodePath = undefined;
    this.currentFormAccordionIndex = undefined;
    this.currentFormShouldReload = undefined;
  }
 
  /**
   * Set the current site and workspace
   */
  setCurrentSite(siteKey: string, workspaceKey: string, sitePath: string): void {
    this.currentSiteKey = siteKey;
    this.currentWorkspaceKey = workspaceKey;
    this.currentSitePath = sitePath;
  }
 
  /**
   * Clear the current site
   */
  clearCurrentSite(): void {
    this.currentSiteKey = undefined;
    this.currentWorkspaceKey = undefined;
    this.currentSitePath = undefined;
  }
 
  /**
   * Set the Hugo server process
   */
  setServerProcess(process: ChildProcess, port: number): void {
    this.currentServerProcess = process;
    this.isServerRunning = true;
    this.serverPort = port;
  }
 
  /**
   * Clear the Hugo server process
   */
  clearServerProcess(): void {
    this.currentServerProcess = undefined;
    this.isServerRunning = false;
    this.serverPort = undefined;
  }
 
  /**
   * Set the current build directory
   */
  setBuildDir(dir: string): void {
    this.currentBuildDir = dir;
  }
 
  /**
   * Get a snapshot of current state (for debugging)
   */
  getSnapshot(): Record<string, unknown> {
    return {
      currentSiteKey: this.currentSiteKey,
      currentWorkspaceKey: this.currentWorkspaceKey,
      currentSitePath: this.currentSitePath,
      isServerRunning: this.isServerRunning,
      serverPort: this.serverPort,
      currentBuildDir: this.currentBuildDir,
      currentBaseUrl: this.currentBaseUrl,
      currentFormNodePath: this.currentFormNodePath,
      currentFormAccordionIndex: this.currentFormAccordionIndex,
      currentFormShouldReload: this.currentFormShouldReload,
    };
  }
}