Skip to content

Commit

Permalink
Provide a root to assets object (#136)
Browse files Browse the repository at this point in the history
This makes it easier to use separate assets objects for different sets of compiled assets at different paths.
  • Loading branch information
timriley committed Feb 5, 2024
1 parent 6602bdc commit 98dfc52
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 33 deletions.
18 changes: 11 additions & 7 deletions lib/hanami/assets.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "json"
require "pathname"
require "zeitwerk"

module Hanami
Expand Down Expand Up @@ -37,10 +38,15 @@ def self.gem_loader
# @since 2.1.0
attr_reader :config

# @api private
# @since 2.1.0
attr_reader :root

# @api public
# @since 2.1.0
def initialize(config:)
def initialize(config:, root:)
@config = config
@root = Pathname(root)
end

# Returns the asset at the given path.
Expand Down Expand Up @@ -92,15 +98,13 @@ def crossorigin?(source_path)
def manifest
return @manifest if instance_variable_defined?(:@manifest)

unless config.manifest_path
raise ConfigError, "no manifest_path configured"
end
full_manifest_path = root.join(config.manifest_path)

unless File.exist?(config.manifest_path)
raise ManifestMissingError.new(config.manifest_path)
unless full_manifest_path.exist?
raise ManifestMissingError.new(full_manifest_path.to_s)
end

@manifest = JSON.parse(File.read(config.manifest_path))
@manifest = JSON.parse(File.read(full_manifest_path))
end
end
end
14 changes: 7 additions & 7 deletions lib/hanami/assets/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ class Config
BASE_URL = ""
private_constant :BASE_URL

# @!attribute [rw] manifest_path
# @return [String, nil]
#
# @api public
# @since 2.1.0
setting :manifest_path, default: "assets.json"

# @!attribute [rw] package_manager_run_command
# @return [String]
#
Expand Down Expand Up @@ -51,13 +58,6 @@ class Config
# @since 2.1.0
setting :base_url, constructor: -> url { BaseUrl.new(url.to_s) }

# @!attribute [rw] manifest_path
# @return [String, nil]
#
# @api public
# @since 2.1.0
setting :manifest_path

# @api public
# @since 2.1.0
def initialize(**values)
Expand Down
7 changes: 0 additions & 7 deletions lib/hanami/assets/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ class Assets
class Error < ::StandardError
end

# Error raised when assets config is not valid.
#
# @api public
# @since 2.1.0
class ConfigError < Error
end

# Error returned when the assets manifest file is missing.
#
# @api public
Expand Down
22 changes: 10 additions & 12 deletions spec/integration/hanami/assets/manifest_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
require "tmpdir"

RSpec.describe "manifest handling" do
subject(:assets) { Hanami::Assets.new(config: Hanami::Assets::Config.new(**config_kwargs)) }
subject(:assets) {
Hanami::Assets.new(
config: Hanami::Assets::Config.new(**config_kwargs),
root: root,
)
}

let(:config_kwargs) { {} }

context "manifest_path configured and real file exists" do
let(:config_kwargs) { {manifest_path: File.join(@dir, "manifest.json")} }
let(:root) { @dir }

before do
@dir = Dir.mktmpdir
File.write(File.join(@dir, "manifest.json"), <<~JSON)
File.write(File.join(@dir, "assets.json"), <<~JSON)
{
"app.js": {"url": "/path/to/app.js"}
}
Expand All @@ -33,19 +38,12 @@
end
end

context "manifest_path not configured" do
it "raises a ConfigError" do
expect { assets["app.js"] }
.to raise_error Hanami::Assets::ConfigError, "no manifest_path configured"
end
end

context "no file at configured manifest_path" do
let(:config_kwargs) { {manifest_path: "/missing/manifest.json"} }
let(:root) { "/missing/dir" }

it "raises a ManifestMissingError" do
expect { assets["app.js"] }
.to raise_error Hanami::Assets::ManifestMissingError, %r{/missing/manifest.json}
.to raise_error Hanami::Assets::ManifestMissingError, %r{/missing/dir/assets.json}
end
end
end

0 comments on commit 98dfc52

Please sign in to comment.