Skip to content

Commit

Permalink
Fixes non-TTY usage and piping messages and closes #109
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelbr committed May 25, 2016
1 parent f2a8ad5 commit eac63d3
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down

0 comments on commit eac63d3

Please sign in to comment.