All files / src/pipeline/configuration prepare-spec-configuration.js

0% Statements 0/33
0% Branches 0/1
0% Functions 0/1
0% Lines 0/33

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                                                                                                                                                         
/**
 * Handles configuration and initialization tasks before rendering specs.
 * This module centralizes setup logic to reduce complexity in index.js.
 * It loads config, runs validators, prepares external specs, and initializes shared variables.
 */
 
const fs = require('fs-extra');
const path = require('node:path');
const findPkgDir = require('find-pkg-dir');
 
const { initialize } = require('../../init.js');
const Logger = require('../../utils/logger.js');
const { runJsonKeyValidatorSync } = require('../../json-key-validator.js');
const { createTermIndex } = require('./create-term-index.js');
const { insertTermIndex } = require('./insert-term-index.js');
const { normalizeTerminologyMarkdown } = require('../preprocessing/normalize-terminology-markdown.js');
 
/**
 * Initializes configuration and shared variables for spec processing.
 * @param {Object} options - Options passed to the main function.
 * @returns {Object} An object containing config, externalSpecsList, template, assets, and other shared variables.
 */
async function initializeConfig(options = {}) {
  try {
    await initialize();
 
    runJsonKeyValidatorSync();
    createTermIndex();
    insertTermIndex();
 
    const modulePath = findPkgDir(__dirname);
    let config = fs.readJsonSync('./.cache/specs-generated.json');
 
    const createExternalSpecsList = require('./create-external-specs-list.js');
    const externalSpecsList = createExternalSpecsList(config);
 
    // Copy any frozen snapshots from the tracked snapshots/ directory into
    // the output path before regenerating the versions index. This is what makes
    // locally-created or CI-created freezes survive a fresh checkout and redeploy.
    const syncSnapshots = require('./sync-snapshots.js');
    syncSnapshots(config.specs[0].output_path);
 
    const createVersionsIndex = require('./create-versions-index.js');
    createVersionsIndex(config.specs[0].output_path);
 
    normalizeTerminologyMarkdown(path.join(config.specs[0].spec_directory, config.specs[0].spec_terms_directory));
 
    let template = fs.readFileSync(path.join(modulePath, 'templates/template.html'), 'utf8');
    let assets = fs.readJsonSync(modulePath + '/config/asset-map.json');
 
    // Initialize shared variables
    let externalReferences;
    let references = [];
    let definitions = [];
    let toc;
    let specGroups = {};
    let noticeTitles = {};
 
    return {
      config,
      externalSpecsList,
      template,
      assets,
      externalReferences,
      references,
      definitions,
      specGroups,
      noticeTitles
    };
  } catch (error) {
    Logger.error(`Error during configuration initialization: ${error.message}`);
    throw error;
  }
}
 
module.exports = { initializeConfig };