All files / src/External ExternalManager.ts

36.84% Statements 7/19
0% Branches 0/2
42.85% Functions 3/7
41.17% Lines 7/17

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                                                  9x 9x       10x                         16x   16x                                     16x 16x          
import path from 'path';
import _ from 'lodash';
import { External } from './External.js';
import * as fsUtil from '../utils/fsUtil.js';
import * as urlUtil from '../utils/urlUtil.js';
import type { DynamicSrc } from '../Page/PageSources.js';
import type { VariableProcessor } from '../variables/VariableProcessor.js';
import type { SiteLinkManager } from '../html/SiteLinkManager.js';
import type { PluginManager } from '../plugins/PluginManager.js';
import { NodeProcessorConfig } from '../html/NodeProcessor.js';
 
export type ExternalManagerConfig = NodeProcessorConfig & {
  variableProcessor: VariableProcessor,
  siteLinkManager: SiteLinkManager,
  pluginManager: PluginManager,
};
 
/**
 * Manages and generates external files (<panel src="...">) referenced in pages and layouts.
 */
export class ExternalManager {
  config: ExternalManagerConfig;
  builtFiles: Record<string, Promise<External>>;
 
  constructor(cfg: ExternalManagerConfig) {
    this.config = cfg;
    this.builtFiles = {};
  }
 
  reset() {
    this.builtFiles = {};
  }
 
  /**
   * Generates the dependencies referenced by the dependencies provided, and adds any
   * collected sources to the includedFiles set.
   * @param dependencies
   * @param {Set<string>} includedFiles
   * @return {Promise<unknown[]>}
   */
  async generateDependencies(dependencies: DynamicSrc[],
                             includedFiles: Set<string>,
                             userScriptsAndStyles: string[]) {
    const resolvingExternals: Promise<External>[] = [];
 
    _.uniqBy(dependencies, d => d.asIfTo).forEach((src) => {
      Iif (urlUtil.isUrl(src.to)) {
        return;
      }
 
      const relativePath = path.relative(this.config.rootPath, src.asIfTo);
      const resultPath = path.join(this.config.outputPath, relativePath);
      const resultPathWithExternalExt = fsUtil.setExtension(resultPath, '._include_.html');
 
      Iif (!(resultPathWithExternalExt in this.builtFiles)) {
        const external = new External(this, src.to, userScriptsAndStyles);
        this.builtFiles[resultPathWithExternalExt] = external.resolveDependency(src.asIfTo,
                                                                                resultPathWithExternalExt,
                                                                                this.config);
      }
 
      resolvingExternals.push(this.builtFiles[resultPathWithExternalExt]);
    });
 
    const externals = await Promise.all(resolvingExternals);
    externals.forEach((external) => {
      external.includedFiles.forEach(filePath => includedFiles.add(filePath));
    });
  }
}