catalyst-rails 0.1.3 → 2.0.0.beta1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b10f332beb55d01493af5d076e6db9f8cad440f78438fb6af530507c48fc8f2c
4
- data.tar.gz: eedec183e3725404d1762651bf557fec94997c8928696386918a9be7a1206ce6
3
+ metadata.gz: f6dbbb378b752078b95029b38eb8b05001cfa37e5bd64a3c32a38f9db3c6bc68
4
+ data.tar.gz: c6de8f3c01f813d3f7eeea77801d99ce1b44af567c2b9c570eebc370c7d24e56
5
5
  SHA512:
6
- metadata.gz: bf1f3d0e3f6ef1faac21de4adc1025940010cac4c3e8add9d74c3e40f4b4644a264fa9e683d256149d22b7aedb877266afeb816ce217915462790b37d28d9a68
7
- data.tar.gz: c924c91d114ffc003bd51f988ec0bac4697f7db462642bad63f00b87fed4879702fb13bf416f06f3e9228a2bd6a785cd93488a485902e807c3ff14998eb949f0
6
+ metadata.gz: eda47f5e2b159cb08563b13052993506c52fa04f7ff4a573d099970941af356ef2c13e05d4019c7e5527dfaf08c4b71daf0ed1c67f6d583736ab296459648510
7
+ data.tar.gz: 4d9bd288e5017a91f7217819f7d8aa60acc5ebb982e0dfc8d159ff08381bb615ffd7674c4a2e23a64dddd7b48934f75846fd12a77a59391b829d3a5fea165db1
@@ -7,32 +7,55 @@ require 'open3'
7
7
  module Catalyst
8
8
  extend Dry::Configurable
9
9
 
10
- default_environment = if ENV['NODE_ENV']
11
- ENV['NODE_ENV'].to_sym
12
- elsif defined? Rails
13
- Rails.env.to_sym
10
+ def self.default_environment
11
+ if ENV['NODE_ENV']
12
+ ENV['NODE_ENV'].to_sym
13
+ elsif defined?(Rails)
14
+ Rails.env.to_sym
15
+ end
14
16
  end
15
17
 
16
- default_manifest_path = if defined? Rails
17
- File.expand_path('./public/assets/manifest.json', Dir.pwd)
18
+ def self.default_manifest_path
19
+ return unless defined?(Rails)
20
+
21
+ File.expand_path('./public/assets/catalyst.manifest.json', Dir.pwd)
18
22
  end
19
23
 
20
- setting :environment, default_environment
21
- setting :manifest_path, default_manifest_path
22
- setting :assets_host, ENV.fetch('HOST') { nil }
23
- setting :assets_host_protocol, 'https'
24
- setting :dev_server_host, ENV.fetch('DEV_SERVER_HOST') { 'localhost' }
25
- setting :dev_server_port, ENV.fetch('DEV_SERVER_PORT') { 8080 }.to_i
26
- setting :running_feature_tests, -> {
27
- !defined?(RSpec) || RSpec.world.all_example_groups.any? do |group|
28
- group.metadata[:type] == :system
24
+ def self.default_assets_host
25
+ if defined?(Rails) && !Rails.env.production? && ENV['PORT']
26
+ "localhost:#{ENV['PORT']}"
27
+ else
28
+ ENV.fetch('HOST', nil)
29
29
  end
30
- }
30
+ end
31
31
 
32
- def self.log(message, level = :info)
33
- message = message.split("\n").reduce('') do |reduction, line|
34
- reduction + "\e[35m[Catalyst]\e[0m #{line}\n"
35
- end
32
+ def self.default_assets_host_protocol
33
+ !defined?(Rails) || Rails.env.production? ? 'https' : 'http'
34
+ end
35
+
36
+ setting :pwd, Dir.pwd
37
+ setting :environment, default_environment
38
+ setting :manifest_path, default_manifest_path
39
+ setting :assets_host, default_assets_host
40
+ setting :assets_host_protocol, default_assets_host_protocol
41
+ setting :dev_server_host, ENV.fetch('DEV_SERVER_HOST', 'localhost')
42
+ setting :dev_server_port, ENV.fetch('DEV_SERVER_PORT', 8080).to_i
43
+ setting :dev_server_protocol, ENV.fetch('DEV_SERVER_PROTOCOL', 'http')
44
+ setting :running_feature_tests,
45
+ lambda {
46
+ !defined?(RSpec) || RSpec
47
+ .world
48
+ .all_example_groups
49
+ .any? { |group| group.metadata[:type] == :system }
50
+ }
51
+
52
+ def self.log(message)
53
+ message =
54
+ message
55
+ .split("\n")
56
+ .reduce('') do |reduction, line|
57
+ reduction + "\e[35m[Catalyst]\e[0m #{line}\n"
58
+ end
36
59
 
37
60
  puts message
38
61
  end
@@ -70,12 +93,10 @@ module Catalyst
70
93
  $catalyst_server_pid = wait_thr.pid
71
94
 
72
95
  Thread.new do
73
- begin
74
- while line = stdout.gets
75
- puts line
76
- end
77
- rescue IOError
96
+ while (line = stdout.gets)
97
+ puts line
78
98
  end
99
+ rescue IOError
79
100
  end
80
101
 
81
102
  at_exit do
@@ -86,25 +107,26 @@ module Catalyst
86
107
  end
87
108
 
88
109
  def self.check_for_yarn!
89
- unless system 'which yarn > /dev/null 2>&1'
90
- raise NotInstalled, <<~MESSAGE
91
- The yarn binary is not available in this directory.
92
- Please follow the instructions here to install it:
93
- https://yarnpkg.com/lang/en/docs/install
94
- MESSAGE
95
- end
110
+ raise NotInstalled, <<~MESSAGE unless system 'which yarn > /dev/null 2>&1'
111
+ The yarn binary is not available in this directory.
112
+ Please follow the instructions here to install it:
113
+ https://yarnpkg.com/lang/en/docs/install
114
+ MESSAGE
96
115
  end
97
116
 
98
117
  def self.check_for_catalyst!
99
118
  check_for_yarn!
100
119
 
101
- unless File.exist?(File.join(Dir.pwd, 'node_modules/catalyst/lib/index.js'))
102
- raise NotInstalled, <<~MESSAGE
103
- The catalyst binary is not available in this directory.
104
- Please follow the instructions here to install it:
105
- https://github.com/friendsoftheweb/catalyst
106
- MESSAGE
120
+ if File.exist?(File.join(Dir.pwd, 'node_modules/catalyst/lib/bin.js'))
121
+ return
107
122
  end
123
+
124
+ raise NotInstalled, <<~MESSAGE
125
+ The Catalyst binary is not available in this directory or you are using an unsupported version of Catalyst.
126
+
127
+ Please follow the instructions here to install it:
128
+ https://github.com/friendsoftheweb/catalyst
129
+ MESSAGE
108
130
  end
109
131
  end
110
132
 
@@ -113,4 +135,4 @@ require_relative './catalyst/errors'
113
135
  require_relative './catalyst/builder'
114
136
  require_relative './catalyst/helpers'
115
137
  require_relative './catalyst/manifest'
116
- require_relative './catalyst/railtie' if defined? ::Rails::Railtie
138
+ require_relative './catalyst/railtie' if defined?(::Rails::Railtie)
@@ -3,6 +3,7 @@
3
3
 
4
4
  require 'singleton'
5
5
  require 'forwardable'
6
+ require 'fileutils'
6
7
  require_relative './config'
7
8
 
8
9
  module Catalyst
@@ -17,18 +18,20 @@ module Catalyst
17
18
  end
18
19
 
19
20
  def build!(environment = nil)
20
- Catalyst.check_for_catalyst!
21
-
22
21
  environment ||= Catalyst.config.environment
23
22
 
24
- case environment
25
- when :test
26
- test_build!
27
- when :production
28
- production_build!
29
- else
30
- raise ArgumentError,
31
- 'Invalid environment. Must be one of: :test, :production.'
23
+ FileUtils.cd(Catalyst.config.pwd) do
24
+ Catalyst.check_for_catalyst!
25
+
26
+ case environment
27
+ when :test
28
+ test_build!
29
+ when :production
30
+ production_build!
31
+ else
32
+ raise ArgumentError,
33
+ 'Invalid environment. Must be one of: :test, :production.'
34
+ end
32
35
  end
33
36
  end
34
37
 
@@ -70,24 +73,26 @@ module Catalyst
70
73
  end
71
74
 
72
75
  def production_build!
73
- unless system("NODE_ENV=production #{BUILD_COMMAND}")
74
- Catalyst.log('Failed to compile assets!')
76
+ return if system("NODE_ENV=production #{BUILD_COMMAND}")
75
77
 
76
- exit 1
77
- end
78
+ Catalyst.log('Failed to compile assets!')
79
+
80
+ exit 1
78
81
  end
79
82
 
80
83
  def assets_last_modified
81
- asset_paths.lazy.select do |path|
82
- File.exists?(path)
83
- end.map do |path|
84
- File.ctime(path)
85
- end.max || Time.now
84
+ asset_paths
85
+ .lazy
86
+ .select { |path| File.exist?(path) }
87
+ .map { |path| File.ctime(path) }
88
+ .max || Time.now
86
89
  end
87
90
 
88
91
  def asset_paths
89
92
  if ::Catalyst::Config.context_path
90
- Dir.glob(File.join(::Catalyst::Config.context_path, '**/*.{js,ts,tsx,scss}')) + [
93
+ Dir.glob(
94
+ File.join(::Catalyst::Config.context_path, '**/*.{js,ts,tsx,scss}')
95
+ ) + [
91
96
  File.join(Dir.pwd, 'package.json'),
92
97
  File.join(Dir.pwd, 'yarn.lock'),
93
98
  File.join(Dir.pwd, 'catalyst.config.json')
@@ -98,16 +103,18 @@ module Catalyst
98
103
  end
99
104
 
100
105
  def assets_last_built_file_path
101
- if defined?(Rails)
102
- Rails.root.join('tmp/assets-last-built')
103
- end
106
+ Rails.root.join('tmp/assets-last-built') if defined?(Rails)
104
107
  end
105
108
 
106
109
  def assets_last_built
107
110
  if assets_last_built_file_path.nil?
108
111
  Time.at(0)
109
112
  else
110
- File.mtime(assets_last_built_file_path) rescue Time.at(0)
113
+ begin
114
+ File.mtime(assets_last_built_file_path)
115
+ rescue StandardError
116
+ Time.at(0)
117
+ end
111
118
  end
112
119
  end
113
120
  end
@@ -1,6 +1,7 @@
1
- # typed: true
1
+ # typed: strict
2
2
  # frozen_string_literal: true
3
3
 
4
+ require 'sorbet-runtime'
4
5
  require 'singleton'
5
6
  require 'forwardable'
6
7
  require 'json'
@@ -8,8 +9,16 @@ require_relative './errors'
8
9
 
9
10
  module Catalyst
10
11
  class Config
11
- CATALYST_CONFIG_PATH = File.expand_path('./catalyst.config.json', Dir.pwd)
12
- PACKAGE_PATH = File.expand_path('./package.json', Dir.pwd)
12
+ extend T::Sig
13
+ sig { returns(String) }
14
+ def self.catalyst_config_path
15
+ File.expand_path('./catalyst.config.json', Catalyst.config.pwd)
16
+ end
17
+
18
+ sig { returns(String) }
19
+ def self.package_path
20
+ File.expand_path('./package.json', Catalyst.config.pwd)
21
+ end
13
22
 
14
23
  include Singleton
15
24
 
@@ -18,27 +27,36 @@ module Catalyst
18
27
  def_delegators :instance, :context_path
19
28
  end
20
29
 
30
+ sig { void }
21
31
  def initialize
22
- @values = if File.exists?(CATALYST_CONFIG_PATH)
23
- JSON.parse(File.read(CATALYST_CONFIG_PATH))
24
- elsif File.exists?(PACKAGE_PATH)
25
- JSON.parse(File.read(PACKAGE_PATH))['catalyst']
26
- else
27
- raise ::Catalyst::MissingConfig,
28
- "Missing 'catalyst.config.json' or 'package.json' file in: #{Dir.pwd}"
29
- end
30
-
31
- if @values.nil?
32
- raise ::Catalyst::MissingConfig, <<~MESSAGE
33
- Missing "catalyst" config in package.json file.
34
- Please follow the instructions here to set up Catalyst:
35
- https://github.com/friendsoftheweb/catalyst
36
- MESSAGE
37
- end
32
+ @values =
33
+ T.let(
34
+ if File.exist?(self.class.catalyst_config_path)
35
+ JSON.parse(File.read(self.class.catalyst_config_path))
36
+ elsif File.exist?(self.class.package_path)
37
+ JSON.parse(File.read(self.class.package_path))['catalyst']
38
+ else
39
+ raise ::Catalyst::MissingConfig,
40
+ "Missing 'catalyst.config.json' or 'package.json' file in: #{
41
+ Catalyst.config.pwd
42
+ }"
43
+ end,
44
+ T::Hash[String, T.untyped]
45
+ )
46
+
47
+ raise ::Catalyst::MissingConfig, <<~MESSAGE if @values.nil?
48
+ Missing "catalyst" config in package.json file.
49
+ Please follow the instructions here to set up Catalyst:
50
+ https://github.com/friendsoftheweb/catalyst
51
+ MESSAGE
38
52
  end
39
53
 
54
+ sig { returns(String) }
40
55
  def context_path
41
- File.join(Dir.pwd, @values['contextPath'] || @values['rootPath'])
56
+ File.join(
57
+ Catalyst.config.pwd,
58
+ @values['contextPath'] || @values['rootPath']
59
+ )
42
60
  end
43
61
  end
44
62
  end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ # typed: strict
4
+
5
+ require 'sorbet-runtime'
6
+
7
+ module Catalyst
8
+ module ContentType
9
+ extend T::Sig
10
+
11
+ sig { params(filename: String).returns(Symbol) }
12
+ def self.for_filename(filename)
13
+ case File.extname(filename)
14
+ when /\.(js)\z/
15
+ :script
16
+ when /\.(css)\z/
17
+ :style
18
+ when /\.(png|jpe?g|gif|webp)\z/
19
+ :image
20
+ when /\.(woff2?|ttf|eot)\z/
21
+ :font
22
+ when /\.(mp4|webm)\z/
23
+ :video
24
+ else
25
+ raise StandardError,
26
+ "Could not automatically determine the content type for: #{
27
+ filename
28
+ }"
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,16 +1,21 @@
1
- # typed: false
1
+ # typed: true
2
2
  # frozen_string_literal: true
3
3
 
4
+ require 'sorbet-runtime'
4
5
  require 'action_view'
5
6
  require_relative './manifest'
7
+ require_relative './content_type'
6
8
 
7
9
  module Catalyst
8
10
  module Helpers
11
+ extend T::Sig
12
+ include Kernel
9
13
  include ActionView::Helpers::TagHelper
10
14
  include ActionView::Helpers::OutputSafetyHelper
11
15
 
16
+ sig { params(path: T.any(String, Symbol), common: T::Boolean).returns(String) }
12
17
  def catalyst_javascript_include_tag(path, common: true)
13
- path = path.to_s.sub(/\.js\z/, '') + '.js'
18
+ path = "#{path.to_s.sub(/\.js\z/, '')}.js"
14
19
 
15
20
  if catalyst_referenced_files.include?(path)
16
21
  raise ::Catalyst::Manifest::DuplicateAssetReference,
@@ -19,29 +24,34 @@ module Catalyst
19
24
 
20
25
  catalyst_referenced_files << path
21
26
 
22
- if !common
23
- return content_tag(
24
- :script,
25
- nil,
26
- type: 'text/javascript',
27
- crossorigin: 'anonymous',
28
- src: ::Catalyst::Manifest[path]
27
+ unless common
28
+ return(
29
+ content_tag(
30
+ :script,
31
+ nil,
32
+ type: 'text/javascript',
33
+ crossorigin: 'anonymous',
34
+ src: catalyst_asset_url(path)
35
+ )
29
36
  )
30
37
  end
31
38
 
32
- safe_join([
33
- catalyst_javascript_vendor_include_tag,
34
- catalyst_javascript_common_include_tag,
35
- content_tag(
36
- :script,
37
- nil,
38
- type: 'text/javascript',
39
- crossorigin: 'anonymous',
40
- src: ::Catalyst::Manifest[path]
41
- )
42
- ])
39
+ safe_join(
40
+ [
41
+ catalyst_javascript_vendor_include_tag,
42
+ catalyst_javascript_common_include_tag,
43
+ content_tag(
44
+ :script,
45
+ nil,
46
+ type: 'text/javascript',
47
+ crossorigin: 'anonymous',
48
+ src: catalyst_asset_url(path)
49
+ )
50
+ ]
51
+ )
43
52
  end
44
53
 
54
+ sig { returns(T.nilable(String)) }
45
55
  def catalyst_javascript_vendor_include_tag
46
56
  path = 'vendor-dll.js'
47
57
 
@@ -51,20 +61,21 @@ module Catalyst
51
61
  catalyst_javascript_include_tag(path)
52
62
  end
53
63
 
64
+ sig { returns(T.nilable(String)) }
54
65
  def catalyst_javascript_common_include_tag
55
66
  path = 'common.js'
56
67
 
57
- return nil if catalyst_referenced_files.include?(path)
68
+ return if catalyst_referenced_files.include?(path)
69
+ return unless ::Catalyst.development? || ::Catalyst::Manifest.has?(path)
58
70
 
59
- if ::Catalyst.development? || ::Catalyst::Manifest.has?(path)
60
- catalyst_javascript_include_tag(path)
61
- end
71
+ catalyst_javascript_include_tag(path)
62
72
  end
63
73
 
74
+ sig { params(path: T.any(String, Symbol)).returns(T.nilable(String)) }
64
75
  def catalyst_stylesheet_link_tag(path)
65
76
  return nil if ::Catalyst.development?
66
77
 
67
- path = path.to_s.sub(/\.css\z/, '') + '.css'
78
+ path = "#{path.to_s.sub(/\.css\z/, '')}.css"
68
79
 
69
80
  if catalyst_referenced_files.include?(path)
70
81
  raise ::Catalyst::Manifest::DuplicateAssetReference,
@@ -73,37 +84,116 @@ module Catalyst
73
84
 
74
85
  catalyst_referenced_files << path
75
86
 
76
- safe_join([
77
- catalyst_common_stylesheet_link_tag,
78
- content_tag(
79
- :link,
80
- nil,
81
- href: ::Catalyst::Manifest[path],
82
- media: 'screen',
83
- rel: 'stylesheet'
84
- )
85
- ])
87
+ safe_join(
88
+ [
89
+ catalyst_common_stylesheet_link_tag,
90
+ content_tag(
91
+ :link,
92
+ nil,
93
+ href: catalyst_asset_url(path),
94
+ media: 'screen',
95
+ rel: 'stylesheet'
96
+ )
97
+ ]
98
+ )
86
99
  end
87
100
 
101
+ sig { returns(T.nilable(String)) }
88
102
  def catalyst_common_stylesheet_link_tag
89
103
  path = 'common.css'
90
104
 
91
105
  return nil if catalyst_referenced_files.include?(path)
92
106
 
93
- if ::Catalyst::Manifest.has?(path)
94
- catalyst_stylesheet_link_tag(path)
95
- end
107
+ catalyst_stylesheet_link_tag(path) if ::Catalyst::Manifest.has?(path)
108
+ end
109
+
110
+ sig { params(entry_name: T.any(String, Symbol)).returns(T.untyped) }
111
+ def catalyst_link_tags_for(entry_name)
112
+ safe_join(
113
+ [
114
+ catalyst_preload_link_tags_for(entry_name),
115
+ catalyst_prefetch_link_tags_for(entry_name)
116
+ ].flatten
117
+ )
118
+ end
119
+
120
+ sig { params(entry_name: T.any(String, Symbol)).returns(T.untyped) }
121
+ def catalyst_preload_link_tags_for(entry_name)
122
+ safe_join(
123
+ ::Catalyst::Manifest
124
+ .preload_urls_for(entry_name)
125
+ .map do |url|
126
+ content_tag(
127
+ :link,
128
+ nil,
129
+ {
130
+ href: catalyst_asset_url(url),
131
+ rel: 'preload',
132
+ as: ::Catalyst::ContentType.for_filename(url)
133
+ }
134
+ )
135
+ end
136
+ )
137
+ end
138
+
139
+ sig { params(entry_name: T.any(String, Symbol)).returns(T.untyped) }
140
+ def catalyst_prefetch_link_tags_for(entry_name)
141
+ safe_join(
142
+ ::Catalyst::Manifest
143
+ .prefetch_urls_for(entry_name)
144
+ .map do |url|
145
+ content_tag(
146
+ :link,
147
+ nil,
148
+ {
149
+ href: catalyst_asset_url(url),
150
+ rel: 'prefetch',
151
+ as: ::Catalyst::ContentType.for_filename(url)
152
+ }
153
+ )
154
+ end
155
+ )
156
+ end
157
+
158
+ sig { params(path: String).returns(T.untyped) }
159
+ def catalyst_preload_link_tag(path)
160
+ content_tag(
161
+ :link,
162
+ nil,
163
+ {
164
+ href: catalyst_asset_url(path),
165
+ rel: 'preload',
166
+ as: ::Catalyst::ContentType.for_filename(path)
167
+ }
168
+ )
169
+ end
170
+
171
+ sig { params(path: String).returns(T.untyped) }
172
+ def catalyst_prefetch_link_tag(path)
173
+ content_tag(
174
+ :link,
175
+ nil,
176
+ {
177
+ href: catalyst_asset_url(path),
178
+ rel: 'prefetch',
179
+ as: ::Catalyst::ContentType.for_filename(path)
180
+ }
181
+ )
96
182
  end
97
183
 
184
+ sig { params(path: T.any(String, Symbol)).returns(T.nilable(String)) }
98
185
  def catalyst_asset_path(path)
99
186
  ::Catalyst::Manifest[path]
100
187
  end
101
188
 
189
+ sig { params(path: T.any(String, Symbol)).returns(T.nilable(String)) }
102
190
  def catalyst_asset_url(path)
103
191
  if ::Catalyst.development? || ::Catalyst.config.assets_host.nil?
104
192
  catalyst_asset_path(path)
105
193
  else
106
- "#{Catalyst.config.assets_host_protocol}://#{Catalyst.config.assets_host}#{catalyst_asset_path(path)}"
194
+ "#{Catalyst.config.assets_host_protocol}://#{
195
+ Catalyst.config.assets_host
196
+ }#{catalyst_asset_path(path)}"
107
197
  end
108
198
  end
109
199
 
@@ -1,4 +1,4 @@
1
- # typed: true
1
+ # typed: strict
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'singleton'
@@ -11,58 +11,112 @@ module Catalyst
11
11
  AssetMissing = Class.new(::Catalyst::CatalystError)
12
12
  DuplicateAssetReference = Class.new(::Catalyst::CatalystError)
13
13
 
14
+ extend T::Sig
14
15
  include Singleton
15
16
 
16
17
  class << self
17
18
  extend Forwardable
18
- def_delegators :instance, :[], :has?
19
+
20
+ def_delegators :instance,
21
+ :[],
22
+ :has?,
23
+ :preload_urls_for,
24
+ :prefetch_urls_for
19
25
  end
20
26
 
27
+ sig { void }
21
28
  def initialize
22
29
  if Catalyst.development?
23
- @manifest = {}
30
+ @manifest = T.let({}, T::Hash[String, T.untyped])
24
31
  else
25
32
  if Catalyst.config.manifest_path.nil?
26
33
  raise 'Missing "manifest_path" configuration.'
27
34
  end
28
35
 
29
- @manifest = JSON.parse(File.read(Catalyst.config.manifest_path))
36
+ @manifest =
37
+ T.let(
38
+ JSON.parse(File.read(Catalyst.config.manifest_path)),
39
+ T::Hash[String, T.untyped]
40
+ )
30
41
  end
31
42
  end
32
43
 
44
+ sig { params(path: T.any(String, Symbol)).returns(T::Boolean) }
33
45
  def has?(path)
34
- path = path.to_s.gsub(/\A\/+/, '')
46
+ path = path.to_s.gsub(%r{\A/+}, '')
35
47
 
36
- if Catalyst.development?
37
- false
38
- else
39
- @manifest.key?(path)
40
- end
48
+ Catalyst.development? ? false : assets.key?(path)
41
49
  end
42
50
 
51
+ sig { params(path: T.any(String, Symbol)).returns(String) }
43
52
  def [](path)
44
- path = path.to_s.gsub(/\A\/+/, '')
53
+ path = path.to_s.gsub(%r{\A/+}, '')
45
54
 
46
55
  if Catalyst.development?
56
+ dev_server_protocol = Catalyst.config.dev_server_protocol
47
57
  dev_server_host = Catalyst.config.dev_server_host
48
58
  dev_server_port = Catalyst.config.dev_server_port
49
59
 
60
+ if dev_server_protocol.nil?
61
+ raise ::Catalyst::CatalystError,
62
+ 'Missing "dev_server_protocol" configuration.'
63
+ end
64
+
50
65
  if dev_server_host.nil?
51
- raise 'Missing "dev_server_host" configuration.'
66
+ raise ::Catalyst::CatalystError,
67
+ 'Missing "dev_server_host" configuration.'
52
68
  end
53
69
 
54
70
  if dev_server_port.nil?
55
- raise 'Missing "dev_server_port" configuration.'
71
+ raise ::Catalyst::CatalystError,
72
+ 'Missing "dev_server_port" configuration.'
56
73
  end
57
74
 
58
- return "http://#{dev_server_host}:#{dev_server_port}/#{path}"
75
+ "#{dev_server_protocol}://#{dev_server_host}:#{dev_server_port}/#{path}"
76
+ elsif assets.key?(path)
77
+ T.must(assets[path])
59
78
  else
60
- if @manifest.key?(path)
61
- return @manifest[path]
62
- else
63
- raise AssetMissing, "Couldn't find an asset for path: #{path}"
64
- end
79
+ raise AssetMissing, "Couldn't find an asset for path: #{path}"
65
80
  end
66
81
  end
82
+
83
+ sig { params(entry_name: T.any(String, Symbol)).returns(T::Array[String]) }
84
+ def preload_urls_for(entry_name)
85
+ return [] if Catalyst.development?
86
+
87
+ entry_name = entry_name.to_s
88
+
89
+ return [] unless preload.key?(entry_name)
90
+
91
+ T.must(preload[entry_name])
92
+ end
93
+
94
+ sig { params(entry_name: T.any(String, Symbol)).returns(T::Array[String]) }
95
+ def prefetch_urls_for(entry_name)
96
+ return [] if Catalyst.development?
97
+
98
+ entry_name = entry_name.to_s
99
+
100
+ return [] unless prefetch.key?(entry_name)
101
+
102
+ T.must(prefetch[entry_name])
103
+ end
104
+
105
+ private
106
+
107
+ sig { returns(T::Hash[String, String]) }
108
+ def assets
109
+ @manifest['assets'] || {}
110
+ end
111
+
112
+ sig { returns(T::Hash[String, T::Array[String]]) }
113
+ def preload
114
+ @manifest['preload'] || {}
115
+ end
116
+
117
+ sig { returns(T::Hash[String, T::Array[String]]) }
118
+ def prefetch
119
+ @manifest['prefetch'] || {}
120
+ end
67
121
  end
68
122
  end
@@ -10,8 +10,6 @@ module Catalyst
10
10
  ActionView::Base.include(::Catalyst::Helpers)
11
11
  end
12
12
 
13
- rake_tasks do
14
- load File.expand_path('./tasks/build.rake', __dir__)
15
- end
13
+ rake_tasks { load File.expand_path('./tasks/build.rake', __dir__) }
16
14
  end
17
15
  end
@@ -1,12 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  namespace :catalyst do
2
4
  desc 'Build assets with Catalyst'
3
5
  task :build do
4
- if File.exists?('./public/assets/manifest.json')
6
+ if File.exist?('./public/assets/manifest.json')
5
7
  Catalyst.log('Removing previous assets...')
6
8
 
7
9
  manifest = JSON.parse(File.read('./public/assets/manifest.json'))
8
10
 
9
- manifest.values.each do |asset_path|
11
+ manifest.each_value do |asset_path|
10
12
  system "rm -f ./public/assets/#{asset_path}*"
11
13
  end
12
14
  end
@@ -3,6 +3,6 @@
3
3
 
4
4
  module Catalyst
5
5
  def self.version
6
- Gem::Version.new('0.1.3')
6
+ Gem::Version.new('2.0.0.beta1')
7
7
  end
8
8
  end
metadata CHANGED
@@ -1,15 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: catalyst-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 2.0.0.beta1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Martens
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-08-14 00:00:00.000000000 Z
11
+ date: 2020-01-04 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actionview
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '7.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '7.0'
13
33
  - !ruby/object:Gem::Dependency
14
34
  name: dry-configurable
15
35
  requirement: !ruby/object:Gem::Requirement
@@ -25,53 +45,47 @@ dependencies:
25
45
  - !ruby/object:Gem::Version
26
46
  version: '0.7'
27
47
  - !ruby/object:Gem::Dependency
28
- name: actionview
48
+ name: sorbet-runtime
29
49
  requirement: !ruby/object:Gem::Requirement
30
50
  requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '3.0'
34
- - - "<"
51
+ - - "~>"
35
52
  - !ruby/object:Gem::Version
36
- version: '7.0'
53
+ version: 0.5.0
37
54
  type: :runtime
38
55
  prerelease: false
39
56
  version_requirements: !ruby/object:Gem::Requirement
40
57
  requirements:
41
- - - ">="
42
- - !ruby/object:Gem::Version
43
- version: '3.0'
44
- - - "<"
58
+ - - "~>"
45
59
  - !ruby/object:Gem::Version
46
- version: '7.0'
60
+ version: 0.5.0
47
61
  - !ruby/object:Gem::Dependency
48
- name: sorbet-runtime
62
+ name: rubocop
49
63
  requirement: !ruby/object:Gem::Requirement
50
64
  requirements:
51
65
  - - "~>"
52
66
  - !ruby/object:Gem::Version
53
- version: 0.4.0
54
- type: :runtime
67
+ version: '1.7'
68
+ type: :development
55
69
  prerelease: false
56
70
  version_requirements: !ruby/object:Gem::Requirement
57
71
  requirements:
58
72
  - - "~>"
59
73
  - !ruby/object:Gem::Version
60
- version: 0.4.0
74
+ version: '1.7'
61
75
  - !ruby/object:Gem::Dependency
62
76
  name: sorbet
63
77
  requirement: !ruby/object:Gem::Requirement
64
78
  requirements:
65
79
  - - "~>"
66
80
  - !ruby/object:Gem::Version
67
- version: 0.4.0
81
+ version: 0.5.0
68
82
  type: :development
69
83
  prerelease: false
70
84
  version_requirements: !ruby/object:Gem::Requirement
71
85
  requirements:
72
86
  - - "~>"
73
87
  - !ruby/object:Gem::Version
74
- version: 0.4.0
88
+ version: 0.5.0
75
89
  description:
76
90
  email: dan@friendsoftheweb.com
77
91
  executables: []
@@ -81,6 +95,7 @@ files:
81
95
  - lib/catalyst-rails.rb
82
96
  - lib/catalyst/builder.rb
83
97
  - lib/catalyst/config.rb
98
+ - lib/catalyst/content_type.rb
84
99
  - lib/catalyst/errors.rb
85
100
  - lib/catalyst/helpers.rb
86
101
  - lib/catalyst/manifest.rb
@@ -99,15 +114,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
99
114
  requirements:
100
115
  - - ">="
101
116
  - !ruby/object:Gem::Version
102
- version: '0'
117
+ version: 2.5.0
103
118
  required_rubygems_version: !ruby/object:Gem::Requirement
104
119
  requirements:
105
- - - ">="
120
+ - - ">"
106
121
  - !ruby/object:Gem::Version
107
- version: '0'
122
+ version: 1.3.1
108
123
  requirements: []
109
- rubyforge_project:
110
- rubygems_version: 2.7.6.2
124
+ rubygems_version: 3.1.2
111
125
  signing_key:
112
126
  specification_version: 4
113
127
  summary: Ruby helpers for the "catalyst" node package