Status development tools 3.1.0: Responses support, better error descriptions.

This commit is contained in:
alwx 2017-05-21 18:02:21 +03:00
parent 1c0b5618ff
commit 800adf3e30
4 changed files with 180 additions and 86 deletions

3
.gitignore vendored
View File

@ -1,5 +1,8 @@
.DS_Store
*.iml
/.idea
# node.js
#
node_modules/

90
cli.js
View File

@ -91,10 +91,20 @@ 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]")
@ -103,11 +113,13 @@ cli.command("add [contact]")
var statusDev = new StatusDev({ip: cli.ip || defaultIp});
var contactData = getPackageData(contact);
if (contactData) {
statusDev.addContact(contactData, function(err, result) {
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("Contact has been added succesfully."));
console.log(chalk.green(body.text));
}
});
}
@ -119,11 +131,13 @@ cli.command("remove [contact]")
var statusDev = new StatusDev({ip: cli.ip || defaultIp});
var contactData = getPackageData(contact);
if (contactData) {
statusDev.removeContact(contactData, function(err, result) {
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("Contact has been removed succesfully."));
console.log(chalk.green(body.text));
}
});
}
@ -135,11 +149,13 @@ cli.command("refresh [contact]")
var statusDev = new StatusDev({ip: cli.ip || defaultIp});
var contactData = getPackageData(contact);
if (contactData) {
statusDev.refreshContact(contactData, function(err, result) {
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("Contact has been refreshed succesfully."));
console.log(chalk.green(body.text));
}
});
}
@ -149,11 +165,63 @@ cli.command("switch-node <url>")
.description("Switches the current RPC node")
.action(function (url) {
var statusDev = new StatusDev({ip: cli.ip || defaultIp});
statusDev.switchNode(url, function(err, result) {
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("RPC node has been changed succesfully."));
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 <identity>")
.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);
}
});
}
});
});

View File

@ -22,7 +22,7 @@ StatusDev.prototype.addContact = function(contactData, cb) {
body: { encoded: fromAscii(contactData) }
}, function (error, response, body) {
if (cb === undefined) { return }
cb(error, response);
cb(error, body);
});
};
@ -34,7 +34,7 @@ StatusDev.prototype.removeContact = function(contactData, cb) {
body: { encoded: fromAscii(contactData) }
}, function (error, response, body) {
if (cb === undefined) { return }
cb(error, response);
cb(error, body);
});
};
@ -46,7 +46,7 @@ StatusDev.prototype.refreshContact = function(contactData, cb) {
body: { encoded: fromAscii(contactData) }
}, function (error, response, body) {
if (cb === undefined) { return }
cb(error, response);
cb(error, body);
});
};
@ -58,7 +58,30 @@ StatusDev.prototype.switchNode = function(rpcUrl, cb) {
body: {encoded: fromAscii({"url": rpcUrl})}
}, function (error, response, body) {
if (cb === undefined) { return }
cb(error, response);
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);
});
};

View File

@ -1,6 +1,6 @@
{
"name": "status-dev-cli",
"version": "3.0.2",
"version": "3.1.0",
"description": "CLI for Status",
"main": "index.js",
"bin": {