diff --git a/js/lib/cli.js b/js/lib/cli.js index 47644d8f0..8da37a3a8 100755 --- a/js/lib/cli.js +++ b/js/lib/cli.js @@ -383,6 +383,25 @@ function processInputSync(filepath) { outfile = filepath; } + var fileType = getOutputType(outfile, config.type); + + if (config.editorconfig) { + var editorconfig_filepath = filepath; + + if (editorconfig_filepath === '-') { + if (outfile) { + editorconfig_filepath = outfile; + } else { + editorconfig_filepath = 'stdin.' + fileType; + } + } + + debug("EditorConfig is enabled for ", editorconfig_filepath); + config = cc(config).snapshot; + set_file_editorconfig_opts(editorconfig_filepath, config); + debug(config); + } + if (filepath === '-') { input = process.stdin; input.resume(); @@ -394,28 +413,16 @@ function processInputSync(filepath) { }); input.on('end', function() { - makePretty(data, config, outfile, writePretty); // Where things get beautified + makePretty(fileType, data, config, outfile, writePretty); // Where things get beautified }); } else { - // Only enable editorconfig with files (stdin not suppored). - if (config.editorconfig) { - debug("EditorConfig is enabled for ", filepath); - config = cc(config).snapshot; - set_file_editorconfig_opts(filepath, config); - debug(config); - } - - if (outfile) { - mkdirp.sync(path.dirname(outfile)); - } data = fs.readFileSync(filepath, 'utf8'); - makePretty(data, config, outfile, writePretty); + makePretty(fileType, data, config, outfile, writePretty); } } -function makePretty(code, config, outfile, callback) { +function makePretty(fileType, code, config, outfile, callback) { try { - var fileType = getOutputType(outfile, config.type); var pretty = beautify[fileType](code, config); callback(null, pretty, outfile, config); @@ -425,12 +432,15 @@ function makePretty(code, config, outfile, callback) { } function writePretty(err, pretty, outfile, config) { + debug('writing ' + outfile); if (err) { console.error(err); process.exit(1); } if (outfile) { + mkdirp.sync(path.dirname(outfile)); + if (isFileDifferent(outfile, pretty)) { try { fs.writeFileSync(outfile, pretty, 'utf8'); @@ -531,6 +541,8 @@ function checkFiles(parsed) { debug("error querying for isTTY:", ex); } + debug('isTTY: ' + isTTY); + if (!parsed.files) { parsed.files = []; } else { @@ -549,7 +561,7 @@ function checkFiles(parsed) { }); } - if ('string' === typeof parsed.outfile && !parsed.files.length) { + if ('string' === typeof parsed.outfile && isTTY && !parsed.files.length) { // use outfile as input when no other files passed in args parsed.files.push(parsed.outfile); // operation is now an implicit overwrite diff --git a/js/test/shell-smoke-test.sh b/js/test/shell-smoke-test.sh index f92eeac8d..b8521fbdd 100755 --- a/js/test/shell-smoke-test.sh +++ b/js/test/shell-smoke-test.sh @@ -104,6 +104,11 @@ test_cli_js_beautify() } setup_temp + cat $SCRIPT_DIR/../bin/js-beautify.js | $CLI_SCRIPT -o $TEST_TEMP/js-beautify-pipe.js - && diff $TEST_TEMP/js-beautify-pipe.js $SCRIPT_DIR/../bin/js-beautify.js || { + echo "js-beautify output for $SCRIPT_DIR/../bin/js-beautify.js should have been created in $TEST_TEMP/js-beautify-pipe.js." + cleanup 1 + } + $CLI_SCRIPT -o $TEST_TEMP/js-beautify.js $SCRIPT_DIR/../bin/js-beautify.js && diff $SCRIPT_DIR/../bin/js-beautify.js $TEST_TEMP/js-beautify.js || { echo "js-beautify output for $SCRIPT_DIR/../bin/js-beautify.js should have been created in $TEST_TEMP/js-beautify.js." cleanup 1 @@ -169,29 +174,89 @@ test_cli_js_beautify() # TODO: EditorConfig setting should NOT overide cli setting, but that is # the current by-design behavior, due to code limitations. + # file input scenario + SCENARIO=a + cd $TEST_TEMP/editorconfig || exit 1 + $CLI_SCRIPT --end-with-newline --indent-size 6 --editorconfig -o example-${SCENARIO}.js example.js \ + && diff -q example-${SCENARIO}.js example-ec.js || { + echo "EditorConfig setting should overide cli setting." + diff example-${SCENARIO}.js example-ec.js | cat -t -e + cleanup 1 + } + + cd $TEST_TEMP/editorconfig/crlf || exit 1 + $CLI_SCRIPT --end-with-newline --indent-size 6 --editorconfig -o example-${SCENARIO}.js example.js \ + && diff -q example-${SCENARIO}.js example-ec.js || { + echo "EditorConfig setting should overide cli setting." + diff example-${SCENARIO}.js example-ec.js | cat -t -e + cleanup 1 + } + + cd $TEST_TEMP/editorconfig/cr || exit 1 + $CLI_SCRIPT --end-with-newline --indent-size 6 --editorconfig -o example-${SCENARIO}.js example.js \ + && diff -q example-${SCENARIO}.js example-ec.js || { + echo "EditorConfig setting should overide cli setting." + diff example-${SCENARIO}.js example-ec.js | cat -t -e + cleanup 1 + } + + # stdin input to stdout scenario + SCENARIO=b cd $TEST_TEMP/editorconfig || exit 1 - $CLI_SCRIPT -r --end-with-newline --indent-size 6 --editorconfig example.js \ - && diff -q example.js example-ec.js || { + echo "cat example.js | $CLI_SCRIPT --end-with-newline --indent-size 6 --editorconfig > example-${SCENARIO}.js" + cat example.js | $CLI_SCRIPT --end-with-newline --indent-size 6 --editorconfig > example-${SCENARIO}.js \ + && diff -q example-${SCENARIO}.js example-ec.js || { echo "EditorConfig setting should overide cli setting." - diff example.js example-ec.js | cat -t -e + diff example-${SCENARIO}.js example-ec.js | cat -t -e cleanup 1 } cd $TEST_TEMP/editorconfig/crlf || exit 1 - $CLI_SCRIPT -r --end-with-newline --indent-size 6 --editorconfig example.js \ - && diff -q example.js example-ec.js || { + echo "cat example.js | $CLI_SCRIPT --end-with-newline --indent-size 6 --editorconfig > example-${SCENARIO}.js" + cat example.js | $CLI_SCRIPT --end-with-newline --indent-size 6 --editorconfig > example-${SCENARIO}.js \ + && diff -q example-${SCENARIO}.js example-ec.js || { echo "EditorConfig setting should overide cli setting." - diff example.js example-ec.js | cat -t -e + diff example-${SCENARIO}.js example-ec.js | cat -t -e cleanup 1 } cd $TEST_TEMP/editorconfig/cr || exit 1 - $CLI_SCRIPT -r --end-with-newline --indent-size 6 --editorconfig example.js \ - && diff -q example.js example-ec.js || { + echo "cat example.js | $CLI_SCRIPT --end-with-newline --indent-size 6 --editorconfig > example-${SCENARIO}.js" + cat example.js | $CLI_SCRIPT --end-with-newline --indent-size 6 --editorconfig > example-${SCENARIO}.js \ + && diff -q example-${SCENARIO}.js example-ec.js || { echo "EditorConfig setting should overide cli setting." - diff example.js example-ec.js | cat -t -e + diff example-${SCENARIO}.js example-ec.js | cat -t -e cleanup 1 } + + + # stdin input to file scenario + SCENARIO=c + cd $TEST_TEMP/editorconfig || exit 1 + echo "cat example.js | $CLI_SCRIPT --end-with-newline --indent-size 6 --editorconfig -o example-${SCENARIO}.js" + cat example.js | $CLI_SCRIPT --end-with-newline --indent-size 6 --editorconfig -o example-${SCENARIO}.js - \ + && diff -q example-${SCENARIO}.js example-ec.js || { + echo "EditorConfig setting should overide cli setting." + diff example-${SCENARIO}.js example-ec.js | cat -t -e + cleanup 1 + } + + cd $TEST_TEMP/editorconfig/crlf || exit 1 + cat example.js | $CLI_SCRIPT --end-with-newline --indent-size 6 --editorconfig -o example-${SCENARIO}.js - \ + && diff -q example-${SCENARIO}.js example-ec.js || { + echo "EditorConfig setting should overide cli setting." + diff example-${SCENARIO}.js example-ec.js | cat -t -e + cleanup 1 + } + + cd $TEST_TEMP/editorconfig/cr || exit 1 + cat example.js | $CLI_SCRIPT --end-with-newline --indent-size 6 --editorconfig -o example-${SCENARIO}.js - \ + && diff -q example-${SCENARIO}.js example-ec.js || { + echo "EditorConfig setting should overide cli setting." + diff example-${SCENARIO}.js example-ec.js | cat -t -e + cleanup 1 + } + popd # End EditorConfig diff --git a/python/jsbeautifier/__init__.py b/python/jsbeautifier/__init__.py index 1144cc1b9..de72d741a 100644 --- a/python/jsbeautifier/__init__.py +++ b/python/jsbeautifier/__init__.py @@ -315,11 +315,6 @@ def beautify_file(file_name, opts = default_options() ): stream = io.open(file_name, 'rt', newline='') input_string = ''.join(stream.readlines()) - # Editorconfig used only on files, not stdin - if getattr(opts, 'editorconfig'): - opts = copy.copy(opts) - set_file_editorconfig_opts(file_name, opts) - return beautify(input_string, opts) @@ -2265,6 +2260,21 @@ def main(): if outfile == 'stdout' and replace and not file == '-': outfile = file + # Editorconfig used only on files, not stdin + if getattr(js_options, 'editorconfig'): + editorconfig_filepath = file + + if editorconfig_filepath == '-': + if outfile != 'stdout': + editorconfig_filepath = outfile + else: + fileType = 'js' + editorconfig_filepath = 'stdin.' + fileType + + # debug("EditorConfig is enabled for ", editorconfig_filepath); + js_options = copy.copy(js_options) + set_file_editorconfig_opts(editorconfig_filepath, js_options) + pretty = beautify_file(file, js_options) if outfile == 'stdout': @@ -2278,10 +2288,19 @@ def main(): 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) + print('writing ' + outfile, file=sys.stderr) + try: + f.write(pretty) + except TypeError: + # This is not pretty, but given how we did the version import + # it is the only way to do this without having setup.py fail on a missing six dependency. + six = __import__("six") + f.write(six.u(pretty)) + except Exception as ex: print(ex, file=sys.stderr) diff --git a/python/jsbeautifier/tests/shell-smoke-test.sh b/python/jsbeautifier/tests/shell-smoke-test.sh index 457fed61c..bd6f38736 100755 --- a/python/jsbeautifier/tests/shell-smoke-test.sh +++ b/python/jsbeautifier/tests/shell-smoke-test.sh @@ -105,6 +105,12 @@ test_cli_js_beautify() } setup_temp + cat $SCRIPT_DIR/../../../js/bin/js-beautify.js | $CLI_SCRIPT -o $TEST_TEMP/js-beautify-pipe.js - || diff $SCRIPT_DIR/../../../js/bin/js-beautify.js $TEST_TEMP/js-beautify-pipe.js || { + echo "js-beautify output for $SCRIPT_DIR/../../../js/bin/js-beautify.js should have been created in $TEST_TEMP/js-beautify-pipe.js." + cleanup 1 + } + + $CLI_SCRIPT -o $TEST_TEMP/js-beautify.js $SCRIPT_DIR/../../../js/bin/js-beautify.js && diff $SCRIPT_DIR/../../../js/bin/js-beautify.js $TEST_TEMP/js-beautify.js || { $CLI_SCRIPT -o $TEST_TEMP/js-beautify.js $SCRIPT_DIR/../../../js/bin/js-beautify.js && diff $SCRIPT_DIR/../../../js/bin/js-beautify.js $TEST_TEMP/js-beautify.js | cat -t -e echo "js-beautify output for $SCRIPT_DIR/../../../js/bin/js-beautify.js should have been created in $TEST_TEMP/js-beautify.js." @@ -173,27 +179,86 @@ test_cli_js_beautify() # TODO: EditorConfig setting should NOT overide cli setting, but that is # the current by-design behavior, due to code limitations. + # file input scenario + SCENARIO=a + cd $TEST_TEMP/editorconfig || exit 1 + $CLI_SCRIPT --end-with-newline --indent-size 6 --editorconfig -o example-${SCENARIO}.js example.js \ + && diff -q example-${SCENARIO}.js example-ec.js || { + echo "EditorConfig setting should overide cli setting." + diff example-${SCENARIO}.js example-ec.js | cat -t -e + cleanup 1 + } + + cd $TEST_TEMP/editorconfig/crlf || exit 1 + $CLI_SCRIPT --end-with-newline --indent-size 6 --editorconfig -o example-${SCENARIO}.js example.js \ + && diff -q example-${SCENARIO}.js example-ec.js || { + echo "EditorConfig setting should overide cli setting." + diff example-${SCENARIO}.js example-ec.js | cat -t -e + cleanup 1 + } + + cd $TEST_TEMP/editorconfig/cr || exit 1 + $CLI_SCRIPT --end-with-newline --indent-size 6 --editorconfig -o example-${SCENARIO}.js example.js \ + && diff -q example-${SCENARIO}.js example-ec.js || { + echo "EditorConfig setting should overide cli setting." + diff example-${SCENARIO}.js example-ec.js | cat -t -e + cleanup 1 + } + + # stdin input to stdout scenario + SCENARIO=b + cd $TEST_TEMP/editorconfig || exit 1 + echo "cat example.js | $CLI_SCRIPT --end-with-newline --indent-size 6 --editorconfig > example-${SCENARIO}.js" + cat example.js | $CLI_SCRIPT --end-with-newline --indent-size 6 --editorconfig > example-${SCENARIO}.js \ + && diff -q example-${SCENARIO}.js example-ec.js || { + echo "EditorConfig setting should overide cli setting." + diff example-${SCENARIO}.js example-ec.js | cat -t -e + cleanup 1 + } + + cd $TEST_TEMP/editorconfig/crlf || exit 1 + echo "cat example.js | $CLI_SCRIPT --end-with-newline --indent-size 6 --editorconfig > example-${SCENARIO}.js" + cat example.js | $CLI_SCRIPT --end-with-newline --indent-size 6 --editorconfig > example-${SCENARIO}.js \ + && diff -q example-${SCENARIO}.js example-ec.js || { + echo "EditorConfig setting should overide cli setting." + diff example-${SCENARIO}.js example-ec.js | cat -t -e + cleanup 1 + } + + cd $TEST_TEMP/editorconfig/cr || exit 1 + echo "cat example.js | $CLI_SCRIPT --end-with-newline --indent-size 6 --editorconfig > example-${SCENARIO}.js" + cat example.js | $CLI_SCRIPT --end-with-newline --indent-size 6 --editorconfig > example-${SCENARIO}.js \ + && diff -q example-${SCENARIO}.js example-ec.js || { + echo "EditorConfig setting should overide cli setting." + diff example-${SCENARIO}.js example-ec.js | cat -t -e + cleanup 1 + } + + + # stdin input to file scenario + SCENARIO=c cd $TEST_TEMP/editorconfig || exit 1 - $CLI_SCRIPT -r --end-with-newline --indent-size 6 --editorconfig example.js \ - && diff -q example.js example-ec.js || { + echo "cat example.js | $CLI_SCRIPT --end-with-newline --indent-size 6 --editorconfig -o example-${SCENARIO}.js" + cat example.js | $CLI_SCRIPT --end-with-newline --indent-size 6 --editorconfig -o example-${SCENARIO}.js - \ + && diff -q example-${SCENARIO}.js example-ec.js || { echo "EditorConfig setting should overide cli setting." - diff example.js example-ec.js | cat -t -e + diff example-${SCENARIO}.js example-ec.js | cat -t -e cleanup 1 } cd $TEST_TEMP/editorconfig/crlf || exit 1 - $CLI_SCRIPT -r --end-with-newline --indent-size 6 --editorconfig example.js \ - && diff -q example.js example-ec.js || { + cat example.js | $CLI_SCRIPT --end-with-newline --indent-size 6 --editorconfig -o example-${SCENARIO}.js - \ + && diff -q example-${SCENARIO}.js example-ec.js || { echo "EditorConfig setting should overide cli setting." - diff example.js example-ec.js | cat -t -e + diff example-${SCENARIO}.js example-ec.js | cat -t -e cleanup 1 } cd $TEST_TEMP/editorconfig/cr || exit 1 - $CLI_SCRIPT -r --end-with-newline --indent-size 6 --editorconfig example.js \ - && diff -q example.js example-ec.js || { + cat example.js | $CLI_SCRIPT --end-with-newline --indent-size 6 --editorconfig -o example-${SCENARIO}.js - \ + && diff -q example-${SCENARIO}.js example-ec.js || { echo "EditorConfig setting should overide cli setting." - diff example.js example-ec.js | cat -t -e + diff example-${SCENARIO}.js example-ec.js | cat -t -e cleanup 1 }