Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read piped input by default #849

Merged
merged 1 commit into from
Jan 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions js/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ function processInputSync(filepath) {
if (filepath === '-') {
input = process.stdin;
input.resume();

input.setEncoding('utf8');

input.on('data', function(chunk) {
Expand Down Expand Up @@ -414,6 +415,13 @@ function checkType(parsed) {

function checkFiles(parsed) {
var argv = parsed.argv;
var isTTY = true;

try {
isTTY = process.stdin.isTTY;
} catch (ex) {
debug("error querying for isTTY:", ex);
}

if (!parsed.files) {
parsed.files = [];
Expand All @@ -427,7 +435,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,15 +448,15 @@ function checkFiles(parsed) {
parsed.replace = true;
}

if (argv.original.indexOf('-') > -1) {
// ensure '-' without '-f' still consumes stdin
if (!parsed.files.length) {
// read stdin by default
parsed.files.push('-');
}
debug('files.length ' + parsed.files.length);

if (!parsed.files.length) {
throw 'Must define at least one file.';
if (parsed.files.indexOf('-') > -1 && isTTY) {
throw 'Must pipe input or define at least one file.';
}
debug('files.length ' + parsed.files.length);

parsed.files.forEach(testFilePath);

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 "Must pipe input or 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
62 changes: 36 additions & 26 deletions python/jsbeautifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,23 @@ def beautify(string, opts = default_options() ):
return b.beautify(string, opts)

def beautify_file(file_name, opts = default_options() ):
input_string = ''
if file_name == '-': # stdin
stream = sys.stdin
try:
if sys.stdin.isatty():
raise

stream = sys.stdin
input_string = ''.join(stream.readlines())
except Exception as ex:
print("Must pipe input or define at least one file.", file=sys.stderr)
usage(sys.stderr)
raise
else:
stream = io.open(file_name, 'rt', newline='')
input_string = ''.join(stream.readlines())

return beautify(''.join(stream.readlines()), opts)
return beautify(input_string, opts)


def usage(stream=sys.stdout):
Expand Down Expand Up @@ -1895,34 +1906,33 @@ def main():


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

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

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)
pretty = beautify_file(file, js_options)

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)
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)

except Exception as ex:
print(ex, file=sys.stderr)
return 1
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
# set newline to empty 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

# 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 "Must pipe input or 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