Class: Portless::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/portless/config.rb

Overview

Per-project config: an optional portless.json (or portless.yml) plus inferred defaults. Mirrors portless's config.ts/auto.ts: name is inferred from the config, the git root, or the directory; tld defaults to "localhost".

Constant Summary collapse

DEFAULT_SCRIPT =
"dev"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, dir = Dir.pwd) ⇒ Config

Returns a new instance of Config.



18
19
20
21
22
23
24
25
26
27
# File 'lib/portless/config.rb', line 18

def initialize(data, dir = Dir.pwd)
  @dir = dir
  @name = sanitize_label(data["name"] || infer_name(dir))
  @tld = (data["tld"] || Constants::DEFAULT_TLD).to_s
  @script = data["script"] || DEFAULT_SCRIPT
  @app_port = data["appPort"] || data["app_port"]
  @tls = data.fetch("tls", true)
  # Monorepo: { "apps": { "web": "bin/rails server", "api": "node api.js" } }.
  @apps = (data["apps"] || {}).transform_keys { |k| sanitize_label(k) }
end

Instance Attribute Details

#app_portObject (readonly)

Returns the value of attribute app_port.



12
13
14
# File 'lib/portless/config.rb', line 12

def app_port
  @app_port
end

#appsObject (readonly)

Returns the value of attribute apps.



12
13
14
# File 'lib/portless/config.rb', line 12

def apps
  @apps
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/portless/config.rb', line 12

def name
  @name
end

#scriptObject (readonly)

Returns the value of attribute script.



12
13
14
# File 'lib/portless/config.rb', line 12

def script
  @script
end

#tldObject (readonly)

Returns the value of attribute tld.



12
13
14
# File 'lib/portless/config.rb', line 12

def tld
  @tld
end

#tlsObject (readonly)

Returns the value of attribute tls.



12
13
14
# File 'lib/portless/config.rb', line 12

def tls
  @tls
end

Class Method Details

.load(dir = Dir.pwd) ⇒ Object



14
15
16
# File 'lib/portless/config.rb', line 14

def self.load(dir = Dir.pwd)
  new(read_file(dir), dir)
end

.read_file(dir) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/portless/config.rb', line 35

def self.read_file(dir)
  json = File.join(dir, "portless.json")
  return JSON.parse(File.read(json)) if File.exist?(json)

  {}
rescue JSON::ParserError => e
  raise Error, "invalid portless.json: #{e.message}"
end

Instance Method Details

#hostnameObject

The full hostname an app registers (e.g. "shirabe.org.localhost" when tld is set to that, or ".localhost" by default).



31
32
33
# File 'lib/portless/config.rb', line 31

def hostname
  tld.split(".").include?(name) ? tld : "#{name}.#{tld}"
end