Class: UltimateTurboModal::Generators::UpdateGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/ultimate_turbo_modal/update_generator.rb

Instance Method Summary collapse

Instance Method Details

#copy_flavor_fileObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/generators/ultimate_turbo_modal/update_generator.rb', line 52

def copy_flavor_file
  flavor = detect_flavor
  unless flavor
    say "Could not determine UTMR flavor. Skipping flavor file copy.", :yellow
    return
  end

  template_rel = "flavors/#{flavor}.rb"
  template_abs = File.join(self.class.source_root, template_rel)

  unless File.exist?(template_abs)
    say "Flavor template not found for '#{flavor}' at #{template_abs}.", :red
    return
  end

  target_path = "config/initializers/ultimate_turbo_modal_#{flavor}.rb"
  copy_file template_rel, target_path, force: true
  say "Copied flavor initializer to #{target_path}.", :green
end

#update_npm_package_versionObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/generators/ultimate_turbo_modal/update_generator.rb', line 14

def update_npm_package_version
  package_json_path = rails_root_join("package.json")

  unless File.exist?(package_json_path)
    say "No package.json found. Skipping npm package version update.", :yellow
    return
  end

  begin
    json = JSON.parse(File.read(package_json_path))
  rescue JSON::ParserError => e
    say "Unable to parse package.json: #{e.message}", :red
    return
  end

  package_name = "ultimate_turbo_modal"
  new_version = UltimateTurboModal::VERSION.to_s

  updated = false

  %w[dependencies devDependencies].each do |section|
    next unless json.key?(section) && json[section].is_a?(Hash)

    if json[section].key?(package_name)
      old = json[section][package_name]
      json[section][package_name] = new_version
      updated = true if old != new_version
    end
  end

  if updated
    File.write(package_json_path, JSON.pretty_generate(json) + "\n")
    say "Updated #{package_name} version in package.json to #{new_version}.", :green
  else
    say "Did not find #{package_name} in package.json dependencies. Nothing to update.", :blue
  end
end