forked from shakacode/shakapacker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev_server.rb
104 lines (82 loc) · 1.78 KB
/
dev_server.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
class Shakapacker::DevServer
DEFAULT_ENV_PREFIX = "SHAKAPACKER_DEV_SERVER".freeze
# Configure dev server connection timeout (in seconds), default: 0.1
# Shakapacker.dev_server.connect_timeout = 1
cattr_accessor(:connect_timeout) { 0.1 }
attr_reader :config
def initialize(config)
@config = config
end
def running?
if config.dev_server.present?
Socket.tcp(host, port, connect_timeout: connect_timeout).close
true
else
false
end
rescue
false
end
def host
fetch(:host)
end
def port
fetch(:port)
end
def https?
case fetch(:https)
when true, "true", Hash
true
else
false
end
end
def server
server_value = fetch(:server)
server_type = server_value.is_a?(Hash) ? server_value[:type] : server_value
return server_type if ["http", "https"].include?(server_type)
return "http" if server_type.nil?
puts <<~MSG
WARNING:
`server: #{server_type}` is not a valid configuration in Shakapacker.
Falling back to default `server: http`.
MSG
"http"
rescue KeyError
"http"
end
def protocol
return "https" if server == "https" || https? == true
"http"
end
def host_with_port
"#{host}:#{port}"
end
def pretty?
fetch(:pretty)
end
def hmr?
fetch(:hmr)
end
def inline_css?
case fetch(:inline_css)
when false, "false"
false
else
true
end
end
def env_prefix
config.dev_server.fetch(:env_prefix, DEFAULT_ENV_PREFIX)
end
private
def fetch(key)
return nil unless config.dev_server.present?
ENV["#{env_prefix}_#{key.upcase}"] || config.dev_server.fetch(key, defaults[key])
rescue
nil
end
def defaults
config.send(:defaults)[:dev_server] || {}
end
end