Merge pull request #121 from hybridgroup/pure-js-cli
Migrate cylon-cli to pure JS.
This commit is contained in:
commit
82effb6b64
|
@ -2,7 +2,7 @@
|
|||
|
||||
var Program = require('commander'),
|
||||
Pkg = require('../package.json'),
|
||||
CliCommands = require('../dist/cli/cli-commands'),
|
||||
CliCommands = require('../lib/cli/cli-commands'),
|
||||
fs = require('fs');
|
||||
|
||||
Program
|
||||
|
@ -12,7 +12,7 @@ Program
|
|||
Program
|
||||
.command("generate <name>")
|
||||
.description("Generates a new adaptor")
|
||||
.action(function(name) { require('../src/generators/adaptor')(name) });
|
||||
.action(function(name) { require('../lib/generators/adaptor')(name) });
|
||||
|
||||
Program
|
||||
.command("scan <type>")
|
||||
|
@ -36,7 +36,7 @@ Program
|
|||
break;
|
||||
case 'connect':
|
||||
option = (option === null || option === undefined) ? '/dev/rfcomm0' : option;
|
||||
CliCommands.connectToSerial(option, address);
|
||||
CliCommands.bluetooth.connect(option, address);
|
||||
break;
|
||||
case 'scan':
|
||||
CliCommands.bluetooth.scan();
|
||||
|
|
|
@ -1,44 +1,61 @@
|
|||
(function() {
|
||||
var bluetooth, os, scan;
|
||||
require("./process");
|
||||
|
||||
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');
|
||||
|
||||
bluetooth = {
|
||||
pair: 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]);
|
||||
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');
|
||||
switch (platform) {
|
||||
case 'linux':
|
||||
process.spawn('bluez-simple-agent', [hciX, address]);
|
||||
break;
|
||||
case 'darwin':
|
||||
console.log("OS X manages Bluetooth pairing itself.");
|
||||
break;
|
||||
default:
|
||||
console.log("OS not yet supported.");
|
||||
}
|
||||
};
|
||||
},
|
||||
|
||||
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;
|
||||
|
|
|
@ -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');
|
||||
|
||||
cliCommands = {
|
||||
scan: scan,
|
||||
connectToSerial: connectToSerial,
|
||||
bluetooth: {
|
||||
pair: bluetooth.pair,
|
||||
unpair: bluetooth.unpair,
|
||||
scan: bluetooth.scan
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = cliCommands;
|
||||
|
||||
}).call(this);
|
||||
module.exports = cliCommands;
|
||||
|
|
|
@ -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);
|
|
@ -1,53 +1,48 @@
|
|||
(function() {
|
||||
var exec, namespace, spawn;
|
||||
var spawn = require('child_process').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() {
|
||||
return this.Process = (function() {
|
||||
function Process() {}
|
||||
cmd = spawn(command, args, {
|
||||
stdio: 'inherit'
|
||||
});
|
||||
|
||||
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) {
|
||||
return console.log(err);
|
||||
}
|
||||
});
|
||||
return true;
|
||||
};
|
||||
cmd.on('close', function(code) {
|
||||
if (code !== 0) {
|
||||
return console.log("ps process exited with code " + code);
|
||||
}
|
||||
});
|
||||
|
||||
Process.prototype.spawn = function(command, args) {
|
||||
var cmd;
|
||||
cmd = spawn(command, args, {
|
||||
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);
|
||||
}
|
||||
});
|
||||
};
|
||||
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;
|
||||
|
||||
}).call(this);
|
||||
module.exports = Cylon.Process;
|
||||
|
|
|
@ -1,34 +1,35 @@
|
|||
(function() {
|
||||
var os, scan;
|
||||
require("./process");
|
||||
|
||||
require("./process");
|
||||
var scan,
|
||||
os = require('os');
|
||||
|
||||
os = require('os');
|
||||
scan = function(type) {
|
||||
var process = new Cylon.Process,
|
||||
platform = os.platform();
|
||||
|
||||
scan = function(type) {
|
||||
var platform, process;
|
||||
process = new Cylon.Process;
|
||||
platform = os.platform();
|
||||
switch (platform) {
|
||||
case 'linux':
|
||||
switch (type) {
|
||||
case 'serial':
|
||||
return process.exec("dmesg | grep tty");
|
||||
case 'bluetooth':
|
||||
return process.exec("hcitool scan");
|
||||
case 'usb':
|
||||
return process.exec("lsusb");
|
||||
default:
|
||||
return console.log("Device type not yet supported.");
|
||||
}
|
||||
break;
|
||||
case 'darwin':
|
||||
return process.exec("ls /dev/{tty,cu}.*");
|
||||
default:
|
||||
return console.log("OS not yet supported.");
|
||||
}
|
||||
};
|
||||
switch (platform) {
|
||||
case 'linux':
|
||||
switch (type) {
|
||||
case 'serial':
|
||||
process.exec("dmesg | grep tty");
|
||||
break;
|
||||
case 'bluetooth':
|
||||
process.exec("hcitool scan");
|
||||
break;
|
||||
case 'usb':
|
||||
process.exec("lsusb");
|
||||
break;
|
||||
default:
|
||||
console.log("Device type not yet supported.");
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 'darwin':
|
||||
process.exec("ls /dev/{tty,cu}.*");
|
||||
break;
|
||||
default:
|
||||
console.log("OS not yet supported.");
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = scan;
|
||||
|
||||
}).call(this);
|
||||
module.exports = scan;
|
||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
Loading…
Reference in New Issue