All files / backend/src/site-sources folder-site-source.ts

49.2% Statements 31/63
41.93% Branches 13/31
62.5% Functions 5/8
49.2% Lines 31/63

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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218                                2x                           11x 11x               11x 11x     11x                           16x 16x 16x         16x 16x 16x     16x                   16x                     16x 32x 16x                       11x 11x     11x           11x   11x     11x   27x         27x 11x     16x     16x 16x                 11x                       11x                                                                                                                            
/**
 * Folder Site Source
 *
 * Represents a site stored in local folders.
 * Each subdirectory in the site root can be a workspace (e.g., 'main', 'staging', 'develop').
 */
 
import fs from 'fs-extra';
import path from 'path';
import type { Workspace } from '@quiqr/types';
import type { PathHelper } from '../utils/path-helper.js';
import type { SiteSource, SourceConfig } from './site-source-factory.js';
 
/**
 * Directories to exclude when discovering workspaces
 */
const EXCLUDED_DIRS = [
  '.git',
  '.quiqr-cache',
  'node_modules',
  'publish',
  'temp',
  '.DS_Store'
];
 
export class FolderSiteSource implements SiteSource {
  private config: SourceConfig;
  private pathHelper: PathHelper;
 
  constructor(config: SourceConfig, pathHelper: PathHelper) {
    this.config = config;
    this.pathHelper = pathHelper;
    // console.log('[FolderSiteSource] Created for:', config.key, config.path);
  }
 
  /**
   * Get the absolute path to the site root directory
   */
  private getSiteRootPath(): string {
    const siteRoot = this.pathHelper.getSiteRoot(this.config.key || '');
    Iif (!siteRoot) {
      throw new Error(`Could not determine site root for key: ${this.config.key}`);
    }
    return siteRoot;
  }
 
  /**
   * Get the absolute path to a specific workspace
   */
  private getWorkspacePath(workspaceKey: string): string {
    return path.join(this.getSiteRootPath(), workspaceKey);
  }
 
  /**
   * Check if a directory is a valid workspace directory
   */
  private async isValidWorkspace(dirPath: string): Promise<boolean> {
    try {
      const stat = await fs.lstat(dirPath);
      Iif (!stat.isDirectory()) {
        return false;
      }
 
      // Check if it contains typical Hugo/Quarto files
      const hugoConfigExists = await this.hasHugoConfig(dirPath);
      const quiqrDirExists = await fs.pathExists(path.join(dirPath, 'quiqr'));
      const contentDirExists = await fs.pathExists(path.join(dirPath, 'content'));
 
      // A valid workspace should have at least one of these
      return hugoConfigExists || quiqrDirExists || contentDirExists;
    } catch {
      return false;
    }
  }
 
  /**
   * Check if directory has a Hugo configuration file
   */
  private async hasHugoConfig(dirPath: string): Promise<boolean> {
    const configFiles = [
      'config.toml',
      'config.yaml',
      'config.yml',
      'config.json',
      'hugo.toml',
      'hugo.yaml',
      'hugo.yml',
      'hugo.json'
    ];
 
    for (const configFile of configFiles) {
      if (await fs.pathExists(path.join(dirPath, configFile))) {
        return true;
      }
    }
    return false;
  }
 
  /**
   * List all workspaces in the site root directory
   */
  async listWorkspaces(): Promise<Workspace[]> {
    //console.log('[FolderSiteSource] Discovering workspaces for site:', this.config.key);
 
    try {
      const siteRoot = this.getSiteRootPath();
 
      // Check if site root exists
      Iif (!await fs.pathExists(siteRoot)) {
        console.warn('[FolderSiteSource] Site root does not exist:', siteRoot);
        return [];
      }
 
      // Read all entries in the site root
      const entries = await fs.readdir(siteRoot);
 
      const workspaces: Workspace[] = [];
 
      // Check each entry to see if it's a valid workspace
      for (const entry of entries) {
        // Skip excluded directories and files
        Iif (EXCLUDED_DIRS.includes(entry)) {
          continue;
        }
 
        // Skip files (config.json, etc.)
        if (entry.includes('.')) {
          continue;
        }
 
        const entryPath = path.join(siteRoot, entry);
 
        // Check if it's a valid workspace directory
        Eif (await this.isValidWorkspace(entryPath)) {
          workspaces.push({
            key: entry,
            path: entryPath,
            state: 'ready'
          });
        }
      }
 
      // If no workspaces found, check if the configured path itself is a workspace
      Iif (workspaces.length === 0 && this.config.path) {
        const configuredPath = this.getWorkspacePath(this.config.path);
        if (await this.isValidWorkspace(configuredPath)) {
          workspaces.push({
            key: this.config.path,
            path: configuredPath,
            state: 'ready'
          });
        }
      }
 
      //console.log('[FolderSiteSource] Found', workspaces.length, 'workspace(s)');
      return workspaces;
    } catch {
      //console.error('[FolderSiteSource] Error discovering workspaces:', error);
      return [];
    }
  }
 
  /**
   * Mount a workspace (preparation for editing)
   * For folder sources, this is typically a no-op as workspaces are already accessible
   */
  async mountWorkspace(workspaceKey: string): Promise<void> {
    console.log('[FolderSiteSource] Mounting workspace:', workspaceKey);
 
    try {
      const workspacePath = this.getWorkspacePath(workspaceKey);
 
      // Verify workspace exists
      if (!await fs.pathExists(workspacePath)) {
        throw new Error(`Workspace '${workspaceKey}' does not exist at path: ${workspacePath}`);
      }
 
      // Verify it's a valid workspace
      if (!await this.isValidWorkspace(workspacePath)) {
        throw new Error(`Directory '${workspaceKey}' is not a valid workspace`);
      }
 
      console.log('[FolderSiteSource] Workspace mounted successfully:', workspaceKey);
    } catch (error) {
      console.error('[FolderSiteSource] Error mounting workspace:', error);
      throw error;
    }
  }
 
  /**
   * Update workspace from source (e.g., git pull)
   * For simple folder sources, this checks if the folder is a git repository
   * and performs a git pull if so.
   */
  async update(): Promise<void> {
    console.log('[FolderSiteSource] Updating workspaces for site:', this.config.key);
 
    try {
      const siteRoot = this.getSiteRootPath();
 
      // Check if the site root is a git repository
      const gitDir = path.join(siteRoot, '.git');
      const isGitRepo = await fs.pathExists(gitDir);
 
      if (isGitRepo) {
        console.log('[FolderSiteSource] Site is a git repository - update should use git sync');
        // Note: Actual git operations should be handled by the SyncFactory/GitSync
        // This is just a marker that update is possible
      } else {
        console.log('[FolderSiteSource] Site is not a git repository - no update needed');
      }
    } catch (error) {
      console.error('[FolderSiteSource] Error during update:', error);
      throw error;
    }
  }
}