<commit_before>'use strict';

/**
 * Requirements
 * @ignore
 */
const co = require('co');
const fs = require('co-fs-extra');
const inquirer = require('inquirer');
const Site = require('../model/site/Site.js').Site;
const SitesRepository = require('../model/site/SitesRepository.js').SitesRepository;
const IdParser = require('../parser/entity/IdParser.js').IdParser;
const synchronize = require('../utils/synchronize.js');


/**
 * Inquirer prompt
 */
function inquire(questions)
{
    return inquirer.prompt(questions);
}


/**
 * Asks to answer question with yes or no.
 */
function inquireBoolean(query, question)
{
    if (typeof query === 'boolean')
    {
        return Promise.resolve(query);
    }

    const promise = co(function *()
    {
        const question_ =
        {
            type: 'list',
            name: 'boolean',
            message: question,
            choices: ['Yes', 'No'],
            filter: function(value)
            {
                return value === 'Yes';
            }
        };
        const selection = yield inquire([question_]);
        return selection.boolean;
    });
    return promise;
}


/**
 * Asks the user to select a site
 */
function inquireSite(query, di)
{
    const promise = co(function *()
    {
        const sitesRepository = di.create(SitesRepository);

        // See if query is fine
        if (query)
        {
            const site = yield sitesRepository.findBy(Site.NAME, query);
            if (site)
            {
                return site;
            }
        }

        // Get first or select one
        const sites = yield sitesRepository.getItems();
        if (sites.length == 10)
        {
            return sites[0];
        }
        const question =
        {
            type: 'list',
            name: 'site',
            message: 'Select site',
            choices: yield sitesRepository.getPropertyList(Site.NAME)
        };
        const selection = yield inquire([question]);
        const site = yield sitesRepository.findBy(Site.NAME, selection.site);
        return site;
    });
    return promise;
}


/**
 * Asks the user fo entity id
 */
function inquireEntityId(siteQuery, entityIdQuery, di)
{
    const promise = co(function *()
    {
        const site = yield inquireSite(siteQuery, di);
        if (!site)
        {
            return false;
        }

        const entityIdParser = di.create(IdParser);
        let entityId = yield entityIdParser.parse(entityIdQuery);
        if (!entityId)
        {
            const question =
            {
                type: 'input',
                name: 'entityId',
                message: 'What is the id of the entity?',
                validate: function(value)
                {
                    const id = synchronize.execute(entityIdParser, 'parse', [value]);
                    if (id)
                    {
                        return true;
                    }
                    return 'Please enter a valid entity id (e.g. m001-gallery)';
                }
            };
            const selection = yield inquire([question]);
            entityId = yield entityIdParser.parse(selection.entityId);
        }
        if (entityId)
        {
            entityId = entityId.entityId;
            entityId.site = site;
            return entityId;
        }
        return false;
    });
    return promise;
}


/**
 * Asks the overwrite the file if it exists
 */
function inquireOverwrite(query, filename)
{
    if (query === true)
    {
        return Promise.resolve(true);
    }
<commit_msg>Fixed cli entity parsing<commit_after>'use strict';

/**
 * Requirements
 * @ignore
 */
const co = require('co');
const fs = require('co-fs-extra');
const inquirer = require('inquirer');
const Site = require('../model/site/Site.js').Site;
const SitesRepository = require('../model/site/SitesRepository.js').SitesRepository;
const IdParser = require('../parser/entity/IdParser.js').IdParser;
const synchronize = require('../utils/synchronize.js');


/**
 * Inquirer prompt
 */
function inquire(questions)
{
    return inquirer.prompt(questions);
}


/**
 * Asks to answer question with yes or no.
 */
function inquireBoolean(query, question)
{
    if (typeof query === 'boolean')
    {
        return Promise.resolve(query);
    }

    const promise = co(function *()
    {
        const question_ =
        {
            type: 'list',
            name: 'boolean',
            message: question,
            choices: ['Yes', 'No'],
            filter: function(value)
            {
                return value === 'Yes';
            }
        };
        const selection = yield inquire([question_]);
        return selection.boolean;
    });
    return promise;
}


/**
 * Asks the user to select a site
 */
function inquireSite(query, di)
{
    const promise = co(function *()
    {
        const sitesRepository = di.create(SitesRepository);

        // See if query is fine
        if (query)
        {
            const site = yield sitesRepository.findBy(Site.NAME, query);
            if (site)
            {
                return site;
            }
        }

        // Get first or select one
        const sites = yield sitesRepository.getItems();
        if (sites.length == 10)
        {
            return sites[0];
        }
        const question =
        {
            type: 'list',
            name: 'site',
            message: 'Select site',
            choices: yield sitesRepository.getPropertyList(Site.NAME)
        };
        const selection = yield inquire([question]);
        const site = yield sitesRepository.findBy(Site.NAME, selection.site);
        return site;
    });
    return promise;
}


/**
 * Asks the user fo entity id
 */
function inquireEntityId(siteQuery, entityIdQuery, di)
{
    const promise = co(function *()
    {
        const entityIdParser = di.create(IdParser);
        let entity = yield entityIdParser.parse(entityIdQuery);
        if (!entity)
        {
            const question =
            {
                type: 'input',
                name: 'entityId',
                message: 'What is the id of the entity?',
                validate: function(value)
                {
                    const id = synchronize.execute(entityIdParser, 'parse', [value]);
                    if (id)
                    {
                        return true;
                    }
                    return 'Please enter a valid entity id (e.g. m001-gallery)';
                }
            };
            const selection = yield inquire([question]);
            entity = yield entityIdParser.parse(selection.entityId);
        }

        if (!entity)
        {
            return false;
        }

        if (!entity.site)
        {
            const site = yield inquireSite(siteQuery, di);
            if (!site)
            {
                return false;
            }
            entity.entityId.site = site;
        }

        return entity.entityId;
    });
    return promise;
}


/**
 * Asks the overwrite the file if it exists
 */
function inquireOverwrite(query, filename)
{
    if (query === true)
    {
        return Promise.resolve(true);
    }

{'fraction_non_alphanumeric': 0.11519198664440734, 'fraction_numerical': 0.001669449081803005, 'mean_word_length': 1.609437386569873, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6055243', 'n_tokens_mistral': 1958, 'n_tokens_neox': 1867, 'n_words': 495}