Skip to content

Commit

Permalink
Use process.stdout/stderr.write to print to terminal.
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacgr committed Oct 19, 2020
1 parent 54752d2 commit 2030a2a
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions bin/client
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,13 @@ const getClient = () => {

const makeRequests = async () => {
if (program.clientType === "http" && program.subscribe) {
console.error("HTTP client does not support subscriptions.");
process.stderr.write("HTTP client does not support subscriptions." + "\n");
process.exit(-1);
}
if (!program.method && !program.notify && !program.subscribe) {
console.error("No method, notification or subscription request provided.");
process.stderr.write(
"No method, notification or subscription request provided." + "\n"
);
process.exit(-1);
}
if (program.method) {
Expand All @@ -169,7 +171,7 @@ const handleSubscription = (result) => {
if (program.write) {
fs.appendFileSync(program.write, JSON.stringify(result) + "\n");
} else {
console.log(result);
process.stdout.write(result + "\n");
}
};

Expand All @@ -179,13 +181,13 @@ const sendRequest = async (method, params) => {
if (program.write) {
fs.writeFileSync(program.write, JSON.stringify(result));
} else {
console.log(result);
process.stdout.write(JSON.stringify(result, null, 2) + "\n");
}
} catch (error) {
if (program.write) {
fs.writeFileSync(program.write, JSON.stringify(error));
} else {
console.error(error);
process.stderr.write(JSON.stringify(error, null, 2) + "\n");
}
}
};
Expand All @@ -197,13 +199,13 @@ const sendNotification = async (method, params) => {
if (program.write) {
fs.writeFileSync(program.write, JSON.stringify(result.body));
} else {
console.log(result.body);
process.stdout.write(JSON.stringify(result.body, null, 2) + "\n");
}
} catch (error) {
if (program.write) {
fs.writeFileSync(program.write, JSON.stringify(error.body));
} else {
console.error(error.body);
process.stderr.write(JSON.stringify(error.body, null, 2) + "\n");
}
}
} else {
Expand All @@ -221,7 +223,7 @@ if (program.clientType === "http" || program.clientType === "https") {
makeRequests();
})
.catch((error) => {
console.error(error);
process.stderr.write(JSON.stringify(error, null, 2) + "\n");
process.exit(-1);
});
}

0 comments on commit 2030a2a

Please sign in to comment.