Class: ShopifyCLI::Theme::File

Inherits:
Struct
  • Object
show all
Defined in:
lib/shopify_cli/theme/file.rb

Defined Under Namespace

Classes: JsonTemplateNormalizer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, root) ⇒ File

Returns a new instance of File.



10
11
12
13
14
15
16
17
# File 'lib/shopify_cli/theme/file.rb', line 10

def initialize(path, root)
  super(Pathname.new(path))

  # Path may be relative or absolute depending on the source.
  # By converting both the path and the root to absolute paths, we
  # can safely fetch a relative path.
  @relative_path = self.path.expand_path.relative_path_from(root.expand_path)
end

Instance Attribute Details

#pathObject

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path



6
7
8
# File 'lib/shopify_cli/theme/file.rb', line 6

def path
  @path
end

#remote_checksumObject

Returns the value of attribute remote_checksum.



7
8
9
# File 'lib/shopify_cli/theme/file.rb', line 7

def remote_checksum
  @remote_checksum
end

#warningsObject



108
109
110
# File 'lib/shopify_cli/theme/file.rb', line 108

def warnings
  @warnings || []
end

Instance Method Details

#==(other) ⇒ Object

Make it possible to check whether a given File is within a list of Files with ‘include?`, some of which may be relative paths while others are absolute paths.



92
93
94
# File 'lib/shopify_cli/theme/file.rb', line 92

def ==(other)
  relative_path == other.relative_path
end

#absolute_pathObject



100
101
102
# File 'lib/shopify_cli/theme/file.rb', line 100

def absolute_path
  path.realpath.to_s
end

#checksumObject



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/shopify_cli/theme/file.rb', line 77

def checksum
  content = read
  if mime_type.json?
    # 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

#deleteObject



45
46
47
# File 'lib/shopify_cli/theme/file.rb', line 45

def delete
  path.delete
end

#exist?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/shopify_cli/theme/file.rb', line 49

def exist?
  path.exist?
end

#json?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/shopify_cli/theme/file.rb', line 69

def json?
  path.extname == ".json"
end

#liquid?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/shopify_cli/theme/file.rb', line 61

def liquid?
  path.extname == ".liquid"
end

#liquid_css?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/shopify_cli/theme/file.rb', line 65

def liquid_css?
  relative_path.end_with?(".css.liquid")
end

#mime_typeObject



53
54
55
# File 'lib/shopify_cli/theme/file.rb', line 53

def mime_type
  @mime_type ||= MimeType.by_filename(@relative_path)
end

#name(*args) ⇒ Object



96
97
98
# File 'lib/shopify_cli/theme/file.rb', line 96

def name(*args)
  ::File.basename(path, *args)
end

#readObject



19
20
21
22
23
24
25
# File 'lib/shopify_cli/theme/file.rb', line 19

def read
  if text?
    path.read(universal_newline: true)
  else
    path.read(mode: "rb")
  end
end

#relative_pathObject



104
105
106
# File 'lib/shopify_cli/theme/file.rb', line 104

def relative_path
  @relative_path.to_s
end

#template?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/shopify_cli/theme/file.rb', line 73

def template?
  relative_path.start_with?("templates/")
end

#text?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/shopify_cli/theme/file.rb', line 57

def text?
  mime_type.text?
end

#write(content) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/shopify_cli/theme/file.rb', line 27

def write(content)
  path.parent.mkpath unless path.parent.directory?
  if text?
    path.write(content, universal_newline: true)
  else
    path.write(content, 0, mode: "wb")
  end
rescue Encoding::UndefinedConversionError
  ##
  # The CLI tries to write the file and normalize EOL characters to avoid
  # errors on Windows when files are shared across different operational systems.
  #
  # The CLI fallbacks any error during the conversion by writing the file
  # in binary mode when the normalization fails (e.g., ASCII files), so no data is lost.
  #
  path.write(content, 0, mode: "wb")
end