Skip to content

Commit

Permalink
refactor and fix issue #16; sending all known/unknown commands to redis
Browse files Browse the repository at this point in the history
  • Loading branch information
lujiajing1126 committed Dec 27, 2018
1 parent a9d49d0 commit acaf016
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions lib/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,24 @@ class RedisClient {

execute(commands) {
const CMD = commands.shift().toLowerCase();
const func = this._redis_client[`${CMD}Async`];
if (typeof func == "function") {
return func.bind(this._redis_client)(...commands)
.then(function (result) {
if (Array.isArray(result)) {
result.forEach((item, index) => {
console.log("%d) %s", index, item);
});
} else {
console.log(result);
}
});
} else {
console.log(colors.red("(error) ERR unknown command `%s`, with args beginning with: %s"), CMD, commands[0] || "");
return Promise.resolve();
let func = this._redis_client[`${CMD}Async`];
if (typeof func !== "function") {
func = this._redis_client[`send_commandAsync`];
// recombine commands
commands = [CMD, commands];
}
return func.bind(this._redis_client)(...commands)
.then(function (result) {
if (Array.isArray(result)) {
result.forEach((item, index) => {
console.log("%d) %s", index, item);
});
} else {
console.log(result);
}
}).catch((e) => {
console.log(colors.red(`(error) ${e.message}`));
});
}

attachEvent() {
Expand Down Expand Up @@ -86,10 +88,11 @@ class RedisClient {
readline.cursorTo(process.stdout, 0, 0);
readline.clearScreenDown(process.stdout);
} else {
this.execute(commands);
this.execute([CMD, ...commands]).finally(() => {
rl.prompt();
});
}
}
rl.prompt();
} catch (err) {
console.log(colors.red(`(error) ${err.message}`));
rl.prompt();
Expand Down

0 comments on commit acaf016

Please sign in to comment.