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

-- 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



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

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

Instance Method Details

#app_jsxObject



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

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

#default?Boolean

Returns:

  • (Boolean)


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

def default?
  @default
end

#dist_dirObject

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



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

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)


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

def distribution_output_explicit?
  @distribution_output_explicit
end

#exists?Boolean

Returns:

  • (Boolean)


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

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

#labelObject



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

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

#output_prefixObject



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

def output_prefix
  tf_name
end

#package_jsonObject



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

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

#slugObject



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

def slug
  self.class.slug(name)
end

#src_dirObject



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

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

#tf_nameObject

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



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

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

#yaml_linesObject



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

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