All files / src/cli utils.js

100% Statements 21/21
100% Branches 6/6
100% Functions 5/5
100% Lines 19/19
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    2x 2x 2x 2x 2x   2x 4x   4x 11x 5x 7x 7x 7x             7x       4x   7x 4x   4x                 2x  
'use strict';
 
const { Separator } = require('inquirer');
const Path = require('path');
const findIndex = require('lodash/findIndex');
const isObject = require('lodash/isObject');
const sortBy = require('lodash/sortBy');
 
const getChoicesFromServices = services => {
    let choices = [];
 
    const getChoices = (basePath, hash) => {
        if (isObject(hash)) {
            Object.keys(hash).forEach(key => {
                const path = Path.join(basePath, key);
                const isService = key === '.' || typeof hash[key] !== 'object';
                choices.push({
                    // prettier-ignore
                    name: `${isService ? '(service)  ' : '(folder)   '} ${path}`,
                    value: path,
                    isService
                });
 
                getChoices(path, hash[key]);
            });
        }
    };
    getChoices('', services);
 
    choices = sortBy(choices, choice => choice.isService);
    const firstFolderIndex = findIndex(choices, choice => choice.isService);
 
    return [
        new Separator(),
        ...choices.slice(0, firstFolderIndex),
        new Separator(),
        ...choices.slice(firstFolderIndex),
        new Separator()
    ];
};
 
module.exports = { getChoicesFromServices };