<commit_before>"use strict";

const express = require("express");
const router = express.Router({mergeParams: true});
const responseCodes = require("../response_codes.js");
const C = require("../constants");
const systemLogger    = require("../logger.js").systemLogger;
const serialize = require("serialize-javascript");
const _ = require("lodash");
const fs = require("fs");
const pug = require("pug");
const addressMeta = require("../models/addressMeta");
const units = require("../models/unit");
const History = require("../models/history");
const ModelHelper = require("../models/helper/model");
const User = require("../models/user");
const DEFAULT_PLUGIN_STRUCTURE = require("../plugin/plugin-structure.js").DEFAULT_PLUGIN_STRUCTURE;

const config = require("../config");

/**
 * Get the pug files for the required state or plugin
 *
 * @param {string} required - Name of required plugin
 * @param {string} pathToStatesAndPlugins - Root path of plugins 
 * @param {Object} params - Updates with information from plugin structure 
 */
function getPugFiles(required, pathToStatesAndPlugins, params) {
    let requiredFiles,
        requiredDir,
        fileSplit;

    requiredDir = pathToStatesAndPlugins + "/" + required + "/pug";
    try {
        fs.accessSync(requiredDir, fs.F_OK); // Throw for fail

        requiredFiles = fs.readdirSync(requiredDir);
        requiredFiles.forEach(function (file) {
            fileSplit = file.split(".");
            params.frontendPug.push({
                id: fileSplit[0] + ".html",
                path: requiredDir + "/" + file
            });
        });
    } catch (e) {
        // Pug files don"t exist
        systemLogger.logFatal(e.message);
    }
}

/**
 * Setup loading only the required states and plugins pug files
 *
 * @private
 * @param {string[]} statesAndPlugins - List of states and plugins to load 
 * @param {string} required - Plugin to load
 * @param {string} pathToStatesAndPlugins - Base directory to load plugins from
 * @param {Object} params - Updates with information from plugin structure
 */
function setupRequiredPug(statesAndPlugins, required, pathToStatesAndPlugins, params) {
    let i, length;

    if (statesAndPlugins.indexOf(required.plugin) !== -1) {
        getPugFiles(required.plugin, pathToStatesAndPlugins, params);

        // Friends
        if (required.hasOwnProperty("friends")) {
            for (i = 0, length = required.friends.length; i < length; i++) {
                if (statesAndPlugins.indexOf(required.friends[i]) !== -1) {
                    getPugFiles(required.friends[i], pathToStatesAndPlugins, params);
                }
            }
        }

        // Functions
        if (required.hasOwnProperty("functions")) {
            for (i = 0, length = required.functions.length; i < length; i++) {
                if (statesAndPlugins.indexOf(required.functions[i]) !== -1) {
                    getPugFiles(required.functions[i], pathToStatesAndPlugins, params);
                }
            }
        }

        // Recurse for children
        if (required.hasOwnProperty("children")) {
            for (i = 0, length = required.children.length; i < length; i++) {
                setupRequiredPug(statesAndPlugins, required.children[i], pathToStatesAndPlugins, params);
            }
        }

    }
}

/**
 * Get all available states and plugins
 *
 * @param {Object} params - updates with information from plugin structure
 */
function setupPug(params, pluginStructure) {
    let pathToStatesAndPlugins = "./frontend/components",
        statesAndPlugins;

    // Get all available states and plugins in the file system
    statesAndPlugins = fs.readdirSync(pathToStatesAndPlugins);
    setupRequiredPug(statesAndPlugins, pluginStructure, pathToStatesAndPlugins, params);
}

function createClientConfig(serverConfig, req) {

    // TODO: Replace with user based plugin selection
    let pluginStructure = {};

    // If the serverConfig has a plugin structure, use that
    // else just use the default
    if (serverConfig.pluginStructure) {
        pluginStructure = require("../../" + serverConfig.pluginStructure);
    } else {
        pluginStructure = DEFAULT_PLUGIN_STRUCTURE;
    }

    // Set up the legal plugins
    if (config.hasOwnProperty("legal")) {
        for (let i = 0; i < config.legal.length; i += 1) {
            pluginStructure.functions.push(config.legal[i].page);
        }<commit_msg>ISSUE #522 - Attempt to fix pug dir<commit_after>"use strict";

const express = require("express");
const router = express.Router({mergeParams: true});
const responseCodes = require("../response_codes.js");
const C = require("../constants");
const systemLogger    = require("../logger.js").systemLogger;
const serialize = require("serialize-javascript");
const _ = require("lodash");
const fs = require("fs");
const pug = require("pug");
const addressMeta = require("../models/addressMeta");
const units = require("../models/unit");
const History = require("../models/history");
const ModelHelper = require("../models/helper/model");
const User = require("../models/user");
const DEFAULT_PLUGIN_STRUCTURE = require("../plugin/plugin-structure.js").DEFAULT_PLUGIN_STRUCTURE;

const config = require("../config");

/**
 * Get the pug files for the required state or plugin
 *
 * @param {string} required - Name of required plugin
 * @param {string} pathToStatesAndPlugins - Root path of plugins 
 * @param {Object} params - Updates with information from plugin structure 
 */
function getPugFiles(required, pathToStatesAndPlugins, params) {
    let requiredFiles,
        requiredDir,
        fileSplit;

    requiredDir = pathToStatesAndPlugins + "/" + required + "/pug";
    try {
        fs.accessSync(requiredDir, fs.F_OK); // Throw for fail

        requiredFiles = fs.readdirSync(requiredDir);
        requiredFiles.forEach(function (file) {
            fileSplit = file.split(".");
            params.frontendPug.push({
                id: fileSplit[0] + ".html",
                path: requiredDir + "/" + file
            });
        });
    } catch (e) {
        // Pug files don"t exist
        systemLogger.logFatal(e.message);
    }
}

/**
 * Setup loading only the required states and plugins pug files
 *
 * @private
 * @param {string[]} statesAndPlugins - List of states and plugins to load 
 * @param {string} required - Plugin to load
 * @param {string} pathToStatesAndPlugins - Base directory to load plugins from
 * @param {Object} params - Updates with information from plugin structure
 */
function setupRequiredPug(statesAndPlugins, required, pathToStatesAndPlugins, params) {
    let i, length;

    if (statesAndPlugins.indexOf(required.plugin) !== -1) {
        getPugFiles(required.plugin, pathToStatesAndPlugins, params);

        // Friends
        if (required.hasOwnProperty("friends")) {
            for (i = 0, length = required.friends.length; i < length; i++) {
                if (statesAndPlugins.indexOf(required.friends[i]) !== -1) {
                    getPugFiles(required.friends[i], pathToStatesAndPlugins, params);
                }
            }
        }

        // Functions
        if (required.hasOwnProperty("functions")) {
            for (i = 0, length = required.functions.length; i < length; i++) {
                if (statesAndPlugins.indexOf(required.functions[i]) !== -1) {
                    getPugFiles(required.functions[i], pathToStatesAndPlugins, params);
                }
            }
        }

        // Recurse for children
        if (required.hasOwnProperty("children")) {
            for (i = 0, length = required.children.length; i < length; i++) {
                setupRequiredPug(statesAndPlugins, required.children[i], pathToStatesAndPlugins, params);
            }
        }

    }
}

/**
 * Get all available states and plugins
 *
 * @param {Object} params - updates with information from plugin structure
 */
function setupPug(params, pluginStructure) {
    const pathToStatesAndPlugins = "./../../frontend/components";

    // Get all available states and plugins in the file system
    const statesAndPlugins = fs.readdirSync(pathToStatesAndPlugins);
    setupRequiredPug(statesAndPlugins, pluginStructure, pathToStatesAndPlugins, params);
}

function createClientConfig(serverConfig, req) {

    // TODO: Replace with user based plugin selection
    let pluginStructure = {};

    // If the serverConfig has a plugin structure, use that
    // else just use the default
    if (serverConfig.pluginStructure) {
        pluginStructure = require("../../" + serverConfig.pluginStructure);
    } else {
        pluginStructure = DEFAULT_PLUGIN_STRUCTURE;
    }

    // Set up the legal plugins
    if (config.hasOwnProperty("legal")) {
        for (let i = 0; i < config.legal.length; i += 1) {
            pluginStructure.functions.push(config.legal[i].page);
        }
{'fraction_non_alphanumeric': 0.12415578568212517, 'fraction_numerical': 0.0023638000900495274, 'mean_word_length': 2.8149420352082437, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1843861', 'n_tokens_mistral': 2456, 'n_tokens_neox': 2392, 'n_words': 644}