Class: Rails::Worktrees::BrowserCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/worktrees/browser_command.rb

Overview

Opens the current app/worktree in a browser using the local DEV_PORT. rubocop:disable Metrics/ClassLength

Constant Summary collapse

APP_ROOT_ENV_KEY =
'RAILS_WORKTREES_APP_ROOT'.freeze
ENV_FILE_NAME =
'.env'.freeze
FILE_ENCODING =
'UTF-8'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(argv:, io:, env:, cwd:, host_os: RbConfig::CONFIG['host_os']) ⇒ BrowserCommand

Returns a new instance of BrowserCommand.



13
14
15
16
17
18
19
20
21
# File 'lib/rails/worktrees/browser_command.rb', line 13

def initialize(argv:, io:, env:, cwd:, host_os: RbConfig::CONFIG['host_os'])
  @argv = argv.dup
  @stdin = io.fetch(:stdin)
  @stdout = io.fetch(:stdout)
  @stderr = io.fetch(:stderr)
  @env = env
  @cwd = cwd
  @host_os = host_os
end

Instance Method Details

#runObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rails/worktrees/browser_command.rb', line 23

def run
  meta_command_result = handle_meta_command
  return meta_command_result unless meta_command_result.nil?
  return usage_error if @argv.length > 1

  url = build_url(@argv.first)
  open_browser(url)
  @stdout.puts("🌐 Opening #{url}")
  0
rescue Error => e
  @stderr.puts("Error: #{e.message}")
  1
end