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/

164
cli.js
View File

@ -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 <url>")
.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 <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]")
.description("Starts watching for contact changes")

View File

@ -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;

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": {