/**
 * Imports
 */

var fs = require('fs')

/**
 * Vars
 */

function motor () {
  return {
    path: '/sys/class/tacho-motor/',
    available: getPaths('/sys/class/tacho-motor/'),
    prefix: 'out'
  }
}

function sensor () {
  return {
    path: '/sys/class/lego-sensor/',
    available: getPaths('/sys/class/lego-sensor/'),
    prefix: 'in'
  }
}

/**
 * Expose devices
 */

module.exports = devices

/**
 * Get a device
 * @param  {String} port
 * @return {String} path
 */

function devices (port) {
  var device = isNaN(port) ? motor() : sensor()
  if (device.prefix === motor().prefix) {
    port = port.toUpperCase()
  }
  try {
    return device.path + device.available[device.prefix + port]
  } catch (e) {
    throw new Error('No device')
  }
}

function getPaths (path) {
  return fs.readdirSync(path).reduce(function (obj, file) {
    var portName = fs.readFileSync(path + file + '/address', 'utf-8').trim()
    obj[portName] = file.trim()
    return obj
  }, {})
}

console.log(devices(2))

@@ -628,11 +628,51 @@
 %7D%0A  
-try
+if (device.available%5Bdevice.prefix + port%5D)
  %7B%0A 
@@ -742,17 +742,12 @@
   %7D 
-catch (e)
+else
  %7B%0A 
@@ -770,17 +770,17 @@
 or('
-N
+n
 o device
 ')%0A 
@@ -775,17 +775,33 @@
 o device
-'
+ in port ' + port
 )%0A  %7D%0A%7D%0A
@@ -1027,29 +1027,4 @@
 )%0A%7D%0A
-%0Aconsole.log(devices(2))%0A

throw new error