Class: ShopifyCLI::Resources::EnvFile

Inherits:
Object
  • Object
show all
Includes:
SmartProperties
Defined in:
lib/shopify_cli/resources/env_file.rb

Constant Summary collapse

FILENAME =
".env"
KEY_MAP =
{
  "SHOPIFY_API_KEY" => :api_key,
  "SHOPIFY_API_SECRET" => :secret,
  "SHOP" => :shop,
  "SCOPES" => :scopes,
  "HOST" => :host,
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.env_input(parsed_source, overrides: {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/shopify_cli/resources/env_file.rb', line 52

def env_input(parsed_source, overrides: {})
  env_details = {}
  extra = {}
  parsed_source.merge(overrides).each do |key, value|
    if KEY_MAP[key]
      env_details[KEY_MAP[key]] = value
    else
      extra[key] = value
    end
  end
  env_details[:extra] = extra
  env_details
end

.from_hash(hash) ⇒ Object



26
27
28
# File 'lib/shopify_cli/resources/env_file.rb', line 26

def from_hash(hash)
  new(env_input(hash))
end

.parse(directory) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/shopify_cli/resources/env_file.rb', line 30

def parse(directory)
  File.read(path(directory))
    .gsub("\r\n", "\n").split("\n").each_with_object({}) do |line, output|
    match = /\A(#*\s*[A-Za-z_0-9]+)\s*=\s*(.*)\z/.match(line)
    if match
      key = match[1]
      output[key] = case match[2]
      # Remove single quotes
      when /\A'(.*)'\z/ then match[2]
      # Remove double quotes and unescape string preserving newline characters
      when /\A"(.*)"\z/ then match[2].gsub('\n', "\n").gsub(/\\(.)/, '\1')
      else match[2]
      end
    end
    output
  end
end

.parse_external_env(directory = Dir.pwd, overrides: {}) ⇒ Object



48
49
50
# File 'lib/shopify_cli/resources/env_file.rb', line 48

def parse_external_env(directory = Dir.pwd, overrides: {})
  env_input(parse(directory), overrides: overrides)
end

.path(directory) ⇒ Object



17
18
19
# File 'lib/shopify_cli/resources/env_file.rb', line 17

def path(directory)
  File.join(directory, FILENAME)
end

.read(_directory = Dir.pwd, overrides: {}) ⇒ Object



21
22
23
24
# File 'lib/shopify_cli/resources/env_file.rb', line 21

def read(_directory = Dir.pwd, overrides: {})
  input = parse_external_env(overrides: overrides)
  new(input)
end

Instance Method Details

#to_hObject



74
75
76
77
78
79
80
81
82
83
# File 'lib/shopify_cli/resources/env_file.rb', line 74

def to_h
  out = {}
  KEY_MAP.each do |key, value|
    out[key] = send(value).to_s if send(value)
  end
  extra.each do |key, value|
    out[key] = value.to_s
  end
  out
end

#update(ctx, field, value) ⇒ Object



102
103
104
105
# File 'lib/shopify_cli/resources/env_file.rb', line 102

def update(ctx, field, value)
  self[field] = value
  write(ctx)
end

#write(ctx) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/shopify_cli/resources/env_file.rb', line 85

def write(ctx)
  spin_group = CLI::UI::SpinGroup.new
  spin_group.add(ctx.message("core.env_file.saving_header", FILENAME)) do |spinner|
    output = []
    KEY_MAP.each do |key, value|
      output << "#{key}=#{send(value)}" if send(value)
    end
    extra.each do |key, value|
      output << "#{key}=#{value}"
    end
    ctx.print_task(ctx.message("core.env_file.saving", FILENAME))
    ctx.write(FILENAME, output.join("\n") + "\n")
    spinner.update_title(ctx.message("core.env_file.saved", FILENAME))
  end
  spin_group.wait
end