<commit_before>#!/usr/bin/env node

var notifier = require('./');
var minimist = require('minimist');
var usage = require('cli-usage');

var aliases = {
  'help': 'h',
  'title': 't',
  'subtitle': 'st',
  'message': 'm',
  'icon': 'i',
  'sound': 's',
  'open': 'o'
};

var argv = minimist(process.argv.slice(2), {
  alias: aliases,
  string: ['icon', 'message', 'open', 'subtitle', 'title']
});

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;
  }
  doNotification(passedOptions);
});

function doNotification (options) {

  if (!options.message) {
    // Do not show an empty message
    process.exit(0);
  }
  notifier.notify(options, function (err, msg) {
    if (err) {
      console.error(err.message);
      process.exit(1);
    }

    if (!msg) return;<commit_msg>Fixes non-TTY usage and piping messages and closes #109<commit_after>#!/usr/bin/env node

var notifier = require('./');
var minimist = require('minimist');
var usage = require('cli-usage');

var aliases = {
  'help': 'h',
  'title': 't',
  'subtitle': 'st',
  'message': 'm',
  'icon': 'i',
  'sound': 's',
  'open': 'o'
};

var argv = minimist(process.argv.slice(2), {
  alias: aliases,
  string: ['icon', 'message', 'open', 'subtitle', 'title']
});

readme(aliases);

var passedOptions = getOptionsIfExists(Object.keys(aliases), argv);
var 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) {

  if (!options.message) {
    // Do not show an empty message
    process.exit(0);
  }
  notifier.notify(options, function (err, msg) {
    if (err) {
      console.error(err.message);
      process.exit(1);
    }

    if (!msg) return;
{'fraction_non_alphanumeric': 0.1849453662484824, 'fraction_numerical': 0.004046944556859571, 'mean_word_length': 3.5947955390334574, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1278799', 'n_tokens_mistral': 874, 'n_tokens_neox': 858, 'n_words': 189}