Skip to content

Commit

Permalink
Read piped input by default
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwiseman committed Jan 21, 2016
1 parent db89721 commit fa8c0eb
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 34 deletions.
18 changes: 11 additions & 7 deletions js/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ function processInputSync(filepath) {
if (filepath === '-') {
input = process.stdin;
input.resume();

if (input.isRaw === false) {
throw 'Must pipe input or define at least one file.';
}

input.setEncoding('utf8');

input.on('data', function(chunk) {
Expand Down Expand Up @@ -415,6 +420,7 @@ function checkType(parsed) {
function checkFiles(parsed) {
var argv = parsed.argv;


if (!parsed.files) {
parsed.files = [];
} else {
Expand All @@ -427,7 +433,9 @@ function checkFiles(parsed) {
if (argv.remain.length) {
// assume any remaining args are files
argv.remain.forEach(function(f) {
parsed.files.push(path.resolve(f));
if (f !== '-') {
parsed.files.push(path.resolve(f));
}
});
}

Expand All @@ -438,13 +446,9 @@ function checkFiles(parsed) {
parsed.replace = true;
}

if (argv.original.indexOf('-') > -1) {
// ensure '-' without '-f' still consumes stdin
parsed.files.push('-');
}

if (!parsed.files.length) {
throw 'Must define at least one file.';
// read stdin by default
parsed.files.push('-');
}
debug('files.length ' + parsed.files.length);

Expand Down
13 changes: 12 additions & 1 deletion js/test/shell-smoke-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ test_cli_common()
echo Script: $CLI_SCRIPT

# should find the minimal help output
$CLI_SCRIPT 2>&1 | grep -q "Must define at least one file\." || {
$CLI_SCRIPT 2>&1 | grep -q "define at least one file\." || {
$CLI_SCRIPT 2>&1
echo "[$CLI_SCRIPT_NAME] Output should be help message."
exit 1
}
Expand Down Expand Up @@ -77,6 +78,16 @@ test_cli_js_beautify()
exit 1
}

cat $SCRIPT_DIR/../bin/js-beautify.js | $CLI_SCRIPT | diff $SCRIPT_DIR/../bin/js-beautify.js - || {
echo "js-beautify output for $SCRIPT_DIR/../bin/js-beautify.js was expected to be unchanged."
exit 1
}

cat $SCRIPT_DIR/../bin/js-beautify.js | $CLI_SCRIPT - | diff $SCRIPT_DIR/../bin/js-beautify.js - || {
echo "js-beautify output for $SCRIPT_DIR/../bin/js-beautify.js was expected to be unchanged."
exit 1
}

rm -rf /tmp/js-beautify-mkdir
$CLI_SCRIPT -o /tmp/js-beautify-mkdir/js-beautify.js $SCRIPT_DIR/../bin/js-beautify.js && diff $SCRIPT_DIR/../bin/js-beautify.js /tmp/js-beautify-mkdir/js-beautify.js || {
echo "js-beautify output for $SCRIPT_DIR/../bin/js-beautify.js should have been created in /tmp/js-beautify-mkdir/js-beautify.js."
Expand Down
47 changes: 25 additions & 22 deletions python/jsbeautifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1895,34 +1895,37 @@ def main():


if not file:
print("Must define at least one file.", file=sys.stderr)
file = '-'

if file == '-' and sys.stdin.isatty():
print("Must pipe input or define at least one file.", file=sys.stderr)
return usage(sys.stderr)
else:
try:
if outfile == 'stdout' and replace and not file == '-':
outfile = file

pretty = beautify_file(file, js_options)
try:
if outfile == 'stdout' and replace and not file == '-':
outfile = file

pretty = beautify_file(file, js_options)

if outfile == 'stdout':
if outfile == 'stdout':
# python automatically converts newlines in text to "\r\n" when on windows
# switch to binary to prevent this
if sys.platform == "win32":
import msvcrt
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)

sys.stdout.write(pretty)
else:
if isFileDifferent(outfile, pretty):
mkdir_p(os.path.dirname(outfile))
# python automatically converts newlines in text to "\r\n" when on windows
# switch to binary to prevent this
if sys.platform == "win32":
import msvcrt
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
with io.open(outfile, 'wt', newline='') as f:
f.write(pretty)

sys.stdout.write(pretty)
else:
if isFileDifferent(outfile, pretty):
mkdir_p(os.path.dirname(outfile))
# python automatically converts newlines in text to "\r\n" when on windows
# switch to binary to prevent this
with io.open(outfile, 'wt', newline='') as f:
f.write(pretty)

except Exception as ex:
print(ex, file=sys.stderr)
return 1
except Exception as ex:
print(ex, file=sys.stderr)
return 1

# Success
return 0
18 changes: 14 additions & 4 deletions python/jsbeautifier/tests/shell-smoke-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ test_cli_common()
echo Script: $CLI_SCRIPT

# should find the minimal help output
$CLI_SCRIPT 2>&1 | grep -q "Must define at least one file\." || {
$CLI_SCRIPT 2>&1 | grep -q "define at least one file\." || {
$CLI_SCRIPT 2>&1
echo "[$CLI_SCRIPT_NAME] Output should be help message."
exit 1
}
Expand Down Expand Up @@ -71,15 +72,24 @@ test_cli_js_beautify()
exit 1
}

# On windows python automatically converts newlines to windows format
# This occurs on both pipes and files.
# As a short-term workaround, disabling these two tests on windows.
$CLI_SCRIPT $SCRIPT_DIR/../../../js/bin/js-beautify.js | diff $SCRIPT_DIR/../../../js/bin/js-beautify.js - || {
$CLI_SCRIPT $SCRIPT_DIR/../../../js/bin/js-beautify.js | diff $SCRIPT_DIR/../../../js/bin/js-beautify.js - | cat -t -e
echo "js-beautify output for $SCRIPT_DIR/../../../js/bin/js-beautify.js was expected to be unchanged."
exit 1
}

cat $SCRIPT_DIR/../../../js/bin/js-beautify.js | $CLI_SCRIPT | diff $SCRIPT_DIR/../../../js/bin/js-beautify.js - || {
$CLI_SCRIPT $SCRIPT_DIR/../../../js/bin/js-beautify.js | diff $SCRIPT_DIR/../../../js/bin/js-beautify.js - | cat -t -e
echo "js-beautify output for $SCRIPT_DIR/../../../js/bin/js-beautify.js was expected to be unchanged."
exit 1
}

cat $SCRIPT_DIR/../../../js/bin/js-beautify.js | $CLI_SCRIPT - | diff $SCRIPT_DIR/../../../js/bin/js-beautify.js - || {
$CLI_SCRIPT $SCRIPT_DIR/../../../js/bin/js-beautify.js | diff $SCRIPT_DIR/../../../js/bin/js-beautify.js - | cat -t -e
echo "js-beautify output for $SCRIPT_DIR/../../../js/bin/js-beautify.js was expected to be unchanged."
exit 1
}

rm -rf /tmp/js-beautify-mkdir
$CLI_SCRIPT -o /tmp/js-beautify-mkdir/js-beautify.js $SCRIPT_DIR/../../../js/bin/js-beautify.js && diff $SCRIPT_DIR/../../../js/bin/js-beautify.js /tmp/js-beautify-mkdir/js-beautify.js || {
$CLI_SCRIPT -o /tmp/js-beautify-mkdir/js-beautify.js $SCRIPT_DIR/../../../js/bin/js-beautify.js && diff $SCRIPT_DIR/../../../js/bin/js-beautify.js /tmp/js-beautify-mkdir/js-beautify.js | cat -t -e
Expand Down

0 comments on commit fa8c0eb

Please sign in to comment.