Class: Belt::CLI::FrontendEnvCommand

Inherits:
Object
  • Object
show all
Includes:
AppDetection
Defined in:
lib/belt/cli/frontend_env_command.rb

Overview

belt frontend env

Smart-merges terraform outputs into frontend/.env using the declarative map (frontend/env.yml or .belt/frontend_env.yml). Only mapped keys are written; custom local keys are preserved.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AppDetection

#detect_app_name, #detect_environments, #detect_namespace, #s3_safe_name

Constructor Details

#initialize(env) ⇒ FrontendEnvCommand

Returns a new instance of FrontendEnvCommand.



76
77
78
79
80
# File 'lib/belt/cli/frontend_env_command.rb', line 76

def initialize(env)
  @env = env
  @app_name = detect_app_name
  @env_dir = "infrastructure/#{@env}"
end

Class Method Details

.run(args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/belt/cli/frontend_env_command.rb', line 17

def self.run(args)
  subcommand = args.shift

  case subcommand
  when 'env'
    run_env(args)
  when nil, '-h', '--help', 'help'
    puts usage
    exit(subcommand.nil? ? 1 : 0)
  else
    puts "Unknown frontend subcommand: #{subcommand}\n\n#{usage}"
    exit 1
  end
end

.run_env(args) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/belt/cli/frontend_env_command.rb', line 32

def self.run_env(args)
  env = EnvResolver.resolve(args)

  if env.nil?
    puts 'Usage: belt frontend env <environment>'
    puts "\nWrites frontend/.env from terraform outputs using the env map."
    puts 'You can also set BELT_ENV to skip the environment argument.'
    puts "\nMap file (optional): frontend/env.yml or .belt/frontend_env.yml"
    puts 'Default without map: VITE_API_URL ← api_url'
    puts "\nExamples:"
    puts '  belt frontend env dev'
    puts '  BELT_ENV=dev belt frontend env'
    exit 1
  end

  new(env).run
end

.usageObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/belt/cli/frontend_env_command.rb', line 50

def self.usage
  <<~USAGE
    Frontend helpers.

    Usage:
      belt frontend env <environment>   Write frontend/.env from terraform outputs
      belt frontend --help

    Env map (optional):
      frontend/env.yml
      .belt/frontend_env.yml

    Example map:
      VITE_API_URL: api_url
      VITE_COGNITO_USER_POOL_ID: cognito_user_pool_id
      VITE_COGNITO_CLIENT_ID: cognito_user_pool_client_id
      VITE_AWS_REGION: cognito_region

    Without a map, belt injects only VITE_API_URL from api_url.

    Smart merge: only keys listed in the map are updated. Other .env keys
    (local secrets, feature flags) are left alone. Missing terraform outputs
    warn and do not overwrite existing values.
  USAGE
end

Instance Method Details

#runObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/belt/cli/frontend_env_command.rb', line 82

def run
  unless Dir.exist?('frontend')
    abort 'Error: No frontend/ directory found. Run `belt generate frontend react` first.'
  end

  unless Dir.exist?(@env_dir)
    abort "Error: infrastructure/#{@env} not found. Run `belt generate environment #{@env}` first."
  end

  map = FrontendEnvMap.new(@env, env_dir: @env_dir)

  if map.using_default_map?
    puts '📋 No frontend/env.yml found — using default map (VITE_API_URL ← api_url)'
  else
    puts "📋 Loading env map from #{map.map_path}"
  end

  result = map.write_dotenv!
  updated = result[:updated]

  if updated.empty?
    puts "⚠️  No values written to #{result[:path]} (terraform outputs missing?)"
  else
    puts "✅ Updated #{result[:path]} (#{updated.join(', ')})"
  end
end