From 800adf3e30a025880b9d530b5f6c5220e4fcb5ec Mon Sep 17 00:00:00 2001 From: alwx Date: Sun, 21 May 2017 18:02:21 +0300 Subject: [PATCH] Status development tools 3.1.0: Responses support, better error descriptions. --- .gitignore | 3 + cli.js | 164 ++++++++++++++++++++++++++++++++++++--------------- index.js | 97 ++++++++++++++++++------------ package.json | 2 +- 4 files changed, 180 insertions(+), 86 deletions(-) diff --git a/.gitignore b/.gitignore index 4f3dd98..8a34045 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,8 @@ .DS_Store +*.iml +/.idea + # node.js # node_modules/ diff --git a/cli.js b/cli.js index 59d014b..ccc986f 100755 --- a/cli.js +++ b/cli.js @@ -91,72 +91,140 @@ function getPackageData(contact) { function printMan() { console.error(chalk.red("Cannot connect to Status.")); console.log("1. Please, ensure that your device is connected to your computer;"); - console.log("2. If it is connected, ensure that you're logged in and the debug mode is enabled."); + console.log("2. If it is connected, ensure that you're logged in and the debug mode is enabled;"); + console.log("3. If you use Android, you should also execute " + + chalk.yellow("adb forward tcp:5561 tcp:5561") + " before using development tools.") console.log(); - console.log("Check our guide for more information:"); - console.log("https://github.com/status-im/status-dev-cli/blob/master/README.md"); + console.log("Check our docs for more information:"); + console.log("http://docs.status.im/"); +} + +function printServerProblem() { + console.log("Server doesn't respond properly."); + console.log("Please, re-run it or re-login to your account."); + console.log(); + console.log("Check our docs for more information:"); + console.log("http://docs.status.im/"); } cli.command("add [contact]") .description("Adds a contact") .action(function (contact) { - var statusDev = new StatusDev({ip: cli.ip || defaultIp}); - var contactData = getPackageData(contact); - if (contactData) { - statusDev.addContact(contactData, function(err, result) { - if (err) { - printMan(); - } else { - console.log(chalk.green("Contact has been added succesfully.")); - } - }); - } + var statusDev = new StatusDev({ip: cli.ip || defaultIp}); + var contactData = getPackageData(contact); + if (contactData) { + statusDev.addContact(contactData, function(err, body) { + if (err) { + printMan(); + } else if (body.type == "error") { + console.log(chalk.red(body.text)); + } else { + console.log(chalk.green(body.text)); + } + }); + } }); cli.command("remove [contact]") .description("Removes a contact") .action(function (contact) { - var statusDev = new StatusDev({ip: cli.ip || defaultIp}); - var contactData = getPackageData(contact); - if (contactData) { - statusDev.removeContact(contactData, function(err, result) { - if (err) { - printMan(); - } else { - console.log(chalk.green("Contact has been removed succesfully.")); - } - }); - } + var statusDev = new StatusDev({ip: cli.ip || defaultIp}); + var contactData = getPackageData(contact); + if (contactData) { + statusDev.removeContact(contactData, function(err, body) { + if (err) { + printMan(); + } else if (body.type == "error") { + console.log(chalk.red(body.text)); + } else { + console.log(chalk.green(body.text)); + } + }); + } }); cli.command("refresh [contact]") - .description("Refreshes a debuggable contact") - .action(function (contact) { - var statusDev = new StatusDev({ip: cli.ip || defaultIp}); - var contactData = getPackageData(contact); - if (contactData) { - statusDev.refreshContact(contactData, function(err, result) { - if (err) { - printMan(); - } else { - console.log(chalk.green("Contact has been refreshed succesfully.")); + .description("Refreshes a debuggable contact") + .action(function (contact) { + var statusDev = new StatusDev({ip: cli.ip || defaultIp}); + var contactData = getPackageData(contact); + if (contactData) { + statusDev.refreshContact(contactData, function(err, body) { + if (err) { + printMan(); + } else if (body.type == "error") { + console.log(chalk.red(body.text)); + } else { + console.log(chalk.green(body.text)); + } + }); } - }); - } - }); + }); cli.command("switch-node ") - .description("Switches the current RPC node") - .action(function (url) { - var statusDev = new StatusDev({ip: cli.ip || defaultIp}); - statusDev.switchNode(url, function(err, result) { - if (err) { - printMan(); - } else { - console.log(chalk.green("RPC node has been changed succesfully.")); - } + .description("Switches the current RPC node") + .action(function (url) { + var statusDev = new StatusDev({ip: cli.ip || defaultIp}); + statusDev.switchNode(url, function(err, body) { + if (err) { + printMan(); + } else if (body.type == "error") { + console.log(chalk.red(body.text)); + } else { + console.log(chalk.green(body.text)); + } + }); + }); + +cli.command("list") + .description("Displays all debuggable DApps and bots") + .action(function () { + var statusDev = new StatusDev({ip: cli.ip || defaultIp}); + statusDev.listDApps(function (err, body) { + if (err) { + printMan(); + } else if (body === undefined) { + printServerProblem(); + } else { + body.data.forEach(function(item, i, arr) { + if (item["dapp-url"]) { + console.log(chalk.green(chalk.bold(item["whisper-identity"]) + + " (Name: \"" + item.name + "\", DApp URL: \"" + item["dapp-url"] + "\")")); + } else if (item["bot-url"]) { + console.log(chalk.cyan(chalk.bold(item["whisper-identity"]) + + " (Name: \"" + item.name + "\", Bot URL: \"" + item["bot-url"] + "\")")); + } + }); + } + }); + }); + +cli.command("log ") + .description("Returns log for a specified DApp or bot") + .action(function (identity) { + var statusDev = new StatusDev({ip: cli.ip || defaultIp}); + statusDev.getLog(identity, function (err, body) { + if (err) { + printMan(); + } else if (body === undefined) { + printServerProblem(); + } else if (body.type == "error") { + console.log(chalk.red(body.text)); + } else { + body.data.forEach(function(item, i, arr) { + var time = new Date(item.timestamp).toLocaleString(); + + if (item.content.startsWith("error:")) { + console.log(chalk.red(time + " " + item.content)); + } else if (item.content.startsWith("warn:")) { + console.log(chalk.cyan(time + " " + item.content)); + } else { + console.log(time + " " + item.content); + } + }); + } + }); }); - }); cli.command("watch [dir] [contact]") .description("Starts watching for contact changes") diff --git a/index.js b/index.js index 58e42c1..8fc7236 100755 --- a/index.js +++ b/index.js @@ -11,55 +11,78 @@ function fromAscii(str) { }; var StatusDev = function(options) { - this.url = "http://" + options.ip + ":5561"; + this.url = "http://" + options.ip + ":5561"; }; StatusDev.prototype.addContact = function(contactData, cb) { - request({ - url: this.url + "/add-dapp", - method: "POST", - json: true, - body: { encoded: fromAscii(contactData) } - }, function (error, response, body) { - if (cb === undefined) { return } - cb(error, response); - }); + request({ + url: this.url + "/add-dapp", + method: "POST", + json: true, + body: { 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) { - if (cb === undefined) { return } - cb(error, response); - }); + request({ + url: this.url + "/remove-dapp", + method: "POST", + json: true, + body: { 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) { - if (cb === undefined) { return } - cb(error, response); - }); + request({ + url: this.url + "/dapp-changed", + method: "POST", + json: true, + body: { 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({"url": rpcUrl})} - }, function (error, response, body) { - if (cb === undefined) { return } - cb(error, response); - }); + request({ + url: this.url + "/switch-node", + method: "POST", + json: true, + body: {encoded: fromAscii({"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" + }, 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) { + if (cb === undefined) { return } + cb(error, body); + }); }; module.exports = StatusDev; diff --git a/package.json b/package.json index 89c8c37..5b6c78c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "status-dev-cli", - "version": "3.0.2", + "version": "3.1.0", "description": "CLI for Status", "main": "index.js", "bin": {