Skip to content

Commit b29a80f

Browse files
authored
Merge pull request #27 from mintuz/ALLOW_Custom_Commands
Code for executing custom commands
2 parents f8c37e7 + c71c639 commit b29a80f

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

README.md

+28
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ Some commands such as `disco` return a setInterval ID. This allows you to stop a
134134

135135
```
136136
var bb8 = require('bb8-commander');
137+
var appRootPath = require('app-root-path');
137138
138139
// Used to create a .bb8config file within your users home directory.
139140
bb8.setup();
@@ -151,6 +152,33 @@ var id = bb8.executeCommand('disco');
151152
152153
// Used to cancel the disco command.
153154
clearInterval(id);
155+
156+
157+
// Used to execute a custom command you have created.
158+
var filePath = appRootPath + '/path/to/custom/command';
159+
bb8.executeCustomCommand(filePath, options)
160+
161+
```
162+
163+
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.
164+
165+
```
166+
// example custom command
167+
module.exports = function(bb8, options) {
168+
bb8.color(options.colour);
169+
bb8.roll(0, Math.floor(Math.random() * 180));
170+
}
171+
```
172+
173+
You can then execute this command like this
174+
175+
```
176+
var bb8 = require('bb8-commander');
177+
var appRootPath = require('app-root-path');
178+
179+
// Used to execute a custom command you have created.
180+
var filePath = appRootPath + '/path/to/custom/command';
181+
bb8.executeCustomCommand(filePath, {colour: '#000000'});
154182
```
155183

156184
# Examples

index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
module.exports.setup = require('./commands/setup');
2-
module.exports.executeCommand = require('./libs/execute-command');
2+
module.exports.executeCommand = require('./libs/execute-command').connectAndSendCommand;
3+
module.exports.executeCustomCommand = require('./libs/executeCommand').connectAndSendCustomCommand;

libs/execute-command.js

+12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ var bb8 = require('./bb8-instance')(),
22
appRootPath = require('app-root-path'),
33
_ = require('lodash');
44

5+
module.exports.connectAndSendCustomCommand = function(filePath, options) {
6+
if (bb8) {
7+
bb8.connect(function () {
8+
require(filePath)(bb8, options);
9+
});
10+
11+
return;
12+
}
13+
14+
console.log("BB8 Is not Connected");
15+
}
16+
517
module.exports.connectAndSendCommand = function (command, options) {
618

719
if (bb8) {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bb8-commander",
3-
"version": "2.1.0",
3+
"version": "2.2.0",
44
"description": "A Node CLI Tool for Sphero BB8 Robot.",
55
"main": "index.js",
66
"bin": {

0 commit comments

Comments
 (0)