forked from beautifier/js-beautify
/
beautify_js
executable file
·53 lines (40 loc) · 1.35 KB
/
beautify_js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/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