Module: ShopifyCLI::Environment

Defined in:
lib/shopify_cli/environment.rb

Overview

The environment module provides an interface to get information from the environment in which the CLI runs

Constant Summary collapse

TRUTHY_ENV_VARIABLE_VALUES =
["1", "true", "TRUE", "yes", "YES"]
SPIN_OVERRIDE_ENV_NAMES =
[
  Constants::EnvironmentVariables::SPIN_WORKSPACE,
  Constants::EnvironmentVariables::SPIN_NAMESPACE,
  Constants::EnvironmentVariables::SPIN_HOST,
]
THEME_ACCESS_PASSWORD_PREFIX =
"shptka_"

Class Method Summary collapse

Class Method Details

.acceptance_test?(env_variables: ENV) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
# File 'lib/shopify_cli/environment.rb', line 87

def self.acceptance_test?(env_variables: ENV)
  env_variable_truthy?(
    Constants::EnvironmentVariables::ACCEPTANCE_TEST,
    env_variables: env_variables
  )
end

.admin_auth_token(env_variables: ENV) ⇒ Object



171
172
173
# File 'lib/shopify_cli/environment.rb', line 171

def self.admin_auth_token(env_variables: ENV)
  env_variables[Constants::EnvironmentVariables::ADMIN_AUTH_TOKEN]
end

.auth_token(env_variables: ENV) ⇒ Object



167
168
169
# File 'lib/shopify_cli/environment.rb', line 167

def self.auth_token(env_variables: ENV)
  env_variables[Constants::EnvironmentVariables::AUTH_TOKEN]
end

.development?(env_variables: ENV) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
# File 'lib/shopify_cli/environment.rb', line 59

def self.development?(env_variables: ENV)
  env_variable_truthy?(
    Constants::EnvironmentVariables::DEVELOPMENT,
    env_variables: env_variables
  )
end

.env_variable_truthy?(variable_name, env_variables: ENV) ⇒ Boolean

Returns:

  • (Boolean)


183
184
185
# File 'lib/shopify_cli/environment.rb', line 183

def self.env_variable_truthy?(variable_name, env_variables: ENV)
  TRUTHY_ENV_VARIABLE_VALUES.include?(env_variables[variable_name.to_s])
end

.interactive=(interactive) ⇒ Object



44
45
46
# File 'lib/shopify_cli/environment.rb', line 44

def self.interactive=(interactive)
  @interactive = interactive
end

.interactive?(env_variables: ENV) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
# File 'lib/shopify_cli/environment.rb', line 48

def self.interactive?(env_variables: ENV)
  if env_variables.key?(Constants::EnvironmentVariables::TTY)
    env_variable_truthy?(
      Constants::EnvironmentVariables::TTY,
      env_variables: env_variables
    )
  else
    @interactive ||= STDIN.tty?
  end
end

.node_version(context: Context.new) ⇒ Object

Raises:



23
24
25
26
27
28
# File 'lib/shopify_cli/environment.rb', line 23

def self.node_version(context: Context.new)
  output, status = context.capture2e("node", "--version")
  raise ShopifyCLI::Abort, context.message("core.errors.missing_node") unless status.success?
  version = output.match(/v(\d+\.\d+\.\d+)/)[1]
  ::Semantic::Version.new(version)
end

.npm_version(context: Context.new) ⇒ Object

Raises:



30
31
32
33
34
35
# File 'lib/shopify_cli/environment.rb', line 30

def self.npm_version(context: Context.new)
  output, status = context.capture2e("npm", "--version")
  raise ShopifyCLI::Abort, context.message("core.errors.missing_npm") unless status.success?
  version = output.match(/(\d+\.\d+\.\d+)/)[1]
  ::Semantic::Version.new(version)
end

.partners_domain(env_variables: ENV) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/shopify_cli/environment.rb', line 101

def self.partners_domain(env_variables: ENV)
  if use_local_partners_instance?(env_variables: env_variables)
    "partners.myshopify.io"
  elsif use_spin?(env_variables: env_variables)
    "partners.#{spin_url(env_variables: env_variables)}"
  else
    "partners.shopify.com"
  end
end

Returns:

  • (Boolean)


94
95
96
97
98
99
# File 'lib/shopify_cli/environment.rb', line 94

def self.print_backtrace?(env_variables: ENV)
  env_variable_truthy?(
    Constants::EnvironmentVariables::BACKTRACE,
    env_variables: env_variables
  )
end

Returns:

  • (Boolean)


73
74
75
76
77
78
# File 'lib/shopify_cli/environment.rb', line 73

def self.print_stacktrace?(env_variables: ENV)
  env_variable_truthy?(
    Constants::EnvironmentVariables::STACKTRACE,
    env_variables: env_variables
  ) || development?(env_variables: env_variables)
end

.rails_version(context: Context.new) ⇒ Object



37
38
39
40
41
42
# File 'lib/shopify_cli/environment.rb', line 37

def self.rails_version(context: Context.new)
  output, status = context.capture2e("rails", "--version")
  context.abort(context.message("core.app.create.rails.error.install_failure", "rails")) unless status.success?
  version = output.match(/Rails (\d+\.\d+\.\d+)/)[1]
  ::Semantic::Version.new(version)
end

.ruby_version(context: Context.new) ⇒ Object

Raises:



16
17
18
19
20
21
# File 'lib/shopify_cli/environment.rb', line 16

def self.ruby_version(context: Context.new)
  output, status = context.capture2e("ruby", "--version")
  raise ShopifyCLI::Abort, context.message("core.errors.missing_ruby") unless status.success?
  version = output.match(/ruby (\d+\.\d+\.\d+)/)[1]
  ::Semantic::Version.new(version)
end

.run_as_subprocess?(env_variables: ENV) ⇒ Boolean

Returns:

  • (Boolean)


187
188
189
190
191
192
# File 'lib/shopify_cli/environment.rb', line 187

def self.run_as_subprocess?(env_variables: ENV)
  env_variable_truthy?(
    Constants::EnvironmentVariables::RUN_AS_SUBPROCESS,
    env_variables: env_variables
  )
end

.send_monorail_events?(env_variables: ENV) ⇒ Boolean

Returns:

  • (Boolean)


160
161
162
163
164
165
# File 'lib/shopify_cli/environment.rb', line 160

def self.send_monorail_events?(env_variables: ENV)
  env_variable_truthy?(
    Constants::EnvironmentVariables::MONORAIL_REAL_EVENTS,
    env_variables: env_variables
  ) && !run_as_subprocess?(env_variables: env_variables)
end

.spin_show(latest: false) ⇒ Object



156
157
158
# File 'lib/shopify_cli/environment.rb', line 156

def self.spin_show(latest: false)
  latest ? %x(spin show --latest --json) : %x(spin show --json)
end

.spin_url(env_variables: ENV) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/shopify_cli/environment.rb', line 135

def self.spin_url(env_variables: ENV)
  override = spin_url_override(env_variables: env_variables)
  return override unless override.nil?

  spin_response = if env_variables.key?(
    Constants::EnvironmentVariables::SPIN_INSTANCE
  )
    spin_show
  else
    spin_show(latest: true)
  end

  begin
    instance = JSON.parse(spin_response)
    raise "Missing key 'fqdn' from spin show. Actual response: #{instance}" unless instance.include?("fqdn")
    instance["fqdn"]
  rescue => e
    raise "Failed to infer spin environment from spin show response #{spin_response}: #{e}"
  end
end

.spin_url_override(env_variables: ENV) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/shopify_cli/environment.rb', line 111

def self.spin_url_override(env_variables: ENV)
  tokens = SPIN_OVERRIDE_ENV_NAMES.map do |name|
    env_variables[name]
  end

  return if tokens.all?(&:nil?)

  if tokens.any?(&:nil?)
    raise "To manually target a spin instance, you must set #{SPIN_OVERRIDE_ENV_NAMES}"
  else
    tokens.join(".")
  end
end

.store(env_variables: ENV) ⇒ Object



179
180
181
# File 'lib/shopify_cli/environment.rb', line 179

def self.store(env_variables: ENV)
  env_variables[Constants::EnvironmentVariables::STORE]
end

.storefront_renderer_auth_token(env_variables: ENV) ⇒ Object



175
176
177
# File 'lib/shopify_cli/environment.rb', line 175

def self.storefront_renderer_auth_token(env_variables: ENV)
  env_variables[Constants::EnvironmentVariables::STOREFRONT_RENDERER_AUTH_TOKEN]
end

.test?(env_variables: ENV) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
# File 'lib/shopify_cli/environment.rb', line 80

def self.test?(env_variables: ENV)
  env_variable_truthy?(
    Constants::EnvironmentVariables::TEST,
    env_variables: env_variables
  )
end

.theme_access_password?Boolean

Returns:

  • (Boolean)


194
195
196
# File 'lib/shopify_cli/environment.rb', line 194

def self.theme_access_password?
  admin_auth_token&.start_with?(THEME_ACCESS_PASSWORD_PREFIX)
end

.use_local_partners_instance?(env_variables: ENV) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
# File 'lib/shopify_cli/environment.rb', line 66

def self.use_local_partners_instance?(env_variables: ENV)
  env_variable_truthy?(
    Constants::EnvironmentVariables::LOCAL_PARTNERS,
    env_variables: env_variables
  )
end

.use_spin?(env_variables: ENV) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
128
129
130
131
132
133
# File 'lib/shopify_cli/environment.rb', line 125

def self.use_spin?(env_variables: ENV)
  env_variable_truthy?(
    Constants::EnvironmentVariables::SPIN,
    env_variables: env_variables
  ) || env_variable_truthy?(
    Constants::EnvironmentVariables::SPIN_PARTNERS,
    env_variables: env_variables
  )
end