Sha256: 29b0c50bad2adc0b929fafa2c130c2956469836c6018cc232a4321e6e050fabb

Contents?: true

Size: 1.85 KB

Versions: 5

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true
require 'English'

module SewingKit
  module Webpack
    class Dev
      class NodeSewingKitNotInstalled < StandardError
        def initialize
          super(
            "sewing-kit is not installed.  " \
            "Try `yarn add @shopify/sewing-kit`"
          )
        end
      end

      attr_accessor :pid

      def start
        @pid = spawn
        at_exit { handle_exit }
        detach
      end

      private

      def spawn
        Kernel.spawn(
          {
            'NODE_ENV' => 'development',
            'BLUEBIRD_DEBUG' => '0',
            'BLUEBIRD_LONG_STACK_TRACES' => '0',
          },
          command,
          chdir: Rails.root.to_s,
          out: $stdout,
          err: $stderr,
        ) || exit(1)
      end

      def handle_exit
        return if $ERROR_INFO.nil?
        Process.kill 'SIGTERM', pid
      rescue Errno::ESRCH
        nil
      end

      def detach
        Process.detach pid
      end

      def command
        command_list = [
          sewing_kit_bin,
          'dev',
          '--logLevel',
          log_level,
        ]

        if debug_mode?
          command_list.push(['--debug'])
        end

        command_list.join(' ')
      end

      def sewing_kit_bin
        bin = SewingKit.configuration.dev_server_sewing_kit_bin
        raise NodeSewingKitNotInstalled unless File.exist?(bin)
        bin
      end

      def log_level
        if SewingKit.configuration.log_level == :inherit
          log_level_from_rails
        else
          SewingKit.configuration.log_level.to_s
        end
      end

      def log_level_from_rails
        case Rails.logger.level
        when 0
          :debug
        when 1
          :info
        when 2
          :warn
        else
          :error
        end
      end

      def debug_mode?
        !ENV['SK_DEBUG'].nil?
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
sewing_kit-0.95.4 lib/sewing_kit/webpack/dev.rb
sewing_kit-0.95.3 lib/sewing_kit/webpack/dev.rb
sewing_kit-0.95.2 lib/sewing_kit/webpack/dev.rb
sewing_kit-0.95.1 lib/sewing_kit/webpack/dev.rb
sewing_kit-0.95.0 lib/sewing_kit/webpack/dev.rb