All files / lib prompt.js

100% Statements 76/76
73.91% Branches 17/23
100% Functions 0/0
100% Lines 76/76

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 761x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 2x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x
const inquirer = require('inquirer');
const debug = require('debug')('tabtab:prompt');
const path = require('path');
 
/**
 * Asks user about SHELL and desired location.
 *
 * It is too difficult to check spawned SHELL, the user has to use chsh before
 * it is reflected in process.env.SHELL
 */
const prompt = () => {
  const ask = inquirer.createPromptModule();
 
  const questions = [
    {
      type: 'list',
      name: 'shell',
      message: 'Which Shell do you use ?',
      choices: ['bash', 'zsh', 'fish'],
      default: 'bash'
    }
  ];
 
  const locations = {
    bash: '~/.bashrc',
    zsh: '~/.zshrc',
    fish: '~/.config/fish/config.fish'
  };
 
  const finalAnswers = {};
 
  return ask(questions)
    .then(answers => {
      const { shell } = answers;
      debug('answers', shell);
 
      const location = locations[shell];
      debug(`Will install completion to ${location}`);
 
      Object.assign(finalAnswers, { location, shell });
      return location;
    })
    .then(location =>
      ask({
        type: 'confirm',
        name: 'locationOK',
        message: `We will install completion to ${location}, is it ok ?`
      })
    )
    .then(answers => {
      const { locationOK } = answers;
      if (locationOK) {
        debug('location is ok, return', finalAnswers);
        return finalAnswers;
      }
 
      // otherwise, ask for specific **absolute** path
      return ask({
        name: 'userLocation',
        message: 'Which path then ? Must be absolute.',
        validate: input => {
          debug('Validating input', input);
          return path.isAbsolute(input);
        }
      }).then(lastAnswer => {
        const { userLocation } = lastAnswer;
        console.log(`Very well, we will install using ${userLocation}`);
        Object.assign(finalAnswers, { location: userLocation });
 
        return finalAnswers;
      });
    });
};
 
module.exports = prompt;