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 .DS_Store
*.iml
/.idea
# node.js # node.js
# #
node_modules/ node_modules/

164
cli.js
View File

@ -91,72 +91,140 @@ function getPackageData(contact) {
function printMan() { function printMan() {
console.error(chalk.red("Cannot connect to Status.")); console.error(chalk.red("Cannot connect to Status."));
console.log("1. Please, ensure that your device is connected to your computer;"); 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();
console.log("Check our guide for more information:"); console.log("Check our docs for more information:");
console.log("https://github.com/status-im/status-dev-cli/blob/master/README.md"); 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]") cli.command("add [contact]")
.description("Adds a contact") .description("Adds a contact")
.action(function (contact) { .action(function (contact) {
var statusDev = new StatusDev({ip: cli.ip || defaultIp}); var statusDev = new StatusDev({ip: cli.ip || defaultIp});
var contactData = getPackageData(contact); var contactData = getPackageData(contact);
if (contactData) { if (contactData) {
statusDev.addContact(contactData, function(err, result) { statusDev.addContact(contactData, function(err, body) {
if (err) { if (err) {
printMan(); printMan();
} else { } else if (body.type == "error") {
console.log(chalk.green("Contact has been added succesfully.")); console.log(chalk.red(body.text));
} } else {
}); console.log(chalk.green(body.text));
} }
});
}
}); });
cli.command("remove [contact]") cli.command("remove [contact]")
.description("Removes a contact") .description("Removes a contact")
.action(function (contact) { .action(function (contact) {
var statusDev = new StatusDev({ip: cli.ip || defaultIp}); var statusDev = new StatusDev({ip: cli.ip || defaultIp});
var contactData = getPackageData(contact); var contactData = getPackageData(contact);
if (contactData) { if (contactData) {
statusDev.removeContact(contactData, function(err, result) { statusDev.removeContact(contactData, function(err, body) {
if (err) { if (err) {
printMan(); printMan();
} else { } else if (body.type == "error") {
console.log(chalk.green("Contact has been removed succesfully.")); console.log(chalk.red(body.text));
} } else {
}); console.log(chalk.green(body.text));
} }
});
}
}); });
cli.command("refresh [contact]") cli.command("refresh [contact]")
.description("Refreshes a debuggable contact") .description("Refreshes a debuggable contact")
.action(function (contact) { .action(function (contact) {
var statusDev = new StatusDev({ip: cli.ip || defaultIp}); var statusDev = new StatusDev({ip: cli.ip || defaultIp});
var contactData = getPackageData(contact); var contactData = getPackageData(contact);
if (contactData) { if (contactData) {
statusDev.refreshContact(contactData, function(err, result) { statusDev.refreshContact(contactData, function(err, body) {
if (err) { if (err) {
printMan(); printMan();
} else { } else if (body.type == "error") {
console.log(chalk.green("Contact has been refreshed succesfully.")); console.log(chalk.red(body.text));
} else {
console.log(chalk.green(body.text));
}
});
} }
}); });
}
});
cli.command("switch-node <url>") cli.command("switch-node <url>")
.description("Switches the current RPC node") .description("Switches the current RPC node")
.action(function (url) { .action(function (url) {
var statusDev = new StatusDev({ip: cli.ip || defaultIp}); var statusDev = new StatusDev({ip: cli.ip || defaultIp});
statusDev.switchNode(url, function(err, result) { statusDev.switchNode(url, function(err, body) {
if (err) { if (err) {
printMan(); printMan();
} else { } else if (body.type == "error") {
console.log(chalk.green("RPC node has been changed succesfully.")); 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 <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);
}
});
}
});
}); });
});
cli.command("watch [dir] [contact]") cli.command("watch [dir] [contact]")
.description("Starts watching for contact changes") .description("Starts watching for contact changes")

View File

@ -11,55 +11,78 @@ function fromAscii(str) {
}; };
var StatusDev = function(options) { var StatusDev = function(options) {
this.url = "http://" + options.ip + ":5561"; this.url = "http://" + options.ip + ":5561";
}; };
StatusDev.prototype.addContact = function(contactData, cb) { StatusDev.prototype.addContact = function(contactData, cb) {
request({ request({
url: this.url + "/add-dapp", url: this.url + "/add-dapp",
method: "POST", method: "POST",
json: true, json: true,
body: { encoded: fromAscii(contactData) } body: { encoded: fromAscii(contactData) }
}, function (error, response, body) { }, function (error, response, body) {
if (cb === undefined) { return } if (cb === undefined) { return }
cb(error, response); cb(error, body);
}); });
}; };
StatusDev.prototype.removeContact = function(contactData, cb) { StatusDev.prototype.removeContact = function(contactData, cb) {
request({ request({
url: this.url + "/remove-dapp", url: this.url + "/remove-dapp",
method: "POST", method: "POST",
json: true, json: true,
body: { encoded: fromAscii(contactData) } body: { encoded: fromAscii(contactData) }
}, function (error, response, body) { }, function (error, response, body) {
if (cb === undefined) { return } if (cb === undefined) { return }
cb(error, response); cb(error, body);
}); });
}; };
StatusDev.prototype.refreshContact = function(contactData, cb) { StatusDev.prototype.refreshContact = function(contactData, cb) {
request({ request({
url: this.url + "/dapp-changed", url: this.url + "/dapp-changed",
method: "POST", method: "POST",
json: true, json: true,
body: { encoded: fromAscii(contactData) } body: { encoded: fromAscii(contactData) }
}, function (error, response, body) { }, function (error, response, body) {
if (cb === undefined) { return } if (cb === undefined) { return }
cb(error, response); cb(error, body);
}); });
}; };
StatusDev.prototype.switchNode = function(rpcUrl, cb) { StatusDev.prototype.switchNode = function(rpcUrl, cb) {
request({ request({
url: this.url + "/switch-node", url: this.url + "/switch-node",
method: "POST", method: "POST",
json: true, json: true,
body: {encoded: fromAscii({"url": rpcUrl})} body: {encoded: fromAscii({"url": rpcUrl})}
}, function (error, response, body) { }, function (error, response, body) {
if (cb === undefined) { return } 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);
});
}; };
module.exports = StatusDev; module.exports = StatusDev;

View File

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