Sha256: 626cf57d35550bbfc36fc85d7ba0b82b0ade47e29d1da1955732b57fcec2450a

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

require "shellwords"
require "yaml"
require "socket"
require "webpacker/runner"

module Webpacker
  class DevServerRunner < Webpacker::Runner
    def run
      load_config
      detect_port!
      execute_cmd
    end

    private
      def load_config
        @config_file = File.join(@app_path, "config/webpacker.yml")
        dev_server = YAML.load_file(@config_file)[ENV["JETS_ENV"]]["dev_server"]

        @hostname          = dev_server["host"]
        @port              = dev_server["port"]
        @pretty            = dev_server.fetch("pretty", true)

      rescue Errno::ENOENT, NoMethodError
        $stdout.puts "webpack dev_server configuration not found in #{@config_file}."
        $stdout.puts "Please run jets webpacker:install to install Webpacker"
        exit!
      end

      def detect_port!
        server = TCPServer.new(@hostname, @port)
        server.close

      rescue Errno::EADDRINUSE
        $stdout.puts "Another program is running on port #{@port}. Set a new port in #{@config_file} for dev_server"
        exit!
      end

      def execute_cmd
        env = { "NODE_PATH" => @node_modules_path.shellescape }
        cmd = [
          "#{@node_modules_path}/.bin/webpack-dev-server",
          "--config", @webpack_config
        ]
        cmd += ["--progress", "--color"] if @pretty

        Dir.chdir(@app_path) do
          exec env, *cmd
        end
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
webpacker-jets-3.2.101 lib/webpacker/dev_server_runner.rb
webpacker-jets-3.2.100 lib/webpacker/dev_server_runner.rb