diff --git a/js/lib/cli.js b/js/lib/cli.js index 0d5ed0b22..23b63e198 100755 --- a/js/lib/cli.js +++ b/js/lib/cli.js @@ -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) { @@ -415,6 +420,7 @@ function checkType(parsed) { function checkFiles(parsed) { var argv = parsed.argv; + if (!parsed.files) { parsed.files = []; } else { @@ -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)); + } }); } @@ -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); diff --git a/js/test/shell-smoke-test.sh b/js/test/shell-smoke-test.sh index fcf68048f..28d7ece22 100755 --- a/js/test/shell-smoke-test.sh +++ b/js/test/shell-smoke-test.sh @@ -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 } @@ -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." diff --git a/python/jsbeautifier/__init__.py b/python/jsbeautifier/__init__.py index afeb51a75..68b3507e3 100644 --- a/python/jsbeautifier/__init__.py +++ b/python/jsbeautifier/__init__.py @@ -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 diff --git a/python/jsbeautifier/tests/shell-smoke-test.sh b/python/jsbeautifier/tests/shell-smoke-test.sh index a14f962b7..6a6efffa0 100755 --- a/python/jsbeautifier/tests/shell-smoke-test.sh +++ b/python/jsbeautifier/tests/shell-smoke-test.sh @@ -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 } @@ -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