Skip to content

Commit

Permalink
Fix stdin decetion in child-processes
Browse files Browse the repository at this point in the history
There’s no way to accuratly detect *if* text is going to be
piped in, especially when using child-processes (e.g., in gulp)
This fixes that by only throwing errors when the CLI is
sure nothing is going to be piped in.

See the below issue for more info.

Closes GH-58.
  • Loading branch information
wooorm committed Sep 15, 2015
1 parent c699bca commit ef29701
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/cli/file-set-pipeline/stdin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@
*/

var debug = require('debug')('mdast:cli:file-set-pipeline:stdin');
var fs = require('fs');
var toVFile = require('to-vfile');
var concat = require('concat-stream');

/*
* Constants.
*/

var expextPipeIn = !process.stdin.isTTY;
var isTTY = process.stdin.isTTY;
var isFIFO = fs.fstatSync(0).isFIFO();

var definitelyTTY = isTTY === true || isFIFO === true;
var expextPipeIn = !isTTY;

/**
* Read from standard in.
Expand All @@ -36,7 +41,7 @@ function stdin(program, callback) {
if (program.files.length) {
debug('Ignoring stdin');

if (expextPipeIn) {
if (definitelyTTY && expextPipeIn) {
err = new Error('mdast does not accept both files and stdin');
} else if (program.filePath) {
err = new Error(
Expand All @@ -50,7 +55,7 @@ function stdin(program, callback) {
return;
}

if (!expextPipeIn) {
if (definitelyTTY && !expextPipeIn) {
callback(new Error('No input'));

return;
Expand Down
8 changes: 8 additions & 0 deletions test/cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ it "Should accept stdin"
cat $markdown | $COMMAND > /dev/null 2>&1
assert $? 0

it "Should accept stdin as herestring"
$COMMAND <<< $(cat $markdown) > /dev/null 2>&1
assert $? 0

it "Should fail without input"
$COMMAND > /dev/null 2>&1
assert $? 1
Expand All @@ -88,6 +92,10 @@ it "Should fail on stdin and files"
cat $markdown | $COMMAND $markdown > /dev/null 2>&1
assert $? 1

it "Should ignore herestring when with files (for now)"
$COMMAND $markdown <<< "$(cat $markdown)" > /dev/null 2>&1
assert $? 0

#
# `--ast`.
#
Expand Down

0 comments on commit ef29701

Please sign in to comment.