Module: LuxAssets::Cli
Overview
command line helpers
Instance Method Summary collapse
- #die(text) ⇒ Object
- #info(text) ⇒ Object
-
#monitor ⇒ Object
monitor for file changes.
- #run(what, opts = {}) ⇒ Object
- #warn(text) ⇒ Object
Instance Method Details
#die(text) ⇒ Object
6 7 8 9 10 11 |
# File 'lib/lux_assets/cli.rb', line 6 def die text text = text.red if text.respond_to?(:red) puts text puts caller.slice(0, 10) exit end |
#info(text) ⇒ Object
18 19 20 |
# File 'lib/lux_assets/cli.rb', line 18 def info text puts text.yellow end |
#monitor ⇒ Object
monitor for file changes
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 |
# File 'lib/lux_assets/cli.rb', line 49 def monitor puts 'Lux assets - looking for file changes' list = LuxAssets .to_h .values .map(&:values) .flatten .map { |it| Pathname.new it } files = list.inject({}) do |h, file| if file.to_s.end_with?('.scss') # if target file is scss, monitor all child css and scss files h[file.to_s] = Dir['%s/**/*' % file.dirname] .select { |file| file.end_with?('.scss') || file.end_with?('.css') } .map { |it| Pathname.new it } else # othervise monitor only target file h[file.to_s] = [file] end h end last_change = Time.now while true changed = false for key, values in files for file in values if file.mtime > last_change changed = true LuxAssets.compile key, force: true end end end last_change = Time.now if changed sleep 2 end end |
#run(what, opts = {}) ⇒ Object
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 |
# File 'lib/lux_assets/cli.rb', line 22 def run what, opts={} info (opts[:message] || what) stdin, stdout, stderr, wait_thread = Open3.popen3(what) error = stderr.gets while line = stderr.gets do error += line end # node-sass prints to stderror on complete error = nil if error && error.include?('Rendering Complete, saving .css file...') if error puts '--- command ' puts what.yellow puts '--- error response' opts[:cache_file].unlink if opts[:cache_file] && opts[:cache_file].exist? warn error puts '--- end' false else true end end |
#warn(text) ⇒ Object
13 14 15 16 |
# File 'lib/lux_assets/cli.rb', line 13 def warn text text = text.magenta if text.respond_to?(:magenta) puts text end |