Class: Belt::CLI::Frontend

Inherits:
Object
  • Object
show all
Defined in:
lib/belt/cli/frontend_registry.rb

Overview

A named frontend application (SPA) in a Belt project.

Belt apps historically used a single frontend/ directory. Apps like Stowzilla have several (customer app/, ops ops-app/, …). This object is the per-frontend view of path, build output, and terraform outputs.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, path:, dist: nil, bucket_output: nil, distribution_output: nil, url_output: nil, cloudfront_domain_output: nil, default: false) ⇒ Frontend

rubocop:disable Metrics/ParameterLists -- keyword options from YAML config



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

def initialize(name:, path:, dist: nil, bucket_output: nil, distribution_output: nil,
               url_output: nil, cloudfront_domain_output: nil, default: false)
  @name = name.to_s
  @path = path.to_s.sub(%r{/\z}, '')
  @dist = dist
  @default = default
  @bucket_output = bucket_output || default_output('bucket_name')
  @distribution_output_explicit = present_output?(distribution_output)
  @distribution_output = @distribution_output_explicit ? distribution_output : default_output('distribution_id')
  @url_output = url_output || default_output('url')
  @cloudfront_domain_output = cloudfront_domain_output
end

Instance Attribute Details

#bucket_outputObject (readonly)

Returns the value of attribute bucket_output.



14
15
16
# File 'lib/belt/cli/frontend_registry.rb', line 14

def bucket_output
  @bucket_output
end

#cloudfront_domain_outputObject (readonly)

Returns the value of attribute cloudfront_domain_output.



14
15
16
# File 'lib/belt/cli/frontend_registry.rb', line 14

def cloudfront_domain_output
  @cloudfront_domain_output
end

#defaultObject

Returns the value of attribute default.



16
17
18
# File 'lib/belt/cli/frontend_registry.rb', line 16

def default
  @default
end

#distObject (readonly)

Returns the value of attribute dist.



14
15
16
# File 'lib/belt/cli/frontend_registry.rb', line 14

def dist
  @dist
end

#distribution_outputObject (readonly)

Returns the value of attribute distribution_output.



14
15
16
# File 'lib/belt/cli/frontend_registry.rb', line 14

def distribution_output
  @distribution_output
end

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/belt/cli/frontend_registry.rb', line 14

def name
  @name
end

#pathObject

Returns the value of attribute path.



16
17
18
# File 'lib/belt/cli/frontend_registry.rb', line 16

def path
  @path
end

#url_outputObject (readonly)

Returns the value of attribute url_output.



14
15
16
# File 'lib/belt/cli/frontend_registry.rb', line 14

def url_output
  @url_output
end

Class Method Details

.slug(name) ⇒ Object



45
46
47
# File 'lib/belt/cli/frontend_registry.rb', line 45

def self.slug(name)
  name.to_s.downcase.gsub(/[^a-z0-9]+/, '_').gsub(/\A_|_\z/, '')
end

Instance Method Details

#app_jsxObject



66
67
68
# File 'lib/belt/cli/frontend_registry.rb', line 66

def app_jsx
  File.join(src_dir, 'App.jsx')
end

#default?Boolean

rubocop:enable Metrics/ParameterLists

Returns:

  • (Boolean)


33
34
35
# File 'lib/belt/cli/frontend_registry.rb', line 33

def default?
  @default
end

#dist_dirObject

Build output directory. Explicit dist: wins; otherwise prefer dist/ then build/ (CRA / some Vite configs) then default dist.



76
77
78
79
80
81
82
83
84
85
# File 'lib/belt/cli/frontend_registry.rb', line 76

def dist_dir
  return File.join(path, @dist) if @dist && !@dist.to_s.empty?

  %w[dist build].each do |dir|
    candidate = File.join(path, dir)
    return candidate if Dir.exist?(candidate)
  end

  File.join(path, 'dist')
end

#distribution_output_explicit?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/belt/cli/frontend_registry.rb', line 37

def distribution_output_explicit?
  @distribution_output_explicit
end

#exists?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/belt/cli/frontend_registry.rb', line 70

def exists?
  Dir.exist?(path) && File.file?(package_json)
end

#labelObject



87
88
89
# File 'lib/belt/cli/frontend_registry.rb', line 87

def label
  name == 'frontend' ? 'frontend' : "frontend '#{name}'"
end

#output_prefixObject



54
55
56
# File 'lib/belt/cli/frontend_registry.rb', line 54

def output_prefix
  tf_name
end

#package_jsonObject



62
63
64
# File 'lib/belt/cli/frontend_registry.rb', line 62

def package_json
  File.join(path, 'package.json')
end

#slugObject



41
42
43
# File 'lib/belt/cli/frontend_registry.rb', line 41

def slug
  self.class.slug(name)
end

#src_dirObject



58
59
60
# File 'lib/belt/cli/frontend_registry.rb', line 58

def src_dir
  File.join(path, 'src')
end

#tf_nameObject

Terraform resource name: frontend for the default, {slug}_frontend otherwise.



50
51
52
# File 'lib/belt/cli/frontend_registry.rb', line 50

def tf_name
  slug == 'frontend' ? 'frontend' : "#{slug}_frontend"
end

#yaml_linesObject



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/belt/cli/frontend_registry.rb', line 91

def yaml_lines
  lines = ["  #{name}:", "    path: #{path}"]
  lines << "    dist: #{dist}" if dist && !dist.to_s.empty?
  lines << '    default: true' if default?
  lines << "    bucket_output: #{bucket_output}" if bucket_output != inferred_output('bucket_name')
  if distribution_output != inferred_output('distribution_id')
    lines << "    distribution_output: #{distribution_output}"
  end
  lines << "    url_output: #{url_output}" if url_output != inferred_output('url')
  lines << "    cloudfront_domain_output: #{cloudfront_domain_output}" if cloudfront_domain_output
  lines
end