#!/usr/bin/env node

import inline from 'inline-source';
import yargs from 'yargs';
import fs from 'fs';

// inline-source --compress false --root ./ in.html [out.html]

let argv = yargs
	.usage(`Usage: $0 [--compress] [--root cwd] in.html [out.html]`)
	.help()
	.boolean('compress')
	.alias('z', 'compress')
	.string('attribute')
	.default('attribute', 'inline')
	.string('root')
	.alias('d', 'root')
	.alias('h', 'help')
	.demand(1)
	.argv;

inline(argv._[0], {
	compress: argv.compress,
	rootpath: argv.root || process.cwd(),
	attribute: argv.attribute
}, (err, html) => {
	if (err) {
		process.stderr.write(`Error: ${err}\n`);
		return process.exit(1);
	}

	let out = argv._[1];
	if (out) {
		fs.writeFile(out, html, err => {
			if (err) {
				process.stderr.write(`Error: ${err}\n`);
				return process.exit(1);
			}

			process.stderr.write(`Written to ${out}\n`);
			process.exit(0);
		});
	}
	else {
		process.stdout.write(html + '\n');
		process.exit(0);
	}
});

@@ -441,28 +441,389 @@
 v;%0A%0A
-inline(argv._%5B0%5D, %7B%0A
+let source = argv._%5B0%5D;%0A%0A// pass %22-%22 to read from stdin%0Aif (source==='-') %7B%0A%09source = '';%0A%09process.stdin.setEncoding('utf8');%0A%09process.stdin.on('readable', () =%3E %7B%0A%09%09let chunk = process.stdin.read();%0A%09%09if (chunk!==null) source += chunk;%0A%09%7D);%0A%09process.stdin.on('end', () =%3E %7B%0A%09%09run(source, argv);%0A%09%7D);%0A%7D%0Aelse %7B%0A%09run(source, argv);%0A%7D%0A%0Afunction run(source, argv) %7B%0A%09inline(source, %7B%0A%09
 %09com
@@ -844,16 +844,17 @@
 mpress,%0A
+%09
 %09rootpat
@@ -868,16 +868,33 @@
 .root %7C%7C
+ argv.rootpath %7C%7C
  process
@@ -901,16 +901,17 @@
 .cwd(),%0A
+%09
 %09attribu
@@ -929,16 +929,17 @@
 tribute%0A
+%09
 %7D, (err,
@@ -950,16 +950,17 @@
 l) =%3E %7B%0A
+%09
 %09if (err
@@ -961,24 +961,25 @@
 f (err) %7B%0A%09%09
+%09
 process.stde
@@ -1009,16 +1009,17 @@
 %5Cn%60);%0A%09%09
+%09
 return p
@@ -1034,20 +1034,22 @@
 xit(1);%0A
+%09
 %09%7D%0A%0A
+%09
 %09let out
@@ -1062,16 +1062,17 @@
 v._%5B1%5D;%0A
+%09
 %09if (out
@@ -1075,16 +1075,17 @@
 (out) %7B%0A
+%09
 %09%09fs.wri
@@ -1114,16 +1114,17 @@
 =%3E %7B%0A%09%09%09
+%09
 if (err)
@@ -1126,16 +1126,17 @@
 (err) %7B%0A
+%09
 %09%09%09%09proc
@@ -1172,16 +1172,17 @@
 r%7D%5Cn%60);%0A
+%09
 %09%09%09%09retu
@@ -1204,19 +1204,21 @@
 (1);%0A%09%09%09
+%09
 %7D%0A%0A
+%09
 %09%09%09proce
@@ -1256,24 +1256,25 @@
 ut%7D%5Cn%60);%0A%09%09%09
+%09
 process.exit
@@ -1284,15 +1284,18 @@
 ;%0A%09%09
+%09
 %7D);%0A
+%09
 %09%7D%0A
+%09
 %09els
@@ -1300,16 +1300,17 @@
 lse %7B%0A%09%09
+%09
 process.
@@ -1338,16 +1338,17 @@
 %5Cn');%0A%09%09
+%09
 process.
@@ -1360,11 +1360,15 @@
 0);%0A
+%09
 %09%7D%0A
+%09
 %7D);%0A
+%7D%0A

Add support for reading from stdin (fixes #2)