forked from beautifier/js-beautify
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added bin/beautify_js, which is a wrapper for rhino beautify-cl.js that
also lets you supply input via standard input rather than suppyling a filename. This allows it to be used in vim as a filter command.
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#!/usr/bin/ruby | ||
|
||
def print_usage | ||
puts <<End | ||
Usage: #{File.basename($0)} file | ||
Usage: echo 'JavaScript input' | #{File.basename($0)} | ||
Using this is a vim filter command | ||
---------------------------------- | ||
This can also be used as a vim filter command (see help filter). | ||
Simply select the lines to be beautified in visual mode and type .!beautify_js. | ||
Better yet, create a vim command to execute it for you, and put that in your .vimrc: | ||
command! -range=% -nargs=0 BeautifyJavascript <line1>,<line2>!#{$0} | ||
Then you can simply type BeautifyJavascript to process the entire buffer or select a range of lines to only pass those lines through the filter. | ||
End | ||
exit | ||
end | ||
|
||
|
||
if STDIN.tty? | ||
if ARGV.size >= 1 | ||
# Get the absolute path of the filename given | ||
require 'pathname' | ||
path = Pathname.new(ARGV[0]).realpath.to_s | ||
else | ||
print_usage | ||
end | ||
else | ||
# Assume they are piping the input in. Save that input in a temporary file and pass that file to beautify-cl.js | ||
require 'tempfile' | ||
file = Tempfile.new('beautify_js') | ||
file.puts STDIN.read | ||
file.close | ||
path = file.path | ||
end | ||
#system "cat #{path}" | ||
|
||
|
||
# Change directory so that the load() calls in beautify-cl.js are able to find the files they need | ||
Dir.chdir File.dirname(__FILE__) | ||
Dir.chdir '..' | ||
#puts Dir.getwd | ||
|
||
|
||
command = "rhino beautify-cl.js '#{path}' 2>&1" | ||
#puts command | ||
#output = `#{command}` | ||
system command | ||
|