All files install.js

35% Statements 7/20
16.67% Branches 2/12
100% Functions 1/1
35% Lines 7/20
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                                          1x           1x 1x                   1x 81x   81x 81x                                                                      
/*
  All of the operations in this module are synchronous
  because without them, the environment cannot support
  the features provided by tessel-av and it's best
  to get it done and out of the way before proceeding
  with the program's exection.
 
  Generally speaking, the detection and installation
  path will never be taken, as it serves to provide a
  fallback, not a primary execution path.
 
 
  Average execution times
 
  | Avg Exec Time    | Operation                                                    |
  | ---------------- | ------------------------------------------------------------ |
  | 50ms             | cp.spawnSync('which', [binary])                              |
  | 601ms            | cp.spawnSync('opkg', ['update'])                             |
  | 24s !!!!!!!!!!!! | cp.spawnSync('opkg', ['install', binary])                    |
 
 */
var cp = require('child_process');
 
// TODO: this can only work if the Tessel is connected to the network...
// var os = require('os');
// var wlan0 = os.os.networkInterfaces().wlan0[0];
 
var profile = '/etc/profile';
var installed = {
  aplay: true,
  arecord: true,
  espeak: false,
  fswebcam: true,
  ffmpeg: true,
  madplay: false,
  'mjpg-streamer': false,
};
 
module.exports = function(binary) {
  var env = `AV_INSTALLED_${binary.toUpperCase()}`;
 
  Eif (global.IS_TEST_ENV || process.arch !== 'mipsel') {
    return true;
  }
  // If this binary is known to be published in the
  // openwrt build, DO NOT PROCEED.
  if (installed[binary]) {
    return true;
  }
  // If this binary has already been installed via
  // tessel-av installation operations, DO NOT PROCEED.
  if (process.env[env]) {
    return true;
  }
 
  var which = cp.spawnSync('which', [binary]);
  var install;
 
  // According to `which`, this binary does not exist
  if (which.status === 1) {
    // opkg update will return with a status = 1,
    // but that's just because it might fail to wget a package list,
    // which is not fatal.
    cp.spawnSync('opkg', ['update']);
 
    console.log(`Installing ${binary}...`);
 
    install = cp.spawnSync('opkg', ['install', binary]);
 
    if (install.status === 0) {
      console.log('Completed.');
 
      cp.execSync(`echo 'export ${env}=1' >> ${profile}; source ${profile}`);
      installed[binary] = true;
    }
  }
};