Class: Guardrails::Init::ConfigWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/guardrails/init/config_writer.rb

Constant Summary collapse

CONFIG_FILENAME =
"guardrails.yml"
DEFAULT_TOKEN_PATHS =
{
  css_custom_properties: {
    "colors_file" => "app/assets/stylesheets/tokens/_colors.css",
    "type_scale_file" => "app/assets/stylesheets/tokens/_type.css"
  },
  scss_variables: {
    "colors_file" => "app/assets/stylesheets/tokens/_colors.scss",
    "type_scale_file" => "app/assets/stylesheets/tokens/_type.scss"
  },
  raw_hex: {
    "colors_file" => nil,
    "type_scale_file" => nil
  },
  none: {
    "colors_file" => nil,
    "type_scale_file" => nil
  }
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(root, output: $stdout, now: Time.now) ⇒ ConfigWriter

Returns a new instance of ConfigWriter.



30
31
32
33
34
# File 'lib/guardrails/init/config_writer.rb', line 30

def initialize(root, output: $stdout, now: Time.now)
  @root = Pathname(root)
  @output = output
  @now = now
end

Instance Method Details

#write(detection_result, overrides: {}, force: false) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/guardrails/init/config_writer.rb', line 36

def write(detection_result, overrides: {}, force: false)
  path = @root.join(CONFIG_FILENAME)
  existed = path.exist?
  if existed && !force
    @output.puts "#{CONFIG_FILENAME} already exists — refusing to overwrite."
    @output.puts "Delete or rename it, or set FORCE=1 to re-run init."
    return false
  end

  path.write(yaml_for(detection_result, overrides))
  @output.puts(existed ? "Overwrote #{CONFIG_FILENAME}" : "Wrote #{CONFIG_FILENAME}")
  true
end