<commit_before>/*!
 * Teo.js framework
 * @author <NAME> <<EMAIL>>
 * @date {22.05.15}
 */
"use strict";

const
    _ = require("./teo.utils"),
    Base = require("./teo.base"),
    Cluster = require("./teo.cluster"),
    Core = require("./teo.core");

// ----
global.version = require("../package.json").version;
global.copyright = "Powered by Teo.js";
global.logger = require("./teo.logger");

class Teo extends Base {
	constructor(config, callback) {
		super(config, callback);

		this._parseOptions();

        _.async(this.createCore.bind(this))
            .catch(err => {
                logger.error(err.message, err.stack);
                throw new Error(err.message);
            })
            .then(function () {
                _.isGenerator(this.callback) ?
                    _.async(this.callback, this) :
                        this.callback.call(this, this);

                process.nextTick(() => {
                    this.emit("ready", this);
                    if (this.mode !== "test") {
                        logger.showLogo();
                    }
                });
            }.bind(this));
	}

	_parseOptions() {
		this.mode = this.config.mode || process.env.NODE_ENV || "development";
        this.homeDir = this.config.homeDir || process.cwd().replace(/\\/g, "/");// home dirname ( from where framework is started )
        this.appsDir = this.homeDir + "/apps";    // main apps dir
        this.confDir = this.homeDir + "/config";    // config dir
	}

    * createCore() {
        return _.promise((resolve, reject) => {
            this.core = new Core({
                mode: this.mode,
                homeDir: this.homeDir,
                appsDir: this.appsDir,
                confDir: this.confDir
            }, (err, res) => {
                if (err) {
                    reject(err);
                    return;
                }
                resolve(res);
            });
        });
    }

    /**
     * Start application
     * @param [appName] :: name of the application to start (or alternatively, start all)
     */
    * start(appName) {
        if (this.core.coreAppConfig.get("cluster").enabled) {
            yield _.promise(function(resolve, reject) {
                new Cluster(resolve);
            }.bind(this));
        }
        yield* this._runAppLifeCircleAction(appName, "start");
    }

    /**
     * Stop application
     * @param [appName] :: name of the application to stop (or alternatively, to stop all, if no name)
     */
    * stop(appName) {
        yield* this._runAppLifeCircleAction(appName, "stop");
    }

    /**
     * Restarts application
     * @param [appName] :: name of the application to stop (or alternatively, to stop all, if no name)
     * @returns {*}
     */
    * restart(appName) {
        yield* this._runAppLifeCircleAction(appName, "restart");
    }

    /**<commit_msg>minor<commit_after>/*!
 * Teo.js framework
 * @author <NAME> <<EMAIL>>
 * @date {22.05.15}
 */
"use strict";

const
    _ = require("./teo.utils"),
    Base = require("./teo.base"),
    Cluster = require("./teo.cluster"),
    Core = require("./teo.core");

// ----
global.version = require("../package.json").version;
global.copyright = "Powered by Teo.js";
global.logger = require("./teo.logger");

class Teo extends Base {
	constructor(config, callback) {
		super(config, callback);

		this._parseOptions();

        _.async(this.createCore.bind(this))
            .catch(err => {
                logger.error(err.message, err.stack);
                throw new Error(err.message);
            })
            .then(() => {
                _.isGenerator(this.callback) ?
                    _.async(this.callback, this) :
                        this.callback.call(this, this);

                process.nextTick(() => {
                    this.emit("ready", this);
                    if (this.mode !== "test") {
                        logger.showLogo();
                    }
                });
            });
	}

	_parseOptions() {
		this.mode = this.config.mode || process.env.NODE_ENV || "development";
        this.homeDir = this.config.homeDir || process.cwd().replace(/\\/g, "/");// home dirname ( from where framework is started )
        this.appsDir = this.homeDir + "/apps";    // main apps dir
        this.confDir = this.homeDir + "/config";    // config dir
	}

    * createCore() {
        return _.promise((resolve, reject) => {
            this.core = new Core({
                mode: this.mode,
                homeDir: this.homeDir,
                appsDir: this.appsDir,
                confDir: this.confDir
            }, (err, res) => {
                if (err) {
                    reject(err);
                    return;
                }
                resolve(res);
            });
        });
    }

    /**
     * Start application
     * @param [appName] :: name of the application to start (or alternatively, start all)
     */
    * start(appName) {
        if (this.core.coreAppConfig.get("cluster").enabled) {
            yield _.promise((resolve, reject) => {
                new Cluster(resolve);
            });
        }
        yield* this._runAppLifeCircleAction(appName, "start");
    }

    /**
     * Stop application
     * @param [appName] :: name of the application to stop (or alternatively, to stop all, if no name)
     */
    * stop(appName) {
        yield* this._runAppLifeCircleAction(appName, "stop");
    }

    /**
     * Restarts application
     * @param [appName] :: name of the application to stop (or alternatively, to stop all, if no name)
     * @returns {*}
     */
    * restart(appName) {
        yield* this._runAppLifeCircleAction(appName, "restart");
    }

    /**
{'fraction_non_alphanumeric': 0.15027083697361523, 'fraction_numerical': 0.0020968023763760267, 'mean_word_length': 1.6353591160220995, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 19, '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': '4175104', 'n_tokens_mistral': 1696, 'n_tokens_neox': 1606, 'n_words': 348}