js-preflight 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/bin/preflight CHANGED
@@ -39,13 +39,23 @@ USAGE
39
39
  banner
40
40
  end
41
41
 
42
+ def ensure_copy(src, dst)
43
+ FileUtils.mkdir_p(Pathname.new(dst).dirname)
44
+ if File.directory?(src)
45
+ FileUtils.cp_r(src, dst)
46
+ else
47
+ FileUtils.cp(src, dst)
48
+ end
49
+ end
50
+
42
51
  end
43
52
  end
44
53
  end
45
54
 
46
55
  options = {}
47
56
  js_output = StringIO.new
48
- regexp = Js::Preflight::JsRegexp
57
+ js_regexp = Js::Preflight::JsRegexp
58
+ css_regexp = Js::Preflight::CSSRegexp
49
59
  protected_paths = %w{/bin /sbin /usr/bin /usr/sbin /usr/local/bin /usr/local/sbin /var /var/log /var/spool /etc}
50
60
 
51
61
  optparse = OptionParser.new do |opts|
@@ -84,7 +94,7 @@ optparse = OptionParser.new do |opts|
84
94
  end
85
95
 
86
96
  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|
97
+ opts.on("--build-path PATH", "Output the provided HTML file(s), packaged Javascript", " at assets.js, css files, images, and excluded js files to PATH.", " Protected paths are silently ignored; ask an adult for help.", " Javascript file output -j is ignored with this option.") do |path|
88
98
  unless protected_paths.include?(path)
89
99
  options[:build_path] = path
90
100
  end
@@ -142,10 +152,10 @@ if ARGV.length > 0
142
152
  out_filename = Pathname.new(File.join(options[:build_path], path.basename))
143
153
  base_path = File.expand_path(options[:build_path])
144
154
  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
155
+ css_path = File.join(base_path, "css")
156
+ images_path = File.join(base_path, "images")
157
+ build_dirs = [base_path] #, js_path, css_path, images_path # only copy these if they're needed
158
+ FileUtils.mkdir_p(*build_dirs)
149
159
 
150
160
  # Re-read the file line by line, and construct a target duplicate with
151
161
  # script tags for packed files removed. If a script tag is encountered
@@ -156,41 +166,47 @@ if ARGV.length > 0
156
166
  file.rewind
157
167
  File.open(out_filename, "w+") do |outfile|
158
168
  while line = file.gets do
159
- match = line[regexp, 1]
160
-
169
+ match = line[js_regexp, 1]
170
+ case
161
171
  # If this is a script tag, and we haven't packed it, make sure the file
162
172
  # is copied and the script tag is retained.
163
173
  # 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
174
+ # insert a script tag that links to our assets.js file.
175
+ when match = line[js_regexp, 1]
167
176
  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))
177
+ ::Js::Preflight::Instructions.ensure_copy(File.join(path.dirname, match), File.join(base_path, match))
170
178
  outfile << line
171
179
  elsif match == js_files.last
172
180
  outfile << %Q{<script src="js/assets.js" type="text/javascript" charset="utf-8"></script>}
173
181
  end
182
+
183
+ # When a stylesheet link is encountered, ensure it is copied as well.
184
+ when match = line[css_regexp, 1]
185
+ ::Js::Preflight::Instructions.ensure_copy(File.join(path.dirname, match), File.join(base_path, match))
186
+ outfile << line
187
+
188
+ # If this isn't a script tag, just append to the target file.
174
189
  else
175
190
  outfile << line
176
191
  end
177
-
178
192
  end
179
193
  end
180
-
181
194
  end
182
-
183
195
  end
184
-
196
+
197
+ # Finally, copy the packed file as assets.js, and the entire images directory
198
+ # (if it exists), to build_path if we're doing that.
185
199
  if options[:build_path]
186
- # js_output.close
187
- # FileUtils.mv(js_output.path, File.join(options[:build_path], "js", "assets.js"))
188
200
  js_output.rewind
189
201
  File.open(File.join(options[:build_path], "js", "assets.js"), "w") do |assets|
190
202
  assets.write js_output.read
191
203
  end
204
+ images_path = File.join(Pathname.new(ARGV[0]).dirname, "images")
205
+ ::Js::Preflight::Instructions.ensure_copy(images_path, options[:build_path]) if Dir.exists?(images_path)
206
+ # or output the packed .js file as a file, if we're doing that
192
207
  elsif options[:js_file]
193
208
  js_output.close
209
+ # otherwise, just output the packed .js file to STDOUT
194
210
  else
195
211
  js_output.rewind
196
212
  STDOUT.puts js_output.read
@@ -1,5 +1,5 @@
1
1
  module Js
2
2
  module Preflight
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
data/lib/js-preflight.rb CHANGED
@@ -6,7 +6,8 @@ require 'js-preflight/version'
6
6
 
7
7
  module Js
8
8
  module Preflight
9
- JsRegexp = %r|src=\"(.+\.js)\"|
9
+ JsRegexp = %r|src=\"(.+\.js)\"|
10
+ CSSRegexp = %r|href=\"(.+\.css)\"|
10
11
  Basedir = File.expand_path(File.join(__FILE__, "..", ".."))
11
12
  end
12
13
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 3
9
- version: 0.0.3
8
+ - 4
9
+ version: 0.0.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Scott Burton