File tree Expand file tree Collapse file tree 3 files changed +101
-10
lines changed
Expand file tree Collapse file tree 3 files changed +101
-10
lines changed Original file line number Diff line number Diff line change @@ -5,3 +5,4 @@ module Tailwindcss
55require_relative "tailwindcss/version"
66require_relative "tailwindcss/engine"
77require_relative "tailwindcss/commands"
8+ require_relative "tailwindcss/server_process"
Original file line number Diff line number Diff line change @@ -15,16 +15,7 @@ class Engine < ::Rails::Engine
1515 end
1616
1717 server do
18- tailwind_pid = fork do
19- # To avoid stealing keystrokes from the debug gem's IRB console in the main process (which
20- # needs to be able to read from $stdin), we use `IO.open(..., 'r+')`.
21- IO . popen ( Tailwindcss ::Commands . watch_command , 'r+' ) do |io |
22- IO . copy_stream ( io , $stdout)
23- end
24- end
25- at_exit do
26- Process . kill ( :INT , tailwind_pid )
27- end
18+ ServerProcess . start
2819 end
2920 end
3021end
Original file line number Diff line number Diff line change 1+ module Tailwindcss
2+ class ServerProcess
3+ attr_reader :server , :pid
4+
5+ def self . start
6+ new . start
7+ end
8+
9+ def start
10+ @server = Server . new ( self )
11+ @pid = fork do
12+ monitor_server
13+ exit_hook
14+ # Using IO.popen(command, 'r+') will avoid watch_command read from $stdin.
15+ # If we use system(*command) instead, IRB and Debug can't read from $stdin
16+ # correctly bacause some keystrokes will be taken by watch_command.
17+ IO . popen ( Commands . watch_command , 'r+' ) do |io |
18+ IO . copy_stream ( io , $stdout)
19+ end
20+ end
21+ Process . detach pid
22+
23+ server . monitor_process
24+ server . exit_hook
25+ end
26+
27+ def stop
28+ return if dead?
29+
30+ Process . kill ( :INT , pid )
31+ Process . wait ( pid )
32+ rescue Errno ::ECHILD , Errno ::ESRCH
33+ end
34+
35+ def dead?
36+ Process . wait ( pid , Process ::WNOHANG )
37+ false
38+ rescue Errno ::ECHILD , Errno ::ESRCH
39+ true
40+ end
41+
42+ private
43+
44+ def monitor_server
45+ Thread . new do
46+ loop do
47+ if server . dead?
48+ puts "Tailwind detected server has gone away"
49+ exit
50+ end
51+ sleep 2
52+ end
53+ end
54+ end
55+
56+ def exit_hook
57+ at_exit do
58+ puts "Stopping tailwind..."
59+ server . stop
60+ end
61+ end
62+
63+ class Server
64+ attr_reader :process , :pid
65+
66+ def initialize ( process )
67+ @process = process
68+ @pid = Process . pid
69+ end
70+
71+ def monitor_process
72+ Thread . new do
73+ loop do
74+ if process . dead?
75+ puts "Detected tailwind has gone away, stopping server..."
76+ exit
77+ end
78+ sleep 2
79+ end
80+ end
81+ end
82+
83+ def exit_hook
84+ at_exit do
85+ process . stop
86+ end
87+ end
88+
89+ def dead?
90+ Process . ppid != pid
91+ end
92+
93+ def stop
94+ Process . kill ( :INT , pid )
95+ rescue Errno ::ECHILD , Errno ::ESRCH
96+ end
97+ end
98+ end
99+ end
You can’t perform that action at this time.
0 commit comments