Class: Belt::CLI::FrontendEnvCommand
- Inherits:
-
Object
- Object
- Belt::CLI::FrontendEnvCommand
show all
- Includes:
- AppDetection
- Defined in:
- lib/belt/cli/frontend_env_command.rb
Overview
belt frontend env [--frontend NAME]
belt frontend list
Smart-merges terraform outputs into /.env using the declarative map
(/env.yml or .belt/frontend_env.yml for the default frontend).
Class Method Summary
collapse
Instance Method Summary
collapse
#detect_app_name, #detect_environments, #detect_namespace, #find_contracts_file_path, #find_routes_file_path, #find_schema_file_path, #s3_safe_name
Constructor Details
Returns a new instance of FrontendEnvCommand.
111
112
113
114
115
116
|
# File 'lib/belt/cli/frontend_env_command.rb', line 111
def initialize(env, frontend: nil)
@env = env
@app_name = detect_app_name
@env_dir = "infrastructure/#{@env}"
@frontend = frontend || FrontendRegistry.new.resolve!
end
|
Class Method Details
.run(args) ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/belt/cli/frontend_env_command.rb', line 18
def self.run(args)
subcommand = args.shift
case subcommand
when 'env'
run_env(args)
when 'list', 'ls'
run_list
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/belt/cli/frontend_env_command.rb', line 51
def self.run_env(args)
frontend_name = FrontendRegistry.(args, '--frontend')
env = EnvResolver.resolve(args)
if env.nil?
puts 'Usage: belt frontend env <environment> [--frontend NAME]'
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,yaml} or .belt/frontend_env.{yml,yaml}"
puts 'Default without map: VITE_API_URL ← api_url'
puts "\nExamples:"
puts ' belt frontend env dev'
puts ' belt frontend env dev --frontend ops'
puts ' BELT_ENV=dev belt frontend env'
exit 1
end
leftover = args.reject { |a| a.start_with?('-') }
frontend_name ||= leftover.shift
if frontend_name
frontend = FrontendRegistry.new.resolve!(frontend_name)
new(env, frontend: frontend).run
else
frontends = FrontendRegistry.new.all
abort FrontendRegistry.new.empty_message if frontends.empty?
frontends.each { |fe| new(env, frontend: fe).run }
end
end
|
.run_list ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/belt/cli/frontend_env_command.rb', line 35
def self.run_list
registry = FrontendRegistry.new
if registry.empty?
puts 'No frontends configured.'
puts 'Run `belt generate frontend react` or add config/frontends.yml.'
return
end
puts 'Frontends:'
registry.all.each do |fe|
marker = fe.default? ? ' (default)' : ''
status = fe.exists? ? '' : ' missing'
puts " #{fe.name.ljust(16)} #{fe.path}/#{marker}#{status}"
end
end
|
.usage ⇒ Object
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
108
109
|
# File 'lib/belt/cli/frontend_env_command.rb', line 82
def self.usage
<<~USAGE
Frontend helpers.
Usage:
belt frontend env <environment> [--frontend NAME]
belt frontend list
belt frontend --help
Env map (optional, per frontend):
<frontend>/env.yml
.belt/frontend_env.yml (default `frontend/` only)
Multiple frontends: config/frontends.yml (see `belt explain frontend`).
Example map:
VITE_API_URL: api_url
VITE_COGNITO_USER_POOL_ID: cognito_user_pool_id
VITE_COGNITO_CLIENT_ID: cognito_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
#run ⇒ Object
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/belt/cli/frontend_env_command.rb', line 118
def run
unless Dir.exist?(@frontend.path)
abort "Error: No #{@frontend.path}/ 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, frontend_path: @frontend.path)
prefix = @frontend.name == 'frontend' ? '' : "[#{@frontend.name}] "
if map.using_default_map?
puts "📋 #{prefix}No #{@frontend.path}/env.yml found — using default map (VITE_API_URL ← api_url)"
else
puts "📋 #{prefix}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
|