# 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 sewing_kit_bin = 'node_modules/.bin/sewing-kit' raise NodeSewingKitNotInstalled unless File.exist?(sewing_kit_bin) Kernel.spawn( { 'NODE_ENV' => 'development', 'BLUEBIRD_DEBUG' => '0', 'BLUEBIRD_LONG_STACK_TRACES' => '0', }, sewing_kit_bin, 'dev', 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 end end end