#!/usr/bin/env node
'use strict';

var fs = require('fs');
var path = require('path');
var meow = require('meow');
var getStdin = require('get-stdin');
var pathExists = require('path-exists');
var Imagemin = require('./');

var cli = meow({
	help: [
		'Usage',
		'  $ imagemin <file> <directory>',
		'  $ imagemin <directory> <output>',
		'  $ imagemin <file> > <output>',
		'  $ cat <file> | imagemin > <output>',
		'',
		'Example',
		'  $ imagemin images/* build',
		'  $ imagemin images build',
		'  $ imagemin foo.png > foo-optimized.png',
		'  $ cat foo.png | imagemin > foo-optimized.png',
		'',
		'Options',
		'  -i, --interlaced                    Interlace gif for progressive rendering',
		'  -o, --optimizationLevel <number>    Optimization level between 0 and 7',
		'  -p, --progressive                   Lossless conversion to progressive'
	].join('\n')
}, {
	boolean: [
		'interlaced',
		'progressive'
	],
	string: [
		'optimizationLevel'
	],
	alias: {
		i: 'interlaced',
		o: 'optimizationLevel',
		p: 'progressive'
	}
});

function isFile(path) {
	if (/^[^\s]+\.\w*$/.test(path)) {
		return true;
	}

	try {
		return fs.statSync(path).isFile();
	} catch (err) {
		return false;
	}
}

function run(src, dest) {
	var imagemin = new Imagemin()
		.src(src)
		.use(Imagemin.gifsicle(cli.flags))
		.use(Imagemin.jpegtran(cli.flags))
		.use(Imagemin.optipng(cli.flags))
		.use(Imagemin.svgo());

	if (process.stdout.isTTY) {
		imagemin.dest(dest ? dest : 'build');
	}

	imagemin.run(function (err, files) {
		if (err) {
			console.error(err.message);
			process.exit(1);
		}

		if (!process.stdout.isTTY) {
			files.forEach(function (file) {
				process.stdout.write(file.contents);
			});
		}
	});
}

if (process.stdin.isTTY) {
	var src = cli.input;
	var dest;

	if (!cli.input.length) {
		console.error([
			'Provide at least one file to optimize',
			'',
			'Example',
			'  imagemin images/* build',
			'  imagemin foo.png > foo-optimized.png',
			'  cat foo.png | imagemin > foo-optimized.png'
		].join('\n'));

		process.exit(1);
	}

	if (src.length > 1 && !isFile(src[src.length - 1])) {
		dest = src[src.length - 1];
		src.pop();
	}

	src = src.map(function (s) {
		if (!isFile(s) && pathExists.sync(s)) {
			return path.join(s, '**/*');
		}

		return s;
	});

	run(src, dest);
} else {
	getStdin.buffer(run);
}

@@ -1704,24 +1704,45 @@
 %09%7D);%0A%7D%0A%0Aif (
+!cli.input.length && 
 process.stdi
@@ -1756,69 +1756,8 @@
 ) %7B%0A
-%09var src = cli.input;%0A%09var dest;%0A%0A%09if (!cli.input.length) %7B%0A%09
 %09con
@@ -1769,17 +1769,16 @@
 error(%5B%0A
-%09
 %09%09'Provi
@@ -1814,22 +1814,20 @@
 ize',%0A%09%09
-%09
 '',%0A
-%09
 %09%09'Examp
@@ -1829,25 +1829,24 @@
 Example',%0A%09%09
-%09
 '  imagemin 
@@ -1862,17 +1862,16 @@
 build',%0A
-%09
 %09%09'  ima
@@ -1906,17 +1906,16 @@
 d.png',%0A
-%09
 %09%09'  cat
@@ -1955,17 +1955,16 @@
 ed.png'%0A
-%09
 %09%5D.join(
@@ -1972,17 +1972,16 @@
 %5Cn'));%0A%0A
-%09
 %09process
@@ -1990,18 +1990,75 @@
 xit(1);%0A
-%09
 %7D
+%0A%0Aif (cli.input.length) %7B%0A%09var src = cli.input;%0A%09var dest;
 %0A%0A%09if (s

Improve TTY check