catalyst-rails 0.0.9 → 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: a0d65247f97e19b2d6535248e8396850700204b4e0d607148532ef90097c5c2d
4
- data.tar.gz: 4bbb24b41713f1027ca20c2b0bd9f7aa23e8043bcfb12d13597dc2735bcc7d1f
3
+ metadata.gz: f6dbbb378b752078b95029b38eb8b05001cfa37e5bd64a3c32a38f9db3c6bc68
4
+ data.tar.gz: c6de8f3c01f813d3f7eeea77801d99ce1b44af567c2b9c570eebc370c7d24e56
5
5
  SHA512:
6
- metadata.gz: 7aaef005bfa1705c4e801425f0be77f0235aee7bc9fa1d81affbeb722b33880ee299473775261fb7981500a42cfaf7053b22f104b4a8b9c88b233cde44acbf12
7
- data.tar.gz: c22f77a41573433610b10c4884c51bf17586cd7723e47dc613299f31f3121e72cdfbe780d9277e7b56c2fa2765f712acbd7672a83aaa3cbfbba7ecfe6149a55c
6
+ metadata.gz: eda47f5e2b159cb08563b13052993506c52fa04f7ff4a573d099970941af356ef2c13e05d4019c7e5527dfaf08c4b71daf0ed1c67f6d583736ab296459648510
7
+ data.tar.gz: 4d9bd288e5017a91f7217819f7d8aa60acc5ebb982e0dfc8d159ff08381bb615ffd7674c4a2e23a64dddd7b48934f75846fd12a77a59391b829d3a5fea165db1
@@ -1,3 +1,4 @@
1
+ # typed: false
1
2
  # frozen_string_literal: true
2
3
 
3
4
  require 'dry-configurable'
@@ -6,33 +7,55 @@ require 'open3'
6
7
  module Catalyst
7
8
  extend Dry::Configurable
8
9
 
9
- default_environment = if ENV['NODE_ENV']
10
- ENV['NODE_ENV'].to_sym
11
- elsif defined? Rails
12
- 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
13
16
  end
14
17
 
15
- default_manifest_path = if defined? Rails
16
- 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)
17
22
  end
18
23
 
19
- setting :environment, default_environment
20
- setting :manifest_path, default_manifest_path
21
- setting :assets_base_path, '/assets'
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 system 'yarn run which catalyst > /dev/null 2>&1'
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
138
+ require_relative './catalyst/railtie' if defined?(::Rails::Railtie)
@@ -1,7 +1,9 @@
1
+ # typed: false
1
2
  # frozen_string_literal: true
2
3
 
3
4
  require 'singleton'
4
5
  require 'forwardable'
6
+ require 'fileutils'
5
7
  require_relative './config'
6
8
 
7
9
  module Catalyst
@@ -16,18 +18,20 @@ module Catalyst
16
18
  end
17
19
 
18
20
  def build!(environment = nil)
19
- Catalyst.check_for_catalyst!
20
-
21
21
  environment ||= Catalyst.config.environment
22
22
 
23
- case environment
24
- when :test
25
- test_build!
26
- when :production
27
- production_build!
28
- else
29
- raise ArgumentError,
30
- '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
31
35
  end
32
36
  end
33
37
 
@@ -69,21 +73,29 @@ module Catalyst
69
73
  end
70
74
 
71
75
  def production_build!
72
- unless system("NODE_ENV=production #{BUILD_COMMAND}")
73
- Catalyst.log('Failed to compile assets!')
76
+ return if system("NODE_ENV=production #{BUILD_COMMAND}")
74
77
 
75
- exit 1
76
- end
78
+ Catalyst.log('Failed to compile assets!')
79
+
80
+ exit 1
77
81
  end
78
82
 
79
83
  def assets_last_modified
80
- asset_paths.lazy.map { |path| File.ctime(path) }.max || Time.now
84
+ asset_paths
85
+ .lazy
86
+ .select { |path| File.exist?(path) }
87
+ .map { |path| File.ctime(path) }
88
+ .max || Time.now
81
89
  end
82
90
 
83
91
  def asset_paths
84
- if ::Catalyst::Config.root_path
85
- Dir.glob(File.join(::Catalyst::Config.root_path, '**/*.{js,ts,tsx,scss}')) + [
86
- File.join(Dir.pwd, 'yarn.lock')
92
+ if ::Catalyst::Config.context_path
93
+ Dir.glob(
94
+ File.join(::Catalyst::Config.context_path, '**/*.{js,ts,tsx,scss}')
95
+ ) + [
96
+ File.join(Dir.pwd, 'package.json'),
97
+ File.join(Dir.pwd, 'yarn.lock'),
98
+ File.join(Dir.pwd, 'catalyst.config.json')
87
99
  ]
88
100
  else
89
101
  []
@@ -91,16 +103,18 @@ module Catalyst
91
103
  end
92
104
 
93
105
  def assets_last_built_file_path
94
- if defined?(Rails)
95
- Rails.root.join('tmp/assets-last-built')
96
- end
106
+ Rails.root.join('tmp/assets-last-built') if defined?(Rails)
97
107
  end
98
108
 
99
109
  def assets_last_built
100
110
  if assets_last_built_file_path.nil?
101
111
  Time.at(0)
102
112
  else
103
- 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
104
118
  end
105
119
  end
106
120
  end
@@ -1,5 +1,7 @@
1
+ # typed: strict
1
2
  # frozen_string_literal: true
2
3
 
4
+ require 'sorbet-runtime'
3
5
  require 'singleton'
4
6
  require 'forwardable'
5
7
  require 'json'
@@ -7,38 +9,54 @@ require_relative './errors'
7
9
 
8
10
  module Catalyst
9
11
  class Config
10
- 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
11
22
 
12
23
  include Singleton
13
24
 
14
25
  class << self
15
26
  extend Forwardable
16
- def_delegators :instance, :root_path, :build_path
27
+ def_delegators :instance, :context_path
17
28
  end
18
29
 
30
+ sig { void }
19
31
  def initialize
20
- unless File.exists?(PACKAGE_PATH)
21
- raise ::Catalyst::MissingConfig,
22
- "Missing package.json file in: #{Dir.pwd}"
23
- end
24
-
25
- @values = JSON.parse(File.read(PACKAGE_PATH))['catalyst']
26
-
27
- if @values.nil?
28
- raise ::Catalyst::MissingConfig, <<~MESSAGE
29
- Missing "catalyst" config in package.json file.
30
- Please follow the instructions here to set up Catalyst:
31
- https://github.com/friendsoftheweb/catalyst
32
- MESSAGE
33
- end
34
- end
35
-
36
- def root_path
37
- File.join(Dir.pwd, @values['rootPath'])
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
 
40
- def build_path
41
- File.join(Dir.pwd, @values['buildPath'])
54
+ sig { returns(String) }
55
+ def context_path
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,3 +1,4 @@
1
+ # typed: strong
1
2
  # frozen_string_literal: true
2
3
 
3
4
  module Catalyst
@@ -1,15 +1,21 @@
1
+ # typed: true
1
2
  # frozen_string_literal: true
2
3
 
4
+ require 'sorbet-runtime'
3
5
  require 'action_view'
4
6
  require_relative './manifest'
7
+ require_relative './content_type'
5
8
 
6
9
  module Catalyst
7
10
  module Helpers
11
+ extend T::Sig
12
+ include Kernel
8
13
  include ActionView::Helpers::TagHelper
9
14
  include ActionView::Helpers::OutputSafetyHelper
10
15
 
16
+ sig { params(path: T.any(String, Symbol), common: T::Boolean).returns(String) }
11
17
  def catalyst_javascript_include_tag(path, common: true)
12
- path = path.to_s.sub(/\.js\z/, '') + '.js'
18
+ path = "#{path.to_s.sub(/\.js\z/, '')}.js"
13
19
 
14
20
  if catalyst_referenced_files.include?(path)
15
21
  raise ::Catalyst::Manifest::DuplicateAssetReference,
@@ -18,29 +24,34 @@ module Catalyst
18
24
 
19
25
  catalyst_referenced_files << path
20
26
 
21
- if !common
22
- return content_tag(
23
- :script,
24
- nil,
25
- type: 'text/javascript',
26
- crossorigin: 'anonymous',
27
- 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
+ )
28
36
  )
29
37
  end
30
38
 
31
- safe_join([
32
- catalyst_javascript_vendor_include_tag,
33
- catalyst_javascript_common_include_tag,
34
- content_tag(
35
- :script,
36
- nil,
37
- type: 'text/javascript',
38
- crossorigin: 'anonymous',
39
- src: ::Catalyst::Manifest[path]
40
- )
41
- ])
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
+ )
42
52
  end
43
53
 
54
+ sig { returns(T.nilable(String)) }
44
55
  def catalyst_javascript_vendor_include_tag
45
56
  path = 'vendor-dll.js'
46
57
 
@@ -50,20 +61,21 @@ module Catalyst
50
61
  catalyst_javascript_include_tag(path)
51
62
  end
52
63
 
64
+ sig { returns(T.nilable(String)) }
53
65
  def catalyst_javascript_common_include_tag
54
66
  path = 'common.js'
55
67
 
56
- 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)
57
70
 
58
- if ::Catalyst.development? || ::Catalyst::Manifest.has?(path)
59
- catalyst_javascript_include_tag(path)
60
- end
71
+ catalyst_javascript_include_tag(path)
61
72
  end
62
73
 
74
+ sig { params(path: T.any(String, Symbol)).returns(T.nilable(String)) }
63
75
  def catalyst_stylesheet_link_tag(path)
64
76
  return nil if ::Catalyst.development?
65
77
 
66
- path = path.to_s.sub(/\.css\z/, '') + '.css'
78
+ path = "#{path.to_s.sub(/\.css\z/, '')}.css"
67
79
 
68
80
  if catalyst_referenced_files.include?(path)
69
81
  raise ::Catalyst::Manifest::DuplicateAssetReference,
@@ -72,37 +84,116 @@ module Catalyst
72
84
 
73
85
  catalyst_referenced_files << path
74
86
 
75
- safe_join([
76
- catalyst_common_stylesheet_link_tag,
77
- content_tag(
78
- :link,
79
- nil,
80
- href: ::Catalyst::Manifest[path],
81
- media: 'screen',
82
- rel: 'stylesheet'
83
- )
84
- ])
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
+ )
85
99
  end
86
100
 
101
+ sig { returns(T.nilable(String)) }
87
102
  def catalyst_common_stylesheet_link_tag
88
103
  path = 'common.css'
89
104
 
90
105
  return nil if catalyst_referenced_files.include?(path)
91
106
 
92
- if ::Catalyst::Manifest.has?(path)
93
- catalyst_stylesheet_link_tag(path)
94
- 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
+ )
95
182
  end
96
183
 
184
+ sig { params(path: T.any(String, Symbol)).returns(T.nilable(String)) }
97
185
  def catalyst_asset_path(path)
98
186
  ::Catalyst::Manifest[path]
99
187
  end
100
188
 
189
+ sig { params(path: T.any(String, Symbol)).returns(T.nilable(String)) }
101
190
  def catalyst_asset_url(path)
102
191
  if ::Catalyst.development? || ::Catalyst.config.assets_host.nil?
103
192
  catalyst_asset_path(path)
104
193
  else
105
- "#{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)}"
106
197
  end
107
198
  end
108
199
 
@@ -1,3 +1,4 @@
1
+ # typed: strict
1
2
  # frozen_string_literal: true
2
3
 
3
4
  require 'singleton'
@@ -10,64 +11,112 @@ module Catalyst
10
11
  AssetMissing = Class.new(::Catalyst::CatalystError)
11
12
  DuplicateAssetReference = Class.new(::Catalyst::CatalystError)
12
13
 
14
+ extend T::Sig
13
15
  include Singleton
14
16
 
15
17
  class << self
16
18
  extend Forwardable
17
- def_delegators :instance, :[], :has?
19
+
20
+ def_delegators :instance,
21
+ :[],
22
+ :has?,
23
+ :preload_urls_for,
24
+ :prefetch_urls_for
18
25
  end
19
26
 
27
+ sig { void }
20
28
  def initialize
21
29
  if Catalyst.development?
22
- @manifest = {}
30
+ @manifest = T.let({}, T::Hash[String, T.untyped])
23
31
  else
24
32
  if Catalyst.config.manifest_path.nil?
25
33
  raise 'Missing "manifest_path" configuration.'
26
34
  end
27
35
 
28
- @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
+ )
29
41
  end
30
42
  end
31
43
 
44
+ sig { params(path: T.any(String, Symbol)).returns(T::Boolean) }
32
45
  def has?(path)
33
- path = path.to_s.gsub(/\A\/+/, '')
46
+ path = path.to_s.gsub(%r{\A/+}, '')
34
47
 
35
- if Catalyst.development?
36
- false
37
- else
38
- @manifest.key?(path)
39
- end
48
+ Catalyst.development? ? false : assets.key?(path)
40
49
  end
41
50
 
51
+ sig { params(path: T.any(String, Symbol)).returns(String) }
42
52
  def [](path)
43
- path = path.to_s.gsub(/\A\/+/, '')
53
+ path = path.to_s.gsub(%r{\A/+}, '')
44
54
 
45
55
  if Catalyst.development?
56
+ dev_server_protocol = Catalyst.config.dev_server_protocol
46
57
  dev_server_host = Catalyst.config.dev_server_host
47
58
  dev_server_port = Catalyst.config.dev_server_port
48
59
 
60
+ if dev_server_protocol.nil?
61
+ raise ::Catalyst::CatalystError,
62
+ 'Missing "dev_server_protocol" configuration.'
63
+ end
64
+
49
65
  if dev_server_host.nil?
50
- raise 'Missing "dev_server_host" configuration.'
66
+ raise ::Catalyst::CatalystError,
67
+ 'Missing "dev_server_host" configuration.'
51
68
  end
52
69
 
53
70
  if dev_server_port.nil?
54
- raise 'Missing "dev_server_port" configuration.'
71
+ raise ::Catalyst::CatalystError,
72
+ 'Missing "dev_server_port" configuration.'
55
73
  end
56
74
 
57
- 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])
58
78
  else
59
- if @manifest.key?(path)
60
- assets_base_path = Catalyst.config.assets_base_path
79
+ raise AssetMissing, "Couldn't find an asset for path: #{path}"
80
+ end
81
+ end
61
82
 
62
- if assets_base_path.nil?
63
- return @manifest[path]
64
- end
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?
65
86
 
66
- return "#{assets_base_path}/#{@manifest[path]}"
67
- else
68
- raise AssetMissing, "Couldn't find an asset for path: #{path}"
69
- end
70
- end
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'] || {}
71
120
  end
72
121
  end
73
122
  end
@@ -1,5 +1,7 @@
1
+ # typed: ignore
1
2
  # frozen_string_literal: true
2
3
 
4
+ require 'rails'
3
5
  require_relative './helpers'
4
6
 
5
7
  module Catalyst
@@ -8,8 +10,6 @@ module Catalyst
8
10
  ActionView::Base.include(::Catalyst::Helpers)
9
11
  end
10
12
 
11
- rake_tasks do
12
- load File.expand_path('./tasks/build.rake', __dir__)
13
- end
13
+ rake_tasks { load File.expand_path('./tasks/build.rake', __dir__) }
14
14
  end
15
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
@@ -1,7 +1,8 @@
1
+ # typed: true
1
2
  # frozen_string_literal: true
2
3
 
3
4
  module Catalyst
4
5
  def self.version
5
- Gem::Version.new('0.0.9')
6
+ Gem::Version.new('2.0.0.beta1')
6
7
  end
7
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.0.9
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: 2018-04-24 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,25 +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: '6.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
- - - ">="
58
+ - - "~>"
42
59
  - !ruby/object:Gem::Version
43
- version: '3.0'
44
- - - "<="
60
+ version: 0.5.0
61
+ - !ruby/object:Gem::Dependency
62
+ name: rubocop
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.7'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
45
73
  - !ruby/object:Gem::Version
46
- version: '6.0'
74
+ version: '1.7'
75
+ - !ruby/object:Gem::Dependency
76
+ name: sorbet
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: 0.5.0
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: 0.5.0
47
89
  description:
48
90
  email: dan@friendsoftheweb.com
49
91
  executables: []
@@ -53,6 +95,7 @@ files:
53
95
  - lib/catalyst-rails.rb
54
96
  - lib/catalyst/builder.rb
55
97
  - lib/catalyst/config.rb
98
+ - lib/catalyst/content_type.rb
56
99
  - lib/catalyst/errors.rb
57
100
  - lib/catalyst/helpers.rb
58
101
  - lib/catalyst/manifest.rb
@@ -71,15 +114,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
71
114
  requirements:
72
115
  - - ">="
73
116
  - !ruby/object:Gem::Version
74
- version: '0'
117
+ version: 2.5.0
75
118
  required_rubygems_version: !ruby/object:Gem::Requirement
76
119
  requirements:
77
- - - ">="
120
+ - - ">"
78
121
  - !ruby/object:Gem::Version
79
- version: '0'
122
+ version: 1.3.1
80
123
  requirements: []
81
- rubyforge_project:
82
- rubygems_version: 2.7.3
124
+ rubygems_version: 3.1.2
83
125
  signing_key:
84
126
  specification_version: 4
85
127
  summary: Ruby helpers for the "catalyst" node package