Skip to content

Commit

Permalink
Added support for editorconfig from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwiseman committed Dec 26, 2016
1 parent 17cb3d8 commit ae231db
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 40 deletions.
44 changes: 28 additions & 16 deletions js/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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');
Expand Down Expand Up @@ -531,6 +541,8 @@ function checkFiles(parsed) {
debug("error querying for isTTY:", ex);
}

debug('isTTY: ' + isTTY);

if (!parsed.files) {
parsed.files = [];
} else {
Expand All @@ -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
Expand Down
83 changes: 74 additions & 9 deletions js/test/shell-smoke-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
31 changes: 25 additions & 6 deletions python/jsbeautifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down Expand Up @@ -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':
Expand All @@ -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)
Expand Down
83 changes: 74 additions & 9 deletions python/jsbeautifier/tests/shell-smoke-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit ae231db

Please sign in to comment.