From eac63d3f608271055d6f421fb987665c31a3c56d Mon Sep 17 00:00:00 2001 From: Mikael Brevik Date: Wed, 25 May 2016 21:56:55 +0200 Subject: [PATCH] Fixes non-TTY usage and piping messages and closes #109 --- bin.js | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/bin.js b/bin.js index 1b0cc76..381a232 100755 --- a/bin.js +++ b/bin.js @@ -24,23 +24,27 @@ readme(aliases); var passedOptions = getOptionsIfExists(Object.keys(aliases), argv); var stdinMessage = ''; -process.stdin.on('readable', function(){ - var chunk = this.read(); - if (!chunk && !stdinMessage) { - doNotification(passedOptions); - this.end(); - return; - } - if (!chunk) return; - stdinMessage += chunk.toString(); -}); - -process.stdin.on('end', function(){ - if (stdinMessage) { - passedOptions.message = stdinMessage; - } +if (process.stdin.isTTY) { doNotification(passedOptions); -}); +} else { + process.stdin.resume(); + process.stdin.setEncoding('utf8'); + process.stdin.on('data', function(data) { + if (data) { + stdinMessage += data; + } else { + doNotification(passedOptions); + this.end(); + return; + } + }); + process.stdin.on('end', function(){ + if (stdinMessage) { + passedOptions.message = stdinMessage; + } + doNotification(passedOptions); + }); +} function doNotification (options) {