All files / src/External External.ts

5.55% Statements 1/18
0% Branches 0/2
0% Functions 0/2
5.55% Lines 1/18

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                2x                                                                                                                        
import fs from 'fs-extra';
import path from 'path';
import jsBeautify from 'js-beautify';
import { PageSources } from '../Page/PageSources.js';
import { NodeProcessor } from '../html/NodeProcessor.js';
import * as fsUtil from '../utils/fsUtil.js';
import type { ExternalManager, ExternalManagerConfig } from './ExternalManager.js';
 
const htmlBeautify = jsBeautify.html;
 
/**
 * Represents external files (e.g. files referenced by <panel src="...">)
 * not directly included inside the generated page
 */
export class External {
  externalManager: ExternalManager;
  sourceFilePath: string;
  includedFiles: Set<string>;
  userScriptsAndStyles: string[];
 
  constructor(em: ExternalManager, srcFilePath: string, userScriptsAndStyles: string[]) {
    this.externalManager = em;
    this.sourceFilePath = srcFilePath;
    this.includedFiles = new Set([srcFilePath]);
    this.userScriptsAndStyles = userScriptsAndStyles;
  }
 
  /**
   * Generates the content of this External instance
   * @param asIfAtFilePath
   * @param resultPath
   * @param config
   * @return {Promise<External>}
   */
  async resolveDependency(asIfAtFilePath: string, resultPath: string, config: ExternalManagerConfig):
  Promise<External> {
    const fileConfig = {
      ...config,
      headerIdMap: {},
    };
 
    const { variableProcessor, pluginManager, siteLinkManager } = config;
 
    const pageSources = new PageSources();
    const docId = `ext-${fsUtil.removeExtension(path.basename(asIfAtFilePath))}`;
    const nodeProcessor = new NodeProcessor(fileConfig, pageSources, variableProcessor,
                                            pluginManager, siteLinkManager, this.userScriptsAndStyles, docId);
 
    const nunjucksProcessed = variableProcessor.renderWithSiteVariables(this.sourceFilePath, pageSources);
    const mdHtmlProcessed = await nodeProcessor.process(this.sourceFilePath, nunjucksProcessed,
                                                        asIfAtFilePath) as string;
    const pluginPostRendered = pluginManager.postRender(nodeProcessor.frontmatter, mdHtmlProcessed);
 
    const outputContentHTML = process.env.TEST_MODE
      ? htmlBeautify(pluginPostRendered, pluginManager.htmlBeautifyOptions)
      : pluginPostRendered;
 
    await fs.outputFile(resultPath, outputContentHTML);
 
    pageSources.addAllToSet(this.includedFiles);
 
    await this.externalManager.generateDependencies(pageSources.getDynamicIncludeSrc(),
                                                    this.includedFiles,
                                                    this.userScriptsAndStyles);
 
    return this;
  }
}