Skip to content

Commit

Permalink
Merge pull request #27 from mintuz/ALLOW_Custom_Commands
Browse files Browse the repository at this point in the history
Code for executing custom commands
  • Loading branch information
mintuz committed Aug 28, 2016
2 parents f8c37e7 + c71c639 commit b29a80f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
28 changes: 28 additions & 0 deletions README.md
Expand Up @@ -134,6 +134,7 @@ Some commands such as `disco` return a setInterval ID. This allows you to stop a

```
var bb8 = require('bb8-commander');
var appRootPath = require('app-root-path');
// Used to create a .bb8config file within your users home directory.
bb8.setup();
Expand All @@ -151,6 +152,33 @@ var id = bb8.executeCommand('disco');
// Used to cancel the disco command.
clearInterval(id);
// Used to execute a custom command you have created.
var filePath = appRootPath + '/path/to/custom/command';
bb8.executeCustomCommand(filePath, options)
```

You can write custom command which can be executed via the executeCustomCommand function. A custom command accepts two parameters, the bb8 instance and any options you wish to pass in.

```
// example custom command
module.exports = function(bb8, options) {
bb8.color(options.colour);
bb8.roll(0, Math.floor(Math.random() * 180));
}
```

You can then execute this command like this

```
var bb8 = require('bb8-commander');
var appRootPath = require('app-root-path');
// Used to execute a custom command you have created.
var filePath = appRootPath + '/path/to/custom/command';
bb8.executeCustomCommand(filePath, {colour: '#000000'});
```

# Examples
Expand Down
3 changes: 2 additions & 1 deletion index.js
@@ -1,2 +1,3 @@
module.exports.setup = require('./commands/setup');
module.exports.executeCommand = require('./libs/execute-command');
module.exports.executeCommand = require('./libs/execute-command').connectAndSendCommand;
module.exports.executeCustomCommand = require('./libs/executeCommand').connectAndSendCustomCommand;
12 changes: 12 additions & 0 deletions libs/execute-command.js
Expand Up @@ -2,6 +2,18 @@ var bb8 = require('./bb8-instance')(),
appRootPath = require('app-root-path'),
_ = require('lodash');

module.exports.connectAndSendCustomCommand = function(filePath, options) {
if (bb8) {
bb8.connect(function () {
require(filePath)(bb8, options);
});

return;
}

console.log("BB8 Is not Connected");
}

module.exports.connectAndSendCommand = function (command, options) {

if (bb8) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "bb8-commander",
"version": "2.1.0",
"version": "2.2.0",
"description": "A Node CLI Tool for Sphero BB8 Robot.",
"main": "index.js",
"bin": {
Expand Down

0 comments on commit b29a80f

Please sign in to comment.