All files inquire.js

0% Statements 0/25
0% Branches 0/11
0% Functions 0/12
0% Lines 0/25

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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102                                                                                                                                                                                                           
const inquirer = require('inquirer');
const Interchange = require('../lib/interchange');
// let Inquire = require('../lib/inquire');
const firmwares = require('../lib/firmwares.json').firmwares;
 
const Inquire = function(cb) {
  // main entry point.
 
  this.interchange = new Interchange();
 
  this.callback = cb;
 
  this.interchange.get_ports()
    .then(ports => this.promptQuestions(ports))
    .catch(err => console.error(err));
};
 
Inquire.prototype.promptQuestions = function(ports) {
  // lead the user through some questions to help choose and then
  // flash the firmware to the board.
 
  const questions = [
    {
      type: 'list',
      name: 'firmware',
      message: 'Choose a firmware',
      choices: this.interchange.firmwares.map((el) => {
        return el.name
      })
    },
    {
      type: 'confirm',
      name: 'firmata',
      message: 'Install firmata version?',
      default: (answers) => {
        return answers.firmware.indexOf('Firmata') > -1;
      },
      when: (answers) => {
        const firmware = firmwares.filter((obj) => {
          return obj.name === answers.firmware
        });
 
        return firmware.length && firmware[0].firmata && answers.firmware.indexOf('Firmata') === -1;
      }
    },
    {
      type: 'input',
      name: 'firmataType',
      message: 'Firmata name [optional]',
      default: null,
      when: (answers) => {
        return answers.firmata;
      }
    },
    {
      type: 'list',
      name: 'avr',
      message: 'Choose a board',
      choices: [
        'uno',
        'nano',
        'pro-mini'
      ],
      default: 'nano'
    },
    {
      type: 'list',
      name: 'port',
      message: 'Choose a port',
      choices: ports.map((el) => {
        return el.comName;
      }),
      default: null
    },
    {
      type: 'input',
      name: 'address',
      message: 'Choose an I2C address [optional]',
      default: null,
      when: (answers) => {
        return !answers.firmata && answers.firmware.indexOf('Firmata') === -1;
      }
    }
  ];
 
  inquirer.prompt(questions).then((answers) => {
    const firmware = answers.firmware;
    const opts = {
      board : answers.avr,
      port : answers.port,
      address : answers.address,
      firmata : answers.firmataType || answers.firmata
    };
 
    if (this.callback && typeof this.callback === 'function') {
      this.callback(firmware, opts);
    }
  });
};
 
module.exports = Inquire;