forked from jeffsu/js2
/
js2
executable file
·130 lines (97 loc) · 3.24 KB
/
js2
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env ruby
require 'optparse'
require 'ostruct'
require 'rubygems'
require 'js2'
options = OpenStruct.new
options.rails = false
options.sleep_time = 1
options.rdoc_bin = 'rdoc'
op = OptionParser.new do |opts|
opts.banner = "Usage: js2 <daemon|compile|rdoc|help|config> [options]"
opts.on('-r', '--rails', "Rails (out_dir: ./public/javascript js2_dir: ./app/js2)") do |r|
options.rails = true
end
opts.on('-e', '--environment ENV', "Mostly for use with rails or a yml file with multiple configurations.") do |env|
options.env = env
end
opts.on('-c', '--config FILE', "Use js2.yml config file (run: js2 config to generate a config file)") do |file|
options.yml = YAML.load_file(file)
end
opts.on('-i', '--include-rails', "Mostly for use with rails or a yml file with multiple configurations.") do |env|
options.include_rails = true
end
opts.on('-s', '--sleep N', "Defines the wait time in between directory checks. This is only applicable to daemon mode.") do |t|
options.sleep_time = t.to_i
end
opts.on('-j', '--js2-dir DIR', "Directory where js2 files reside. Defaults to current directory.") do |d|
options.js2_dir = d
end
opts.on('-o', '--out-dir DIR', "Output directory where javascript is written to. Defaults to current directory.") do |d|
options.out_dir = d
end
opts.on('-h', '--haml-dir DIR', "Directory where js2.haml files reside. Defaults to current directory.") do |d|
options.haml_dir = d
end
opts.on('-d', '--rdoc EXECUTABLE', "Rdoc executable to use. Defaults to 'rdoc'") do |d|
options.rdoc_bin = d
end
end
main_action = ARGV.first
if [ 'daemon', 'compile', 'rdoc', 'help', 'config' ].include?(main_action)
else
main_action = nil
end
if main_action == 'help' || ! main_action
puts op
exit
end
if ! main_action
main_action = 'daemon'
end
op.parse!
if options.yml && options.env
options.yml = options.yml[options.env]
end
if options.yml
options.yml.each_pair do |k,v|
options.send(k +'=', v) if options.send(k) == nil
end
end
puts options.inspect
puts options.include_rails.inspect
if options.include_rails
puts "loading rails environment..."
ENV['RAILS_ENV'] = options.env if options.env
require './config/environment'
end
config = JS2::Util::Config.new
if options.rails
config.rails!
end
config.out_dir = options.out_dir if options.out_dir
config.js2_dir = options.js2_dir if options.js2_dir
config.haml_dir = options.haml_dir if options.haml_dir
config.haml_engine = eval('::' + options.haml_engine_class + '.new') if options.haml_engine_class
puts config
if main_action == 'config'
puts "# run: js2 config > js2.yml to create a config file"
puts config.example_yml
exit
end
processor = JS2::Util::Processor.new(config)
if main_action == 'daemon'
puts "running in daemon mode..."
while 1
res = processor.process!
puts "Compiling #{Time.now}..." if res[:changed].any?
sleep options.sleep_time
end
elsif main_action == 'compile'
puts "compiling once! use daemon to monitor changes and compile automatically."
res = processor.process!
puts "Compiling #{Time.now}..." if res[:changed].any?
elsif main_action == 'rdoc'
res = processor.process!
JS2::Util::Rdoc.build(res[:pages], config.file_handler, options.rdoc_bin)
end