{"size": 2538, "ext": "js", "max_stars_repo_path": "lib/getData.js", "max_stars_repo_name": "signalkuppe/pequeno", "max_stars_repo_stars_event_min_datetime": "2021-09-27T05:34:20.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-15T08:32:55.000Z", "max_issues_repo_path": "lib/getData.js", "max_issues_repo_name": "signalkuppe/pequeno", "max_issues_repo_issues_event_min_datetime": "2021-10-03T16:11:15.000Z", "max_issues_repo_issues_event_max_datetime": "2021-12-23T17:17:51.000Z", "max_forks_repo_path": "lib/getData.js", "max_forks_repo_name": "signalkuppe/pequeno", "max_forks_repo_forks_event_min_datetime": "2021-12-07T10:40:50.000Z", "max_forks_repo_forks_event_max_datetime": "2021-12-07T10:40:50.000Z"}
{"max_stars_count": 10, "max_issues_count": 5, "max_forks_count": 1, "avg_line_length": 37.8805970149, "max_line_length": 80, "alphanum_fraction": 0.4814814815}
const path = require('path');
const ora = require('ora');
const fg = require('fast-glob');
const { mapPromisesSeries } = require('./utils');
const { joinPath } = require('./utils');

const getData = async function () {
    let data = {};
    let dataFiles;
    const lookFor = joinPath(pequeno.baseDir, pequeno.config.dataDir, `/*.js`);
    dataFiles = await fg([lookFor]);
    if (!dataFiles.length) {
        pequeno.log.verbose('No data files found');
    } else {
        if (pequeno.options.data) {
            pequeno.log.verbose(`Using only ${pequeno.options.data} data file`);
            dataFiles = dataFiles.filter(
                (df) => path.basename(df, '.js') === pequeno.options.data,
            );
        }
        return await mapPromisesSeries(dataFiles, async (dataFile) => {
            const dataFilePromise = require(dataFile);
            const dataFileName = path.parse(dataFile).name;

            if (typeof dataFilePromise !== 'function') {
                pequeno.log.warn(
                    'Data file %s doesn`t export an async function',
                    dataFileName,
                );
            } else {
                const cachedCollection = pequeno.cache.get(dataFileName);
                if (!cachedCollection) {
                    const time = process.hrtime();
                    const spinner = ora();
                    let spinnerText = `Fetching async data for ${dataFileName}`;
                    spinner.start(spinnerText);
                    const result = await dataFilePromise();
                    pequeno.cache.set(dataFileName, result, 1000000);
                    data[dataFileName] = result;
                    const ended = process.hrtime(time);
                    const secondsElapsed = (
                        ended[0] +
                        ended[1] / Math.pow(10, 9)
                    ).toFixed(1);
                    spinner.succeed(`${spinnerText} (${secondsElapsed}s)`);
                    spinner.stop();
                } else {
                    pequeno.log.verbose(
                        'Collection %s fetched from CACHE',
                        dataFileName,
                    );
                    data[dataFileName] = cachedCollection;
                }
            }
        })
            .then(() => {
                return data;
            })
            .catch((err) => {
                pequeno.log.error('Data fetch error', err);
                throw new Error(err);
            });
    }
};

module.exports = getData;
