-
Notifications
You must be signed in to change notification settings - Fork 1
/
minify.rb
46 lines (38 loc) · 1.45 KB
/
minify.rb
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
# Watches current directory for changes to js files
# Automatically minifies them, using YUI Compressor
# YUI Compressor jar file was renamed to 'yui.jar'
# Run by using `ruby minify.rb
require 'listen'
directory = Dir.pwd
puts `clear`
puts "Watching the #{directory} directory for changes"
listener = Listen.to(directory) do |modified, added, removed|
(modified + added).each do |file|
# strip the filename from the full pathname
local = File.basename(file)
# convert regular js file to minified js file
if /^.*js/.match(local) && !local.include?('.min.js')
cmd = "java -jar yui.jar #{file} -o #{file.gsub(/js$/, 'min.js')}"
puts "#{Time.now.strftime("%H:%M:%S")}: #{file.gsub(directory, '')}" \
" -> #{file.gsub(directory, '').gsub(/js$/, 'min.js')}"
`#{cmd}`
end
end
removed.each do |file|
local = File.basename(file)
# convert regular js file to minified js file
if /^.*js/.match(local) && !local.include?('.min.js')
begin
File.delete(file.gsub('.js', '.min.js'))
puts "#{Time.now.strftime("%H:%M:%S")}: #{file.gsub(directory, '')}" \
" removed, deleting #{file.gsub(directory, '').gsub(/js$/, 'min.js')}"
rescue Errno::ENOENT => e
puts "#{Time.now.strftime("%H:%M:%S")}: #{file.gsub(directory, '')}" \
" removed, but #{file.gsub(directory, '').gsub(/js$/, 'min.js')}" \
" not found"
end
end
end
end
listener.start
sleep