File tree 1 file changed +53
-0
lines changed
1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ require 'socket'
4
+ require 'snowpacker/env'
5
+
6
+ module Snowpacker
7
+ module Rails
8
+ class Runner
9
+ attr_reader :config_file
10
+
11
+ def initialize
12
+ @config_file = Snowpacker . config . config_file
13
+ @env = Env . set_env_variables
14
+
15
+ detect_port!
16
+
17
+ rescue Errno ::ENOENT , NoMethodError
18
+ $stdout. puts "Snowpacker configuration not found in #{ Snowpacker . config_location } "
19
+ $stdout. puts "Please run bundle exec rails generate snowpacker to install Snowpacker"
20
+ exit!
21
+ end
22
+
23
+ # Build for production
24
+ def build
25
+ snowpacker_command ( env : :production , cmd : :build )
26
+ end
27
+
28
+ # Serve for development
29
+ def dev
30
+ snowpacker_command ( env : :development , cmd : :dev )
31
+ end
32
+
33
+ private
34
+
35
+ def snowpacker_command ( env : '' , cmd : '' )
36
+ env = ENV [ "NODE_ENV" ] || env
37
+ command = "NODE_ENV=#{ env } yarn run snowpack #{ cmd } --config #{ @config_file } "
38
+ exec ( command )
39
+ end
40
+
41
+ def detect_port!
42
+ hostname = Snowpacker . config . hostname
43
+ port = Snowpacker . config . port
44
+ server = TCPServer . new ( hostname , port )
45
+ server . close
46
+ rescue Errno ::EADDRINUSE
47
+ $stdout. puts "Another program is running on port #{ port } .\n
48
+ Set a new port in #{ Snowpacker . config_path } for devOptions: { port: \" #{ port } \" }"
49
+ exit!
50
+ end
51
+ end
52
+ end
53
+ end
You can’t perform that action at this time.
0 commit comments