diff --git a/README.md b/README.md index f25ce35..be14ee5 100644 --- a/README.md +++ b/README.md @@ -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(); @@ -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 diff --git a/index.js b/index.js index b45a074..fe9df55 100644 --- a/index.js +++ b/index.js @@ -1,2 +1,3 @@ module.exports.setup = require('./commands/setup'); -module.exports.executeCommand = require('./libs/execute-command'); \ No newline at end of file +module.exports.executeCommand = require('./libs/execute-command').connectAndSendCommand; +module.exports.executeCustomCommand = require('./libs/executeCommand').connectAndSendCustomCommand; \ No newline at end of file diff --git a/libs/execute-command.js b/libs/execute-command.js index 1ef1125..dae52f8 100644 --- a/libs/execute-command.js +++ b/libs/execute-command.js @@ -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) { diff --git a/package.json b/package.json index e928a34..2062fca 100644 --- a/package.json +++ b/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": {