Skip to content

Commit

Permalink
[3.63] Avoid loop when users pass the --theme-editor-sync flag in t…
Browse files Browse the repository at this point in the history
…he `shopify theme dev` command
  • Loading branch information
karreiro committed Jul 1, 2024
1 parent 808a156 commit 3c02813
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 53 deletions.
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

0 comments on commit 3c02813

Please sign in to comment.