Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.63] Avoid loop when users pass the --theme-editor-sync flag in the shopify theme dev command #4143

Merged
merged 1 commit into from
Jul 3, 2024
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
5 changes: 5 additions & 0 deletions .changeset/warm-chicken-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/theme': patch
---

Avoid loop when users pass the `--theme-editor-sync` flag in the `shopify theme dev` command
38 changes: 8 additions & 30 deletions packages/cli-kit/assets/cli-ruby/lib/shopify_cli/theme/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,16 @@ def template?

def checksum
content = read
if mime_type.json?

if mime_type.json? && !settings_schema?
# Normalize JSON to match backend
begin
content = normalize_json(content)
rescue JSON::JSONError
# Fallback to using the raw content
end
end

Digest::MD5.hexdigest(content)
end

Expand Down Expand Up @@ -111,41 +113,17 @@ def warnings

private

def normalize_json(content)
parsed = JSON.parse(content)
def settings_schema?
relative_path.end_with?("config/settings_schema.json")
end

if template?
JsonTemplateNormalizer.new.visit_document(parsed)
end
def normalize_json(content)
normalized = JSON.generate(JSON.parse(content))

normalized = JSON.generate(parsed)
# Backend escapes forward slashes
normalized.gsub!(/\//, "\\/")
normalized
end

class JsonTemplateNormalizer
def visit_document(value)
visit_hash(value["sections"])
end

def visit_hash(hash)
return unless hash.is_a?(Hash)
hash.each do |_, value|
visit_value(value)
end
end

def visit_value(value)
return unless value.is_a?(Hash)

# Reinsert settings to force the same ordering as in the backend
settings = value.delete("settings") || {}
value["settings"] = settings

visit_hash(value["blocks"])
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -94,31 +94,20 @@ def test_checksum
assert_equal(Digest::MD5.hexdigest(content), @theme["layout/theme.liquid"].checksum)
end

def test_normalize_json_template_for_checksum
expected_content = <<~EOS
{
"name": "Blog",
"sections": {
"main": {
"type": "main-blog",
"settings": {}
}
},
"order": [
"main"
]
}
def test_do_not_normalize_settings_schema_for_checksum
normalized = <<~EOS.rstrip
[
{
"name": "theme_info",
"theme_name": "Example",
"theme_version": "1.0.0",
"theme_author": "Shopify",
"theme_documentation_url": "https://shopify.com",
"theme_support_url": "https://support.shopify.com/"
}
]
EOS
normalized = JSON.parse(expected_content).to_json
assert_equal(Digest::MD5.hexdigest(normalized), @theme["templates/blog.json"].checksum)
end

def test_normalize_settings_schema_for_checksum
normalized =
"[{\"name\":\"theme_info\",\"theme_name\":\"Example\"," \
"\"theme_version\":\"1.0.0\",\"theme_author\":\"Shopify\"," \
"\"theme_documentation_url\":\"https:\\/\\/shopify.com\"," \
"\"theme_support_url\":\"https:\\/\\/support.shopify.com\\/\"}]"
assert_equal(Digest::MD5.hexdigest(normalized), @theme["config/settings_schema.json"].checksum)
end

Expand Down