Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Block directories in theme assets #1934

Merged
merged 6 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/project_types/theme/messages/messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ module Messages
error: {
address_binding_error: "Couldn't bind to localhost."\
" To serve your theme, set a different address with {{command:%s theme serve --host=<address>}}",
invalid_subdirectory: <<~MESSAGE,
The presence of %s in the directory structure isn't supported.

Move any files to a parent folder, then delete unsupported subdirectories.

• Required directory structure: https://shopify.dev/themes/architecture#directory-structure-and-component-types
MESSAGE
},
serving: <<~SERVING,

Expand Down
16 changes: 12 additions & 4 deletions lib/shopify_cli/theme/theme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ def initialize(ctx, root: nil, id: nil, name: nil, role: nil)
end

def theme_files
glob(["**/*.liquid", "**/*.json", "assets/*"]).uniq
(glob(["**/*.liquid", "**/*.json"]) + static_asset_files).uniq
end

def static_asset_files
glob("assets/*").reject(&:liquid?)
glob("assets/*", raise_on_dir: true).reject(&:liquid?)
end

def liquid_files
Expand All @@ -36,8 +36,11 @@ def json_files
glob("**/*.json")
end

def glob(pattern)
root.glob(pattern).map { |path| File.new(path, root) }
def glob(pattern, raise_on_dir: false)
root.glob(pattern).map do |path|
abort_if_directory!(path) if raise_on_dir
File.new(path, root)
end
end

def theme_file?(file)
Expand Down Expand Up @@ -218,6 +221,11 @@ def load_info_from_api

self
end

def abort_if_directory!(path)
return unless ::File.directory?(path)
@ctx.abort(@ctx.message("theme.serve.error.invalid_subdirectory", path.to_s))
end
end
end
end
12 changes: 12 additions & 0 deletions test/shopify-cli/theme/theme_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ def setup
@theme = Theme.new(@ctx, root: @root, id: "123")
end

def test_static_assets_with_subdirectory
root = @root + "_with_subdirectories"
ctx = TestHelpers::FakeContext.new(root: root)
theme = Theme.new(ctx, root: root, id: "123")

io = capture_io_and_assert_raises(ShopifyCLI::Abort) do
theme.static_asset_paths
end
filepath = root + "/assets/assets_subdir"
assert_message_output(io: io, expected_content: ctx.message("theme.serve.error.invalid_subdirectory", filepath))
end

def test_static_assets
assert_includes(@theme.static_asset_paths, Pathname.new("assets/theme.css"))
end
Expand Down