Merge pull request #121 from hybridgroup/pure-js-cli

Migrate cylon-cli to pure JS.
This commit is contained in:
Andrew Stewart 2014-02-27 12:56:53 -08:00
commit 82effb6b64
11 changed files with 142 additions and 279 deletions

View File

@ -2,7 +2,7 @@
var Program = require('commander'), var Program = require('commander'),
Pkg = require('../package.json'), Pkg = require('../package.json'),
CliCommands = require('../dist/cli/cli-commands'), CliCommands = require('../lib/cli/cli-commands'),
fs = require('fs'); fs = require('fs');
Program Program
@ -12,7 +12,7 @@ Program
Program Program
.command("generate <name>") .command("generate <name>")
.description("Generates a new adaptor") .description("Generates a new adaptor")
.action(function(name) { require('../src/generators/adaptor')(name) }); .action(function(name) { require('../lib/generators/adaptor')(name) });
Program Program
.command("scan <type>") .command("scan <type>")
@ -36,7 +36,7 @@ Program
break; break;
case 'connect': case 'connect':
option = (option === null || option === undefined) ? '/dev/rfcomm0' : option; option = (option === null || option === undefined) ? '/dev/rfcomm0' : option;
CliCommands.connectToSerial(option, address); CliCommands.bluetooth.connect(option, address);
break; break;
case 'scan': case 'scan':
CliCommands.bluetooth.scan(); CliCommands.bluetooth.scan();

View File

@ -1,44 +1,61 @@
(function() { require("./process");
var bluetooth, os, scan;
require("./process"); var bluetooth,
scan = require("./scan"),
os = require('os');
scan = require("./scan"); bluetooth = {
pair: function(hciX, address) {
var process = new Cylon.Process,
platform = os.platform();
os = require('os'); switch (platform) {
case 'linux':
bluetooth = { process.spawn('bluez-simple-agent', [hciX, address]);
pair: function(hciX, address) { break;
var platform, process; case 'darwin':
process = new Cylon.Process; console.log("OS X manages Bluetooth pairing itself.");
platform = os.platform(); break;
switch (platform) { default:
case 'linux': console.log("OS not yet supported.");
return process.spawn('bluez-simple-agent', [hciX, address]);
case 'darwin':
return console.log("OS X manages Bluetooth pairing itself.");
default:
return console.log("OS not yet supported.");
}
},
unpair: function(hciX, address) {
var platform, process;
process = new Cylon.Process;
platform = os.platform();
switch (platform) {
case 'linux':
return process.spawn('bluez-simple-agent', [hciX, address, 'remove']);
case 'darwin':
return console.log("OS X manages Bluetooth unpairing itself.");
default:
return console.log("OS not yet supported.");
}
},
scan: function() {
return scan('bluetooth');
} }
}; },
module.exports = bluetooth; unpair: function(hciX, address) {
var process = new Cylon.Process,
platform = os.platform();
}).call(this); switch (platform) {
case 'linux':
process.spawn('bluez-simple-agent', [hciX, address, 'remove']);
break;
case 'darwin':
console.log("OS X manages Bluetooth unpairing itself.");
break;
default:
console.log("OS not yet supported.");
}
},
connect: function(dev, address) {
var process = new Cylon.Process,
platform = os.platform();
switch (platform) {
case 'linux':
process.spawn('sudo', ['rfcomm', 'connect', dev, address, '1']);
break;
case 'darwin':
console.log("OS X manages binding itself.");
break;
default:
console.log("OS not yet supported");
}
},
scan: function() {
scan('bluetooth');
}
};
module.exports = bluetooth;

View File

@ -1,22 +1,17 @@
(function() { var bluetooth, cliCommands, connectToSerial, scan;
var bluetooth, cliCommands, connectToSerial, scan;
scan = require('./scan'); var cliCommands,
scan = require('./scan'),
bluetooth = require('./bluetooth'),
connectToSerial = require('./connect-to-serial'); cliCommands = {
scan: scan,
bluetooth: {
pair: bluetooth.pair,
unpair: bluetooth.unpair,
scan: bluetooth.scan,
connect: bluetooth.connect
}
};
bluetooth = require('./bluetooth'); module.exports = cliCommands;
cliCommands = {
scan: scan,
connectToSerial: connectToSerial,
bluetooth: {
pair: bluetooth.pair,
unpair: bluetooth.unpair,
scan: bluetooth.scan
}
};
module.exports = cliCommands;
}).call(this);

View File

@ -1,24 +0,0 @@
(function() {
var connectToSerial, os;
require("./process");
os = require('os');
connectToSerial = function(dev, address) {
var platform, process;
process = new Cylon.Process;
platform = os.platform();
switch (platform) {
case 'linux':
return process.spawn('sudo', ['rfcomm', 'connect', dev, address, '1']);
case 'darwin':
return console.log("OS X manages binding itself.");
default:
return console.log("OS not yet supported");
}
};
module.exports = connectToSerial;
}).call(this);

View File

@ -1,53 +1,48 @@
(function() { var spawn = require('child_process').spawn,
var exec, namespace, spawn; exec = require('child_process').exec,
namespace = require('node-namespace');
spawn = require('child_process').spawn; namespace("Cylon", function() {
this.Process = (function() {
function Process() {}
exec = require('child_process').exec; Process.prototype.exec = function(command) {
exec(command, function(err, stdout, stderr) {
if (stdout != null) {
console.log(stdout);
}
if (stderr != null) {
console.log(stderr);
}
if (err != null) {
console.log(err);
}
});
};
namespace = require('node-namespace'); Process.prototype.spawn = function(command, args) {
var cmd;
namespace("Cylon", function() { cmd = spawn(command, args, {
return this.Process = (function() { stdio: 'inherit'
function Process() {} });
Process.prototype.exec = function(command) { cmd.on('close', function(code) {
exec(command, function(err, stdout, stderr) { if (code !== 0) {
if (stdout != null) { return console.log("ps process exited with code " + code);
console.log(stdout); }
} });
if (stderr != null) {
console.log(stderr);
}
if (err != null) {
return console.log(err);
}
});
return true;
};
Process.prototype.spawn = function(command, args) { cmd.on('exit', function(code) {
var cmd; if (code !== 0) {
cmd = spawn(command, args, { return console.log("ps process exited with code " + code);
stdio: 'inherit' }
}); });
cmd.on('close', function(code) { };
if (code !== 0) {
return console.log("ps process exited with code " + code);
}
});
return cmd.on('exit', function(code) {
if (code !== 0) {
return console.log("ps process exited with code " + code);
}
});
};
return Process; return Process;
})(); })();
}); });
module.exports = Cylon.Process; module.exports = Cylon.Process;
}).call(this);

View File

@ -1,34 +1,35 @@
(function() { require("./process");
var os, scan;
require("./process"); var scan,
os = require('os');
os = require('os'); scan = function(type) {
var process = new Cylon.Process,
platform = os.platform();
scan = function(type) { switch (platform) {
var platform, process; case 'linux':
process = new Cylon.Process; switch (type) {
platform = os.platform(); case 'serial':
switch (platform) { process.exec("dmesg | grep tty");
case 'linux': break;
switch (type) { case 'bluetooth':
case 'serial': process.exec("hcitool scan");
return process.exec("dmesg | grep tty"); break;
case 'bluetooth': case 'usb':
return process.exec("hcitool scan"); process.exec("lsusb");
case 'usb': break;
return process.exec("lsusb"); default:
default: console.log("Device type not yet supported.");
return console.log("Device type not yet supported."); break;
} }
break; break;
case 'darwin': case 'darwin':
return process.exec("ls /dev/{tty,cu}.*"); process.exec("ls /dev/{tty,cu}.*");
default: break;
return console.log("OS not yet supported."); default:
} console.log("OS not yet supported.");
}; }
};
module.exports = scan; module.exports = scan;
}).call(this);

View File

@ -1,37 +0,0 @@
require "./process"
scan = require "./scan"
os = require('os')
bluetooth =
pair: (hciX, address) ->
process = new Cylon.Process
platform = os.platform()
switch platform
when 'linux'
process.spawn 'bluez-simple-agent', [hciX, address]
when 'darwin'
console.log "OS X manages Bluetooth pairing itself."
else
console.log "OS not yet supported."
unpair: (hciX, address) ->
process = new Cylon.Process
platform = os.platform()
switch platform
when 'linux'
process.spawn 'bluez-simple-agent', [hciX, address, 'remove']
when 'darwin'
console.log "OS X manages Bluetooth unpairing itself."
else
console.log "OS not yet supported."
scan: () ->
scan('bluetooth')
module.exports = bluetooth

View File

@ -1,13 +0,0 @@
scan = require('./scan')
connectToSerial = require('./connect-to-serial')
bluetooth = require('./bluetooth')
cliCommands =
scan: scan
connectToSerial: connectToSerial
bluetooth:
pair: bluetooth.pair
unpair: bluetooth.unpair
scan: bluetooth.scan
module.exports = cliCommands

View File

@ -1,18 +0,0 @@
require "./process"
os = require 'os'
connectToSerial = (dev, address) ->
process = new Cylon.Process
platform = os.platform()
switch platform
when 'linux'
process.spawn 'sudo', ['rfcomm', 'connect', dev, address, '1']
when 'darwin'
console.log "OS X manages binding itself."
else
console.log "OS not yet supported"
module.exports = connectToSerial

View File

@ -1,24 +0,0 @@
spawn = require('child_process').spawn
exec = require('child_process').exec
namespace = require 'node-namespace'
namespace "Cylon", ->
class @Process
exec: (command) ->
exec command, (err, stdout, stderr) ->
console.log(stdout) if stdout?
console.log(stderr) if stderr?
console.log(err) if err?
true
spawn: (command, args) ->
cmd = spawn command, args, { stdio: 'inherit' }
cmd.on 'close', (code) ->
console.log "ps process exited with code #{code}" if code isnt 0
cmd.on 'exit', (code) ->
console.log "ps process exited with code #{code}" if code isnt 0
module.exports = Cylon.Process

View File

@ -1,29 +0,0 @@
require "./process"
os = require('os')
scan = (type) ->
process = new Cylon.Process
platform = os.platform()
switch platform
when 'linux'
switch type
when 'serial'
process.exec "dmesg | grep tty"
when 'bluetooth'
process.exec "hcitool scan"
when 'usb'
process.exec "lsusb"
else
console.log "Device type not yet supported."
when 'darwin'
process.exec "ls /dev/{tty,cu}.*"
else
console.log "OS not yet supported."
module.exports = scan