All files / src/Site index.ts

85.36% Statements 35/41
57.14% Branches 4/7
76.19% Functions 16/21
85% Lines 34/40

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                                                  2x   6x 6x 6x 6x           14x 14x 14x 14x   14x 14x 14x 14x         14x                       3x 3x 3x                   6x       1x       1x       2x       1x       1x     1x 1x 1x   1x       1x       1x                       2x 2x                 1x 1x 1x      
import { SiteAssetsManager } from './SiteAssetsManager.js';
import { SiteDeployManager } from './SiteDeployManager.js';
import { SiteGenerationManager } from './SiteGenerationManager.js';
import { SitePagesManager } from './SitePagesManager.js';
import { SiteConfig } from './SiteConfig.js';
import * as logger from '../utils/logger.js';
import {
  SITE_CONFIG_NAME,
} from './constants.js';
 
import '../patches/htmlparser2.js';
 
export class Site {
  isDevMode: boolean;
  rootPath: string;
  outputPath: string;
  siteConfigPath: string;
 
  // Facade Internal Components
  assetsManager: SiteAssetsManager;
  pagesManager: SitePagesManager;
  deployManager: SiteDeployManager;
  generationManager: SiteGenerationManager;
 
  // Getters for compatibility
  get siteConfig() { return this.generationManager.siteConfig; }
  set siteConfig(config: SiteConfig) {
    this.generationManager.siteConfig = config;
    this.assetsManager.siteConfig = config;
    this.pagesManager.siteConfig = config;
    this.deployManager.siteConfig = config;
  }
 
  constructor(rootPath: string, outputPath: string, onePagePath: string, forceReload = false,
              siteConfigPath = SITE_CONFIG_NAME, isDevMode: any, backgroundBuildMode: boolean,
              postBackgroundBuildFunc: () => void) {
    this.isDevMode = !!isDevMode;
    this.rootPath = rootPath;
    this.outputPath = outputPath;
    this.siteConfigPath = siteConfigPath;
 
    this.assetsManager = new SiteAssetsManager(rootPath, outputPath);
    this.pagesManager = new SitePagesManager(rootPath, outputPath, this.isDevMode);
    this.deployManager = new SiteDeployManager(rootPath, outputPath);
    this.generationManager = new SiteGenerationManager(rootPath, outputPath, onePagePath, forceReload,
                                                       siteConfigPath, this.isDevMode, backgroundBuildMode,
                                                       postBackgroundBuildFunc);
 
    // Configure Generator with references
    this.generationManager.configure(this.assetsManager, this.pagesManager);
  }
 
  changeCurrentPage(normalizedUrl: string) {
    return this.generationManager.changeCurrentPage(normalizedUrl);
  }
 
  changeCurrentOpenedPages(normalizedUrls: string[]) {
    return this.generationManager.changeCurrentOpenedPages(normalizedUrls);
  }
 
  async readSiteConfig(baseUrl?: string) {
    const config = await this.generationManager.readSiteConfig(baseUrl);
    this.siteConfig = config;
    return config;
  }
 
  static printBaseUrlMessage() {
    logger.info('The default base URL of your site is set to /\n'
      + 'You can change the base URL of your site by editing site.json\n'
      + 'Check https://markbind.org/userGuide/siteConfiguration.html for more information.');
  }
 
  async generate(baseUrl: string | undefined): Promise<any> {
    return this.generationManager.generate(baseUrl);
  }
 
  async buildSourceFiles() {
    return this.generationManager.buildSourceFiles();
  }
 
  rebuildAffectedSourceFiles(filePaths: string | string[]) {
    return this.generationManager.rebuildAffectedSourceFiles(filePaths);
  }
 
  rebuildSourceFiles() {
    return this.generationManager.rebuildSourceFiles();
  }
 
  async indexSiteWithPagefind(): Promise<boolean> {
    return this.generationManager.indexSiteWithPagefind();
  }
 
  async updatePagefindIndex(filePaths: string | string[]): Promise<boolean> {
    Iif (!this.siteConfig.enableSearch) {
      return false;
    }
    const paths = Array.isArray(filePaths) ? filePaths : [filePaths];
    const pages = this.generationManager.sitePages.pages.filter(page =>
      paths.some(p => page.pageConfig.sourcePath === p || page.isDependency(p)),
    );
    return this.generationManager.updatePagefindIndex(pages);
  }
 
  buildAsset(filePaths: string | string[]) {
    return this.assetsManager.buildAsset(filePaths);
  }
 
  removeAsset(filePaths: string | string[]) {
    return this.assetsManager.removeAsset(filePaths);
  }
 
  isFilepathAPage(filePath: string) {
    return this.pagesManager.isFilepathAPage(filePath);
  }
 
  isDependencyOfPage(filePath: string) {
    return this.pagesManager.isDependencyOfPage(filePath);
  }
 
  async reloadSiteConfig() {
    await this.generationManager.reloadSiteConfig();
    this.siteConfig = this.generationManager.siteConfig;
  }
 
  /**
   * Deploys the site to the specified deployment platform.
   * @param ciTokenVar The CI token variable to use for authentication.
   * @returns A promise that resolves when the deployment is complete.
   */
  async deploy(ciTokenVar: string | boolean) {
    const config = await this.readSiteConfig();
    this.deployManager.siteConfig = config;
    return this.deployManager.deploy(ciTokenVar);
  }
}