js-preflight 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/bin/preflight +47 -13
  2. data/lib/js-preflight/version.rb +1 -1
  3. 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[:html_path] = false
87
- opts.on("--html-path PATH", "Output the provided HTML file(s), with affected script tags", " replaced by the packaged version, at <PATH>/[JS_FILE].", " Protected paths are silently ignored; ask an adult for help.", " Depends on -j, which provides the basename for JS_FILE") do |path|
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[:html_path] = path
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[:html_path]
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
- outfile << line unless js_files.include?(line[regexp, 1])
151
- outfile << %Q{<script src="#{js_output.path}" type="text/javascript" charset="utf-8"></script>} if line[regexp, 1] == js_files.last
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[:js_file]
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
@@ -1,5 +1,5 @@
1
1
  module Js
2
2
  module Preflight
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Scott Burton