lib/webpack_driver/process.rb in webpack_driver-0.1.1 vs lib/webpack_driver/process.rb in webpack_driver-0.2.0

- old
+ new

@@ -8,22 +8,28 @@ class Process READ_CHUNK_SIZE = 1024 extend Forwardable - attr_reader :assets, :buffer + def_delegators :@proc, :alive?, :environment, :wait - def initialize(*flags) + attr_reader :assets, :messages + attr_reader :config + + def initialize(script, config) self.reset! - args = ["node"] + flags + @config = config + args = ["./node_modules/.bin/#{script}"] + config.flags + @proc = ::ChildProcess.build(*args) - @proc.environment['NODE_ENV'] = WebpackDriver.config.environment - @proc.cwd = WebpackDriver.config.directory - end - def alive? - @proc.alive? + @proc.environment.merge!( + config.environment + ) + puts "ENV" + p config.environment + @proc.cwd = config.directory end def start self.reset! @output, w = IO.pipe @@ -32,12 +38,12 @@ w.close @listener = listen_for_status_updates end def stop - @output.close unless @output.closed? @proc.stop + @output.close unless @output.closed? @listener.join end def valid? last_compilation_message['operation'] == 'emit' @@ -45,31 +51,47 @@ protected def reset! @assets = Concurrent::Map.new - @buffer = Concurrent::Array.new + @messages = Concurrent::Array.new end def last_compilation_message - msg = @buffer.reverse_each.detect{ |l| l['type'] == 'compile' } + msg = @messages.reverse_each.detect{ |l| l['type'] == 'compile' } msg ? msg['value'] : {} end + def record_error(error) + config.logger.error( + "#{error['name']}: #{error['resource']}\n#{error['message']}" + ) + end + def record_message(msg) - if msg['type'] == 'asset' + case msg['type'] + when 'asset' name = msg['value']['name'] @assets[name] = Asset.new(name, msg['value']['size']) + when 'error' + record_error(msg['value']) + else + config.logger.debug(msg) end - @buffer << msg + @messages << msg end def listen_for_status_updates Thread.new do @output.each_line do | l | - match = l.match(/STATUS: (.*)/) - record_message(JSON.parse(match[1])) if match + match = l.match(/^STATUS: (.*)/) + if match + record_message(JSON.parse(match[1])) + config.logger.debug(l.chomp) + else + config.logger.info(l.chomp) + end end end end end