<commit_before>var settings = require('./settings.js'),
    compile = require('node.class').compile,
    compilePage = require('./compilePage.js'),
    node2browser = require('node2browser'),
    Finder = require('fshelpers').Finder,
    makeDirsSync = require('fshelpers').makeDirsSync,
    removeSync = require('fshelpers').removeSync,
    paths = require('./paths.js'),
    services = require('./services.js'),
    validators = require('./validators.js'),
    modifyPathsInHTML = require('../tools/modifyPathsInHTML.js'),
    toSrc = require('toSrc'),
    async = require('async'),
    fs = require('fs');

var pathModifier = paths.getPathModifier(
        ['noServerFiles'],           // filters
        ['nodeModules'] // modifier
    ),
    patternLeft = '<script type="text/javascript">',
    patternRight = '</script>',
    availableServices = {},
    availableValidators = {};

function createSettings() {
    var newSettings = fs.readFileSync(paths.appConfig, 'utf8');

    try {
        newSettings = JSON.parse(newSettings);
    } catch (err) {
        throw new Error('Error while parsing the config.json\n' + err.message);
    }
    process.env.ENV_NODE = newSettings.env;
    settings.env = newSettings.env;
    settings.basePath = newSettings.basePath;
    settings.isDev = newSettings.env === 'development';
    settings.isLive = newSettings.env === 'production';
    settings.isClient = false;
    settings.isServer = true;
    settings.port = newSettings.port;
}

function compileApp() {
    var compileOptions = {
            includes: {
                alamid: paths.appAlamid
            }
        };

    compile(paths.appSrcPath, paths.appCompiledPath, compileOptions);
}

function copyAlamidFiles() {
    var fileFinder = new Finder();

    fileFinder
        .on('end', function onFileFinderEnd(path, files) {
            var file,
                filePath;

            for (filePath in files) {
                if (files.hasOwnProperty(filePath)) {
                    file = files[filePath];
                    filePath = filePath
                        .replace(paths.alamidSharedLib, paths.appAlamid);
                    fs.writeFileSync(filePath, file, 'utf8');
                }
            }
        })
        .walkSync(paths.alamidSharedLib, Finder.RECURSIVE, 'utf8');
}

function compilePages(callback) {
    var finder = new Finder();

    function doCompile(path, files) {
        async.forEach(files, function(item, callback) {
            var path = item.substring(
                paths.appHTML.length,
                item.length - '.html'.length
            );

            compilePage(path, callback);
        }, callback);
    }

    finder.on('end', doCompile);
    finder.walk(paths.appHTML + '/pages');
}

function prepareAvailableServices() {
    var path,
        client = services.services.client,
        server = services.services.server,
        both = services.services.both;

    availableServices.client = {};
    for (path in client) { if (client.hasOwnProperty(path)) {
        path = pathModifier(path);
        availableServices.client[path] = true;
    }}
    availableServices.server = {};
    for (path in server) { if (server.hasOwnProperty(path)) {
        path = pathModifier(path);
        availableServices.server[path] = true;
    }}
    availableServices.both = {};
    for (path in both) { if (both.hasOwnProperty(path)) {
        path = pathModifier(path);
        availableServices.both[path] = true;
    }}
}

function prepareAvailableValidators() {
    var path,
        client = validators.validators.client,
        server = validators.validators.server,
        both = validators.validators.both;

    availableValidators.client = {};
    for (path in client) { if (client.hasOwnProperty(path)) {
        path = pathModifier(path);
        availableValidators.client[path] = true;
    }}
    availableValidators.server = {};
    for (path in server) { if (server.hasOwnProperty(path)) {
        path = pathModifier(path);
        availableValidators.server[path] = true;
    }}
    availableValidators.both = {};
    for (path in both) { if (both.hasOwnProperty(path)) {
        path = pathModifier(path);
        availableValidators.both[path] = true;
    }}
}


function writeClientInit(callback) {
    var moduleFinder = new Finder(),
        scriptsFinder = new Finder(),
        modules = [],
        scripts = {},
        src = '';

    function writeInitHTML() {
        var initHTML = fs.readFileSync(paths.appHTML + '/init.html', 'utf8');

        initHTML = modifyPathsInHTML(initHTML, settings.basePath);
        writeScripts();<commit_msg>fixed path modifier bug, where no services where available<commit_after>var settings = require('./settings.js'),
    compile = require('node.class').compile,
    compilePage = require('./compilePage.js'),
    node2browser = require('node2browser'),
    Finder = require('fshelpers').Finder,
    makeDirsSync = require('fshelpers').makeDirsSync,
    removeSync = require('fshelpers').removeSync,
    paths = require('./paths.js'),
    services = require('./services.js'),
    validators = require('./validators.js'),
    modifyPathsInHTML = require('../tools/modifyPathsInHTML.js'),
    toSrc = require('toSrc'),
    async = require('async'),
    fs = require('fs');

var patternLeft = '<script type="text/javascript">',
    patternRight = '</script>',
    availableServices = {},
    availableValidators = {};

function createSettings() {
    var newSettings = fs.readFileSync(paths.appConfig, 'utf8');

    try {
        newSettings = JSON.parse(newSettings);
    } catch (err) {
        throw new Error('Error while parsing the config.json\n' + err.message);
    }
    process.env.ENV_NODE = newSettings.env;
    settings.env = newSettings.env;
    settings.basePath = newSettings.basePath;
    settings.isDev = newSettings.env === 'development';
    settings.isLive = newSettings.env === 'production';
    settings.isClient = false;
    settings.isServer = true;
    settings.port = newSettings.port;
}

function compileApp() {
    var compileOptions = {
            includes: {
                alamid: paths.appAlamid
            }
        };

    compile(paths.appSrcPath, paths.appCompiledPath, compileOptions);
}

function copyAlamidFiles() {
    var fileFinder = new Finder();

    fileFinder
        .on('end', function onFileFinderEnd(path, files) {
            var file,
                filePath;

            for (filePath in files) {
                if (files.hasOwnProperty(filePath)) {
                    file = files[filePath];
                    filePath = filePath
                        .replace(paths.alamidSharedLib, paths.appAlamid);
                    fs.writeFileSync(filePath, file, 'utf8');
                }
            }
        })
        .walkSync(paths.alamidSharedLib, Finder.RECURSIVE, 'utf8');
}

function compilePages(callback) {
    var finder = new Finder();

    function doCompile(path, files) {
        async.forEach(files, function(item, callback) {
            var path = item.substring(
                paths.appHTML.length,
                item.length - '.html'.length
            );

            compilePage(path, callback);
        }, callback);
    }

    finder.on('end', doCompile);
    finder.walk(paths.appHTML + '/pages');
}

function prepareAvailableServices() {
    var path,
        pathModifier = paths.getPathModifier(
            null,           // filters
            ['nodeModules'] // modifier
        ),
        client = services.services.client,
        server = services.services.server,
        both = services.services.both;

    availableServices.client = {};
    for (path in client) { if (client.hasOwnProperty(path)) {
        path = pathModifier(path);
        availableServices.client[path] = true;
    }}
    availableServices.server = {};
    for (path in server) { if (server.hasOwnProperty(path)) {
        path = pathModifier(path);
        availableServices.server[path] = true;
    }}
    availableServices.both = {};
    for (path in both) { if (both.hasOwnProperty(path)) {
        path = pathModifier(path);
        availableServices.both[path] = true;
    }}
}

function prepareAvailableValidators() {
    var path,
        client = validators.validators.client,
        server = validators.validators.server,
        both = validators.validators.both;

    availableValidators.client = {};
    for (path in client) { if (client.hasOwnProperty(path)) {
        path = pathModifier(path);
        availableValidators.client[path] = true;
    }}
    availableValidators.server = {};
    for (path in server) { if (server.hasOwnProperty(path)) {
        path = pathModifier(path);
        availableValidators.server[path] = true;
    }}
    availableValidators.both = {};
    for (path in both) { if (both.hasOwnProperty(path)) {
        path = pathModifier(path);
        availableValidators.both[path] = true;
    }}
}


function writeClientInit(callback) {
    var moduleFinder = new Finder(),
        scriptsFinder = new Finder(),
        pathModifier = paths.getPathModifier(
            ['noServerFiles'],           // filters
            ['nodeModules'] // modifier
        ),
        modules = [],
        scripts = {},
        src = '';

    function writeInitHTML() {
        var initHTML = fs.readFileSync(paths.appHTML + '/init.html', 'utf8');

        initHTML = modifyPathsInHTML(initHTML, settings.basePath);
        writeScripts();
{'fraction_non_alphanumeric': 0.12879027997886952, 'fraction_numerical': 0.0012678288431061807, 'mean_word_length': 2.4321972443799855, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '2435268', 'n_tokens_mistral': 2643, 'n_tokens_neox': 2625, 'n_words': 525}