All files / lib inquire.js

100% Statements 39/39
94.74% Branches 18/19
100% Functions 15/15
100% Lines 39/39

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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 1411x 1x 1x   1x     10x 1x     9x   9x   9x       9x 9x   8x 8x   1x       1x       2x 1x             1x   6x 1x     5x           15x               2x     2x 6x     2x                 2x                                     5x                   2x         5x       1x       2x 1x     1x   1x 1x             1x 1x         1x  
const inquirer = require('inquirer');
const Interchange = require('../lib/interchange');
const firmwares = require('../lib/firmwares.json').firmwares;
 
const Inquire = function(cb) {
  // main entry point.
 
  if (typeof(cb) === 'undefined') {
    throw new Error('No callback defined to return to');
  }
 
  this.callback = cb;
 
  this.interchange = new Interchange();
 
  this.ports = null;
 
  // return this as a promise so we don't call the prompt too rapidly before
  // we've enumerated all of the ports etc.
  return new Promise((resolve, reject) => {
    this.interchange.get_ports()
      .then(ports => {
        this.ports = ports
        resolve(this);
      })
      .catch(err => { reject(err) });
  });
};
 
Inquire.prototype.prompt = function() {
  // this method iniitiates the questions side of things in order to decouple
  // the process of construction from triggering
 
  if (this.ports === null) {
    throw new Error('No ports have been discovered');
  }
 
  /* istanbul ignore next */
  this.promptQuestions(this.ports);
};
 
Inquire.prototype.questions = function(ports) {
  // return the set of questions as an object (makes this easier to test);
  if (typeof(ports) == 'undefined') {
    throw new Error('No ports were provided to select from');
  }
 
  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.path;
      }),
      default: null
    },
    {
      type: 'input',
      name: 'address',
      message: 'Choose an I2C address [optional]',
      default: null,
      when: (answers) => {
        return !answers.firmata && answers.firmware.indexOf('Firmata') === -1;
      }
    }
  ];
 
  return questions;
};
 
 
Inquire.prototype.promptQuestions = function(ports) {
  // lead the user through some questions to help choose and then
  // flash the firmware to the board.
 
  if (typeof(ports) == 'undefined') {
    throw new Error('No ports were provided to select from');
  }
 
  inquirer.prompt(this.questions(ports))
    .then((answers) => {
      const firmware = answers.firmware;
      const opts = {
        board : answers.avr,
        port : answers.port,
        address : answers.address,
        firmata : answers.firmataType || answers.firmata
      };
 
      Eif (this.callback && typeof this.callback === 'function') {
        this.callback(firmware, opts);
      }
    });
};
 
module.exports = Inquire;