Class: UltimateTurboModal::Generators::UpdateGenerator

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

Instance Method Summary collapse

Instance Method Details

#copy_flavor_fileObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/generators/ultimate_turbo_modal/update_generator.rb', line 72

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

#install_js_dependenciesObject



68
69
70
# File 'lib/generators/ultimate_turbo_modal/update_generator.rb', line 68

def install_js_dependencies
  install_all_js_dependencies
end

#update_npm_package_versionObject



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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/generators/ultimate_turbo_modal/update_generator.rb', line 16

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 = gem_version_to_npm(UltimateTurboModal::VERSION.to_s)

  # Special case: demo app links to local JS package; never update its version
  local_link = json.dig("dependencies", package_name) || json.dig("devDependencies", package_name)
  if local_link&.match?(/\A(file|link):/)
    say "Detected local link for '#{package_name}' (#{local_link}). Skipping version update.", :blue
    return
  end

  found = false
  updated = false

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

    if json[section].key?(package_name)
      found = true
      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
  elsif found
    say "#{package_name} in package.json is already at version #{new_version}.", :blue
  else
    json["dependencies"] ||= {}
    json["dependencies"][package_name] = new_version
    File.write(package_json_path, JSON.pretty_generate(json) + "\n")
    say "Added #{package_name} (#{new_version}) to package.json dependencies.", :green
  end
end