Module: ReactNativeUtil::Util

Included in:
CLI, Converter
Defined in:
lib/react_native_util/util.rb

Overview

Module with utility methods

Instance Method Summary collapse

Instance Method Details

#boolean_env_var?(var, default_value: false) ⇒ Boolean

Return a Boolean value associated with an environment variable.



57
58
59
60
61
62
# File 'lib/react_native_util/util.rb', line 57

def boolean_env_var?(var, default_value: false)
  value = ENV[var.to_s]
  return default_value if value.nil?

  /^(y|t)/i.match? value
end

#execute(*command, chdir: nil, output: STDOUT, log: STDOUT) ⇒ Object

Execute the specified command. If output is non-nil, generate a log at that location. Main log (open) is log.

Raises:

  • ExecutionError If the command fails



41
42
43
44
45
46
47
48
49
50
# File 'lib/react_native_util/util.rb', line 41

def execute(*command, chdir: nil, output: STDOUT, log: STDOUT)
  log.log_command command unless log.nil?

  options = chdir.nil? ? {} : { chdir: chdir }
  system(*command, options.merge(i[err out] => output))

  raise ExecutionError, "#{command.shelljoin}: #{$?}" unless $?.success?

  nil
end

#float_env_var(var, default_value: 0) ⇒ Float

Return a Float value associated with an environment variable.



69
70
71
72
73
74
# File 'lib/react_native_util/util.rb', line 69

def float_env_var(var, default_value: 0)
  value = ENV[var.to_s]
  return default_value.to_f if value.nil?

  value.to_f
end

#log(message) ⇒ Object

Wrapper for STDOUT.log



90
91
92
# File 'lib/react_native_util/util.rb', line 90

def log(message)
  STDOUT.log message
end

#mac?Boolean

Convenience method to determine if running on a Mac.



84
85
86
# File 'lib/react_native_util/util.rb', line 84

def mac?
  platform.mac?
end

#platformObject

TTY::Platform

Object with platform information



77
78
79
# File 'lib/react_native_util/util.rb', line 77

def platform
  @platform ||= TTY::Platform.new
end

#run_command_with_spinner!(*command, log: nil, chdir: nil) ⇒ Object

Executes a command with no output to the terminal. A spinner is displayed instead. Output may be directed to a file.

Raises:

  • ExecutionError on failure



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/react_native_util/util.rb', line 16

def run_command_with_spinner!(*command, log: nil, chdir: nil)
  STDOUT.flush
  STDERR.flush
  spinner = TTY::Spinner.new "[:spinner] #{command.shelljoin}", format: :flip
  spinner.auto_spin
  start_time = Time.now
  execute(*command, log: nil, output: log, chdir: chdir)
  elapsed = Time.now - start_time
  spinner.success "success in #{format('%.1f', elapsed)} s"
rescue ExecutionError
  elapsed = Time.now - start_time
  spinner.error "failure in #{format('%.1f', elapsed)} s"
  STDOUT.log "See #{log} for details." if log && log.kind_of?(String)
  raise
end