All files QuestionModule.ts

65.52% Statements 19/29
50% Branches 3/6
60% Functions 6/10
67.86% Lines 19/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 831x 1x 1x 1x       1x   1x     2x 4x   4x   4x     4x 4x 4x                   4x             4x   2x       1x         1x           1x                                                    
import fs from 'fs';
import path from 'path';
import inquirer, { Question, Answers } from 'inquirer';
import chalk from 'chalk';
import { ComponentData, PJSON } from './types';
import Logger from './Logger';
 
inquirer.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt'));
 
export default class QuestionModule {
    components: ComponentData[];
    constructor(paths: string[], logger: Logger) {
        this.components = paths.reduce((components: ComponentData[], componentPath: string) => {
            const folders: string[] = fs.readdirSync(componentPath);
 
            const componentsInFolder: ComponentData[] = folders
                .filter(folder =>
                    fs.existsSync((componentPath === '/' ? '/' : componentPath + '/') + folder + '/package.json')
                )
                .map(folder => {
                    const location = (componentPath === '/' ? '/' : componentPath + '/') + folder;
                    const data: PJSON = JSON.parse(fs.readFileSync(location + '/package.json', 'utf8'));
                    return {
                        path: location,
                        data,
                    };
                });
 
            if (!componentsInFolder.length) {
                logger.warn(`Not found components in ${path.resolve(componentPath)}`);
                return components;
            }
            logger.info(
                `Found ` +
                    chalk.bold(`${componentsInFolder.length} `) +
                    chalk.bold(`(${componentPath})`) +
                    ` components in`,
                path.resolve(componentPath)
            );
            return components.concat(componentsInFolder);
        }, []);
        logger.info('Total: ' + chalk.bold.white(`${this.components.length}`) + ' found components');
    }
 
    createQuestions(): Question[] {
        const choices = this.components.map(c => ({
            name: `${c.data.name}@${c.data.version} - ${chalk.bold.grey(c.data.description)}`,
            value: c,
        }));
 
        return [
            {
                name: 'component',
                type: 'autocomplete',
                message: 'Название компонента?',
                source: (answersSoFar: any, input: string) => {
                    return Promise.resolve(input ? choices.filter(c => c.name.includes(input)) : choices);
                },
            },
            {
                name: 'version',
                type: 'list',
                message: 'Новая версия?',
                choices: (answer: Answers) => {
                    const version = answer.component.data.version.split('.');
                    return version
                        .map((n: string, i: number) => {
                            const newVer = Array.from(version);
                            newVer.splice(i, 3 - i, ...[+n + 1, 0, 0].splice(0, 3 - i));
                            return newVer.join('.');
                        })
                        .sort();
                },
            },
        ];
    }
 
    ask(): Promise<Answers> {
        const questions: Question[] = this.createQuestions();
        return inquirer.prompt(questions);
    }
}