diff --git a/README.md b/README.md index 590d543..7b1293b 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,8 @@ npm i -g status-dev-cli * `--ip ` to specify your device's IP address. If you don't know your device's IP address, just run `status-dev-cli scan`. The IP should be provided for every command you try to execute (except `scan`, of course) +Device IP can also be provided using the `STATUS_DEVICE_IP` environment variable (e.g. `STATUS_DEVICE_IP=192.168.0.2 status-dev-cli list`) + #### 1. Scanning the network ***status-dev-cli 3.2.0+, Status 0.9.8+*** diff --git a/cli.js b/cli.js index 0f6a389..26a35ae 100755 --- a/cli.js +++ b/cli.js @@ -1,20 +1,26 @@ #!/usr/bin/env node const cli = require("commander"); -const child = require('child_process'); const watchman = require('fb-watchman'); const fs = require('fs'); -const path = require('path'); -const request = require('request'); const chalk = require('chalk'); const mdns = require('mdns'); const pkgJson = require(__dirname + '/package.json'); const client = new watchman.Client(); -const defaultIp = "localhost"; +const defaultIp = process.env.STATUS_DEVICE_IP; const statusDebugServerPort = 5561; const StatusDev = require('./index.js'); +function createStatusDev() { + const ip = cli.ip || defaultIp; + if (ip == null) { + console.error(chalk.red("You have provide your device IP using --ip.")); + process.exit(1); + } + return new StatusDev({ip: ip}); +} + function fromAscii(str) { var hex = ""; for(var i = 0; i < str.length; i++) { @@ -104,7 +110,7 @@ function printServerProblem() { cli.command("add [contact]") .description("Adds a contact") .action(function (contact) { - var statusDev = new StatusDev({ip: cli.ip || defaultIp}); + var statusDev = createStatusDev(); var contactData = getPackageData(contact); if (contactData) { statusDev.addContact(contactData, function(err, body) { @@ -122,7 +128,7 @@ cli.command("add [contact]") cli.command("remove [contactIdentity]") .description("Removes a contact") .action(function (contactIdentity) { - var statusDev = new StatusDev({ip: cli.ip || defaultIp}); + var statusDev = createStatusDev(); var contact = null; if (contactIdentity) { @@ -145,7 +151,7 @@ cli.command("remove [contactIdentity]") cli.command("refresh [contactIdentity]") .description("Refreshes a debuggable contact") .action(function (contactIdentity) { - var statusDev = new StatusDev({ip: cli.ip || defaultIp}); + var statusDev = createStatusDev(); var contact = null; if (contactIdentity) { @@ -168,7 +174,7 @@ cli.command("refresh [contactIdentity]") cli.command("switch-node ") .description("Switches the current RPC node") .action(function (url) { - var statusDev = new StatusDev({ip: cli.ip || defaultIp}); + var statusDev = createStatusDev(); statusDev.switchNode(url, function(err, body) { if (err) { printMan(); @@ -183,7 +189,7 @@ cli.command("switch-node ") cli.command("list") .description("Displays all debuggable DApps and bots") .action(function () { - var statusDev = new StatusDev({ip: cli.ip || defaultIp}); + var statusDev = createStatusDev(); statusDev.listDApps(function (err, body) { if (err) { printMan(); @@ -208,7 +214,7 @@ cli.command("list") cli.command("log ") .description("Returns log for a specified DApp or bot") .action(function (contactIdentity) { - var statusDev = new StatusDev({ip: cli.ip || defaultIp}); + var statusDev = createStatusDev(); statusDev.getLog(contactIdentity, function (err, body) { if (err) { printMan(); diff --git a/index.js b/index.js index 40dc176..c7847d5 100755 --- a/index.js +++ b/index.js @@ -14,73 +14,58 @@ var StatusDev = function(options) { this.url = "http://" + options.ip + ":5561"; }; -StatusDev.prototype.addContact = function(contactData, cb) { - request({ - url: this.url + "/add-dapp", +function requestOptions(url, body) { + return { + url: url, method: "POST", + timeout: 3000, json: true, - body: { encoded: fromAscii(contactData) } - }, function (error, response, body) { + body: body}; +} + +StatusDev.prototype.addContact = function(contactData, cb) { + request(requestOptions(this.url + "/add-dapp", { encoded: fromAscii(contactData) }) + , function (error, response, body) { if (cb === undefined) { return } cb(error, body); }); }; StatusDev.prototype.removeContact = function(contactData, cb) { - request({ - url: this.url + "/remove-dapp", - method: "POST", - json: true, - body: { encoded: fromAscii(contactData) } - }, function (error, response, body) { + request(requestOptions(this.url + "/remove-dapp", { encoded: fromAscii(contactData) }) + , function (error, response, body) { if (cb === undefined) { return } cb(error, body); }); }; StatusDev.prototype.refreshContact = function(contactData, cb) { - request({ - url: this.url + "/dapp-changed", - method: "POST", - json: true, - body: { encoded: fromAscii(contactData) } - }, function (error, response, body) { + request(requestOptions(this.url + "/dapp-changed", { encoded: fromAscii(contactData) }) + , function (error, response, body) { if (cb === undefined) { return } cb(error, body); }); }; StatusDev.prototype.switchNode = function(rpcUrl, cb) { - request({ - url: this.url + "/switch-node", - method: "POST", - json: true, - body: {encoded: fromAscii(JSON.stringify({"url": rpcUrl}))} - }, function (error, response, body) { + request(requestOptions(this.url + "/switch-node", {encoded: fromAscii(JSON.stringify({"url": rpcUrl}))}) + , function (error, response, body) { if (cb === undefined) { return } cb(error, body); }); }; StatusDev.prototype.listDApps = function(cb) { - request({ - url: this.url + "/list", - json: true, - method: "POST", - body: {} - }, function (error, response, body) { + request(requestOptions(this.url + "/list", {}) + , function (error, response, body) { if (cb === undefined) { return } cb(error, body); }); }; StatusDev.prototype.getLog = function(identity, cb) { - request({ - url: this.url + "/log", - method: "POST", - json: true, - body: { identity: identity } - }, function (error, response, body) { + request(requestOptions(this.url + "/log", { identity: identity }) + , function (error, response, body) { if (cb === undefined) { return } cb(error, body); }); diff --git a/package.json b/package.json index 914e7e2..4890b67 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "status-dev-cli", - "version": "3.2.8", + "version": "3.2.9", "description": "CLI for Status", "main": "index.js", "bin": {