All files / src/pipeline/configuration create-versions-index.js

0% Statements 0/26
0% Branches 0/10
0% Functions 0/3
0% Lines 0/25

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                                                                                                                                                                                               
/**
 * @file create-versions-index.js
 * @description This script reads the configuration from specs.json, checks for the existence of a versions directory, creates it if it doesn't exist, and generates an index.html file listing all version directories in the directory.
 * 
 * @requires fs-extra - File system operations with extra methods.
 * @requires path - Utilities for working with file and directory paths.
 * 
 * @example
 * // To run this script, use the following command:
 * // node create-versions-index.js
 * 
 * @version 1.0.0
 */
 
const fs = require('fs-extra');
const path = require('node:path');
const Logger = require('../../utils/logger.js');
 
function createVersionsIndex(outputPath) {
    // Directory containing the version files
    const versionsDir = path.join(outputPath, 'versions');
 
    // Check if the directory that holds the versions / snapshots exists, if not create it
    if (!fs.existsSync(versionsDir)) {
        fs.mkdirSync(versionsDir, { recursive: true });
        Logger.info('Directory created:', versionsDir);
    }
    
    // Get all directories in the destination directory
    const dirs = fs.readdirSync(versionsDir).filter(file => fs.statSync(path.join(versionsDir, file)).isDirectory());
 
    // Load persisted labels written by freeze-spec-data.js.
    // Falls back to an empty object when no labels file exists yet.
    const labelsFile = path.join(versionsDir, 'labels.json');
    const labels = fs.existsSync(labelsFile) ? fs.readJsonSync(labelsFile) : {};
 
    // Generate HTML content
    let htmlContent = `
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Spec-Up-T Snapshot Index</title>
  <link href="https://fonts.googleapis.com/css2?family=Heebo:wght@300;400&display=swap" rel="stylesheet">
  <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
  <style>
    body { font-family: 'Heebo', Arial, sans-serif; background: #A9DDE0}
    ul { list-style-type: none; padding: 0; }
    li { margin: 10px 0; }
    a { text-decoration: none; color: #0071eb; }
    a:hover { text-decoration: underline; }
  </style>
</head>
<body class="container mt-5">
  <h1 class="mb-4">Spec-Up-T Snapshot Index</h1>
  <p>This page lists all available snapshots of this Spec-Up-T specification.</p>
  <ul class="list-group">
    <li class="list-group-item"><a href="../">Latest version</a></li>
`;
 
    if (dirs.length === 0) {
        htmlContent += `    <li class="list-group-item">No versions available</li>\n`;
    } else {
        dirs.forEach(dir => {
            // Use the stored label when available; fall back to the directory name
            const linkText = labels[dir] || `Version ${dir}`;
            htmlContent += `    <li class="list-group-item"><a href="${dir}/">${linkText}</a></li>\n`;
        });
    }
 
    htmlContent += `
  </ul>
</body>
</html>
`;
 
    // Write the HTML content to the index file synchronously.
    // Using writeFileSync ensures the file exists on disk before the render pipeline
    // continues and before GitHub Actions deploys docs/ to gh-pages.
    const indexPath = path.join(versionsDir, 'index.html');
    try {
        fs.writeFileSync(indexPath, htmlContent);
        Logger.success(`Index file created at ${indexPath}`);
    } catch (err) {
        Logger.error(`Error writing index file: ${err}`);
    }
}
 
// Export the function
module.exports = createVersionsIndex;
 
// If the script is run directly, execute the function
if (require.main === module) {
    createVersionsIndex();
}