Class: Shadwire::Config

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

Overview

Reads and writes a consuming Rails app's shadwire.json.

On-disk JSON shape (mirrors shadcn's components.json conventions):

{
"registry": "https://edumoraes.github.io/shadwire/r",
"tailwind": { "css": "app/assets/tailwind/application.css" },
"aliases": {
  "components":  "app/components",
  "ui":          "app/components/ui",
  "helpers":     "app/helpers",
  "controllers": "app/javascript/controllers",
  "vendorCss":   "vendor/shadwire"
},
"installed": {
  "button": { "version": "1.0.0", "files": ["app/components/ui/button_component.rb"] }
}
}

Constant Summary collapse

CONFIG_FILE =
"shadwire.json"
DEFAULTS =
{
  "registry" => "https://edumoraes.github.io/shadwire/r",
  "tailwind" => { "css" => "app/assets/tailwind/application.css" },
  "aliases" => {
    "components"  => "app/components",
    "ui"          => "app/components/ui",
    "helpers"     => "app/helpers",
    "controllers" => "app/javascript/controllers",
    "vendorCss"   => "vendor/shadwire"
  },
  "installed" => {}
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



41
42
43
# File 'lib/shadwire/config.rb', line 41

def aliases
  @aliases
end

#installedObject (readonly)

Returns the value of attribute installed.



41
42
43
# File 'lib/shadwire/config.rb', line 41

def installed
  @installed
end

#registryObject

Returns the value of attribute registry.



42
43
44
# File 'lib/shadwire/config.rb', line 42

def registry
  @registry
end

#rootObject (readonly)

Returns the value of attribute root.



41
42
43
# File 'lib/shadwire/config.rb', line 41

def root
  @root
end

#tailwind_cssObject (readonly)

Returns the value of attribute tailwind_css.



41
42
43
# File 'lib/shadwire/config.rb', line 41

def tailwind_css
  @tailwind_css
end

Class Method Details

.load(root) ⇒ Object

Loads shadwire.json from the given app root, or returns a Config with defaults when the file does not exist.



46
47
48
49
50
# File 'lib/shadwire/config.rb', line 46

def self.load(root)
  path = File.join(root, CONFIG_FILE)
  data = File.exist?(path) ? parse(path) : JSON.parse(JSON.generate(DEFAULTS))
  new(root, data)
end

.parse(path) ⇒ Object

A hand-edited shadwire.json is common enough that a raw JSON::ParserError backtrace is the wrong answer; name the file and the problem.



54
55
56
57
58
# File 'lib/shadwire/config.rb', line 54

def self.parse(path)
  JSON.parse(File.read(path))
rescue JSON::ParserError => e
  raise ConfigError, "#{path} is not valid JSON: #{e.message}"
end

Instance Method Details

#forget(name) ⇒ Object

Removes an entry from installed.



72
73
74
# File 'lib/shadwire/config.rb', line 72

def forget(name)
  @installed.delete(name)
end

#record_installed(name, version, files) ⇒ Object

Records an installed component: installed = { "version" => ..., "files" => [...] }.



67
68
69
# File 'lib/shadwire/config.rb', line 67

def record_installed(name, version, files)
  @installed[name] = { "version" => version, "files" => files }
end

#saveObject

Writes pretty JSON to /shadwire.json.



61
62
63
64
# File 'lib/shadwire/config.rb', line 61

def save
  FileUtils.mkdir_p(@root)
  File.write(File.join(@root, CONFIG_FILE), JSON.pretty_generate(to_h) + "\n")
end