js-preflight 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/preflight +47 -13
- data/lib/js-preflight/version.rb +1 -1
- metadata +2 -2
data/bin/preflight
CHANGED
@@ -83,14 +83,10 @@ optparse = OptionParser.new do |opts|
|
|
83
83
|
js_output = File.new(f, "w")
|
84
84
|
end
|
85
85
|
|
86
|
-
options[:
|
87
|
-
opts.on("--
|
88
|
-
unless options[:js_file]
|
89
|
-
STDERR.puts "--html-path PATH requires --js-file JS_FILE"
|
90
|
-
exit
|
91
|
-
end
|
86
|
+
options[:build_path] = false
|
87
|
+
opts.on("--build-path PATH", "Output the provided HTML file(s), packaged Javascript", " at assets.js, and excluded files to PATH.", " Protected paths are silently ignored; ask an adult for help.", " Javascript file output -j is ignored with this option.") do |path|
|
92
88
|
unless protected_paths.include?(path)
|
93
|
-
options[:
|
89
|
+
options[:build_path] = path
|
94
90
|
end
|
95
91
|
end
|
96
92
|
|
@@ -141,21 +137,59 @@ if ARGV.length > 0
|
|
141
137
|
end
|
142
138
|
end
|
143
139
|
|
144
|
-
if options[:
|
140
|
+
if options[:build_path]
|
141
|
+
# Set up the target build directory structure
|
142
|
+
out_filename = Pathname.new(File.join(options[:build_path], path.basename))
|
143
|
+
base_path = File.expand_path(options[:build_path])
|
144
|
+
js_path = File.join(base_path, "js")
|
145
|
+
build_dirs = [base_path, js_path]
|
146
|
+
build_dirs.each do |path|
|
147
|
+
FileUtils.mkdir_p(path)
|
148
|
+
end
|
149
|
+
|
150
|
+
# Re-read the file line by line, and construct a target duplicate with
|
151
|
+
# script tags for packed files removed. If a script tag is encountered
|
152
|
+
# that links a non-packed file (likely in exclude_paths), ensure the file
|
153
|
+
# is copied to the appropriate location in the build directory. Finally,
|
154
|
+
# link the packed .js file where we encounter the last .js file we operated
|
155
|
+
# on.
|
145
156
|
file.rewind
|
146
|
-
out_filename = Pathname.new(File.join(options[:html_path], path.basename))
|
147
|
-
FileUtils.mkdir_p(out_filename.dirname)
|
148
157
|
File.open(out_filename, "w+") do |outfile|
|
149
158
|
while line = file.gets do
|
150
|
-
|
151
|
-
|
159
|
+
match = line[regexp, 1]
|
160
|
+
|
161
|
+
# If this is a script tag, and we haven't packed it, make sure the file
|
162
|
+
# is copied and the script tag is retained.
|
163
|
+
# If it's a script tag and it matches the last of the packed .js files,
|
164
|
+
# insert a script tag that links to our assets.js file.
|
165
|
+
# If this isn't a script tag, just append to the target file.
|
166
|
+
if match
|
167
|
+
if !js_files.include?(match)
|
168
|
+
FileUtils.mkdir_p(File.join(base_path, Pathname.new(match).dirname))
|
169
|
+
FileUtils.cp(File.join(path.dirname, match), File.join(base_path, match))
|
170
|
+
outfile << line
|
171
|
+
elsif match == js_files.last
|
172
|
+
outfile << %Q{<script src="js/assets.js" type="text/javascript" charset="utf-8"></script>}
|
173
|
+
end
|
174
|
+
else
|
175
|
+
outfile << line
|
176
|
+
end
|
177
|
+
|
152
178
|
end
|
153
179
|
end
|
180
|
+
|
154
181
|
end
|
155
182
|
|
156
183
|
end
|
157
184
|
|
158
|
-
if options[:
|
185
|
+
if options[:build_path]
|
186
|
+
# js_output.close
|
187
|
+
# FileUtils.mv(js_output.path, File.join(options[:build_path], "js", "assets.js"))
|
188
|
+
js_output.rewind
|
189
|
+
File.open(File.join(options[:build_path], "js", "assets.js"), "w") do |assets|
|
190
|
+
assets.write js_output.read
|
191
|
+
end
|
192
|
+
elsif options[:js_file]
|
159
193
|
js_output.close
|
160
194
|
else
|
161
195
|
js_output.rewind
|
data/lib/js-preflight/version.rb
CHANGED