<commit_before>'use strict';

/**
 * Requirements
 * @ignore
 */
const Command = require('./Command.js').Command;
const Server = require('../server/Server.js').Server;
const Context = require('../application/Context.js').Context;
const Communication = require('../application/Communication.js').Communication;
const CliLogger = require('../cli/CliLogger.js').CliLogger;
const ModelSynchronizer = require('../watch/ModelSynchronizer.js').ModelSynchronizer;
const ErrorHandler = require('../error/ErrorHandler.js').ErrorHandler;
const co = require('co');


/**
 * @memberOf command
 */
class ServerCommand extends Command
{
    /**
     * @param {application.Context} context
     * @param {Object} options
     */
    constructor(context, options)
    {
        super(context);

        this._name = 'server';
        this._options = options || {};
        this._server = false;
    }


    /**
     * @inheritDoc
     */
    static get injections()
    {
        return { 'parameters': [Context, 'command/ServerCommand.options'] };
    }


    /**
     * @inheritDocs
     */
    static get className()
    {
        return 'command/ServerCommand';
    }


    /**
     * @type {server.Server}
     */
    get server()
    {
        return this._server;
    }


    /**
     * @type {Object}
     */
    get options()
    {
        return this._options;
    }


    /**
     * @inheritDocs
     */
    get help()
    {
        const help =
        {
            name: this._name,
            description: 'Provides the development server',
            actions:
            [
                {
                    name: 'start',
                    options: [],
                    description: 'Start the server'
                }
            ]
        };
        return help;
    }


    /**
     * @returns {Promise<server.Server>}
     */
    start(parameters)
    {
        const scope = this;
        const promise = co(function*()
        {
            if (!scope.server)
            {
                const logger = scope.createLogger('command.server');

                // Start ipc communication
                logger.info('Starting IPC server');
                const com = scope.context.di.create(Communication);
                com.events.on('find-server', () =>
                {
                    if (scope.server && scope.server.baseUrl)
                    {
                        com.send('found-server', scope.server.baseUrl);
                    }
                });
                com.serve();

                // prepare routes
                const configure = logger.section('Configuring routes');
                const routes = [];
                if (Array.isArray(scope.options.routes))
                {
                    for (const route of scope.options.routes)
                    {
                        const work = logger.work('Configuring route <' + route.type.className + '>');
                        const type = route.type;
                        const mappings = new Map();
                        for (const key in route)
                        {
                            if (key !== 'type')
                            {
                                mappings.set(type.className + '.' + key, route[key]);
                            }
                        }<commit_msg>feat(command.ServerCommand): listen to ipc linter broadcasts and update entitites<commit_after>'use strict';

/**
 * Requirements
 * @ignore
 */
const Command = require('./Command.js').Command;
const Server = require('../server/Server.js').Server;
const Context = require('../application/Context.js').Context;
const Communication = require('../application/Communication.js').Communication;
const CliLogger = require('../cli/CliLogger.js').CliLogger;
const ModelSynchronizer = require('../watch/ModelSynchronizer.js').ModelSynchronizer;
const EntitiesRepository = require('../model/entity/EntitiesRepository.js').EntitiesRepository;
const ErrorHandler = require('../error/ErrorHandler.js').ErrorHandler;
const co = require('co');


/**
 * @memberOf command
 */
class ServerCommand extends Command
{
    /**
     * @param {application.Context} context
     * @param {Object} options
     */
    constructor(context, options)
    {
        super(context);

        this._name = 'server';
        this._options = options || {};
        this._server = false;
    }


    /**
     * @inheritDoc
     */
    static get injections()
    {
        return { 'parameters': [Context, 'command/ServerCommand.options'] };
    }


    /**
     * @inheritDocs
     */
    static get className()
    {
        return 'command/ServerCommand';
    }


    /**
     * @type {server.Server}
     */
    get server()
    {
        return this._server;
    }


    /**
     * @type {Object}
     */
    get options()
    {
        return this._options;
    }


    /**
     * @inheritDocs
     */
    get help()
    {
        const help =
        {
            name: this._name,
            description: 'Provides the development server',
            actions:
            [
                {
                    name: 'start',
                    options: [],
                    description: 'Start the server'
                }
            ]
        };
        return help;
    }


    /**
     *
     */
    importLinterResults(lintResults)
    {
        if (!lintResults)
        {
            return;
        }
        const scope = this;
        co(function *()
        {
            const entityRepository = scope.context.di.create(EntitiesRepository);
            for (const lintResult of lintResults)
            {
                const entityAspect = yield entityRepository.getById(lintResult.entity);
                if (entityAspect)
                {
                    lintResult.site = entityAspect.id.site;
                    entityAspect.entity.lintResults.import(lintResult);
                }
            }
        });
    }


    /**
     * @returns {Promise<server.Server>}
     */
    start(parameters)
    {
        const scope = this;
        const promise = co(function*()
        {
            if (!scope.server)
            {
                const logger = scope.createLogger('command.server');

                // Start ipc communication
                logger.info('Starting IPC server');
                const com = scope.context.di.create(Communication);
                com.events.on('find-server', () =>
                {
                    if (scope.server && scope.server.baseUrl)
                    {
                        com.send('found-server', scope.server.baseUrl);
                    }
                });
                com.events.on('lint-results', (data) =>
                {
                    scope.importLinterResults(data);
                });
                com.serve();

                // prepare routes
                const configure = logger.section('Configuring routes');
                const routes = [];
                if (Array.isArray(scope.options.routes))
                {
                    for (const route of scope.options.routes)
                    {
                        const work = logger.work('Configuring route <' + route.type.className + '>');
                        const type = route.type;
                        const mappings = new Map();
                        for (const key in route)
                        {
                            if (key !== 'type')
                            {
                                mappings.set(type.className + '.' + key, route[key]);
                            }
                        }
{'fraction_non_alphanumeric': 0.1220374492601807, 'fraction_numerical': 0.0, 'mean_word_length': 1.2930051035725008, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, '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': '5693952', 'n_tokens_mistral': 1973, 'n_tokens_neox': 1849, 'n_words': 369}