All files / piscosour/plugins/inquirer index.js

0% Statements 0/44
0% Branches 0/42
0% Functions 0/11
0% Lines 0/43
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 83 84 85 86 87 88 89 90 91 92 93 94                                                                                                                                                                                           
'use strict';
 
const inquirer = require('inquirer');
 
module.exports = {
  check: function() {
    if (this.params.prompts && !this.params.disablePrompts) {
      return this.inquire('prompts');
    }
  },
 
  addons: {
 
    inquire: function(name) {
      const prompts = this.params[name];
 
      if (!Array.isArray(prompts)) {
        throw Error(`Inquire is not possible. The ${name} is not an array.`);
      }
 
      const getValidate = function(prompt) {
        return function(userInput) {
          return userInput ? true : '"' + prompt.name + '" is required. ' + prompt.message;
        };
      };
 
      const stepResolution = (prompt, attr) => {
        if (prompt[attr] !== undefined
          && Object.prototype.toString.call(prompt[attr]) !== '[object Function]'
          && typeof prompt[attr] !== 'boolean'
          && prompt[attr].indexOf('#') === 0) {
          let functionName = prompt[attr].replace('#', '');
          if (functionName.indexOf('(') >= 0) {
            functionName = functionName.replace('()', '');
          }
          const func = this[functionName];
          if (func) {
            prompt[attr] = attr === 'type' ? func() : func;
          } else {
            prompt[attr] = undefined;
            this.logger.info('#yellow', 'WARNING', 'value', functionName, 'doesn\'t exist in this step!!!');
          }
        }
      };
 
      const reqs = [];
 
      if (prompts) {
        prompts.forEach((prompt) => {
 
          ['when', 'validate', 'filter', 'choices', 'filter', 'type', 'default', 'message'].forEach((method) => stepResolution(prompt, method));
 
          if (prompt.required && !prompt.validate) {
            prompt.validate = getValidate(prompt);
          }
 
          if (prompt.env !== undefined && process.env[prompt.env]) {
            this.params[prompt.name] = process.env[prompt.env];
          }
 
          if (prompt.value !== undefined && !this.params[prompt.name]) {
            this.params[prompt.name] = prompt.value;
          }
 
          if (this.params[prompt.name] === undefined) {
            reqs.push(prompt);
          }
        });
      }
      if (reqs.length > 0) {
        return inquirer.prompt(reqs).then((answers) => {
          reqs.forEach((req) => {
            if (answers[req.name]) {
              this.params[req.name] = answers[req.name];
            }
          });
        });
      } else {
        return Promise.resolve();
      }
    },
 
    promptArgs: function(array) {
      if (this.params.prompts) {
        this.params.prompts.forEach((prompt) => {
          array.push('--' + prompt.name);
          array.push(this.params[prompt.name]);
        });
      }
      return array;
    }
  }
};