Module: Shakapacker::Install::Env

Defined in:
lib/shakapacker/install/env.rb

Constant Summary collapse

TRUTHY_VALUES =
%w[true 1 yes].freeze
VALID_BUNDLERS =
%w[webpack rspack].freeze

Class Method Summary collapse

Class Method Details

.apply_bundler_arg(bundler_arg) ⇒ Object

Apply an optional shakapacker:install[bundler] task argument. A recognized bundler (webpack or rspack) is written to SHAKAPACKER_ASSETS_BUNDLER so the install template picks it up, overriding any value already set in that env var (the explicit argument wins). An unrecognized value returns an error message (and leaves the env var unset) so the caller can abort, mirroring the template's strict validation of SHAKAPACKER_ASSETS_BUNDLER. Returns nil when the argument is valid or absent.



71
72
73
74
75
76
77
78
79
80
# File 'lib/shakapacker/install/env.rb', line 71

def apply_bundler_arg(bundler_arg)
  return nil if bundler_arg.nil? || bundler_arg.empty?

  if VALID_BUNDLERS.include?(bundler_arg)
    ENV["SHAKAPACKER_ASSETS_BUNDLER"] = bundler_arg
    nil
  else
    "Unknown bundler '#{bundler_arg}'. Valid values: #{VALID_BUNDLERS.join(", ")}."
  end
end

.conflict_optionObject



13
14
15
16
17
18
19
20
21
# File 'lib/shakapacker/install/env.rb', line 13

def conflict_option
  if truthy_env?("FORCE")
    { force: true }
  elsif truthy_env?("SKIP")
    { skip: true }
  else
    {}
  end
end

.resolve_assets_bundler(env_value:, existing_bundler:, force:) ⇒ Object

Resolve which bundler the installer should set up. Precedence: an explicit SHAKAPACKER_ASSETS_BUNDLER env var (or task argument, which sets that env var) always wins; otherwise a FORCE overwrite installs the new-project default; otherwise an existing app keeps its current bundler (when it is a recognized value) so a re-install never silently switches it; brand-new installs fall back to rspack. Returning the env var verbatim lets the caller's strict VALID_BUNDLERS check still reject a misspelled value.



55
56
57
58
59
60
61
# File 'lib/shakapacker/install/env.rb', line 55

def resolve_assets_bundler(env_value:, existing_bundler:, force:)
  return env_value if env_value
  return "rspack" if force
  return existing_bundler if VALID_BUNDLERS.include?(existing_bundler)

  "rspack"
end

.truthy_env?(name) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/shakapacker/install/env.rb', line 9

def truthy_env?(name)
  TRUTHY_VALUES.include?(ENV[name].to_s.downcase)
end

.update_assets_bundler_config?(assets_bundler_to_install:, conflict_option:, config_preexisting:) ⇒ Boolean

Keep bundled runtime defaults backward compatible while allowing the installer to write an explicit new-project bundler choice. Takes the same inputs as update_transpiler_config? for consistency, but is deliberately more conservative: switching an existing app's bundler is impactful, so we only rewrite a config the installer owns (a fresh install, or an explicit FORCE overwrite) and never a pre-existing one (interactive or SKIP mode).

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
# File 'lib/shakapacker/install/env.rb', line 39

def update_assets_bundler_config?(assets_bundler_to_install:, conflict_option:, config_preexisting:)
  # The bundled shakapacker.yml already ships assets_bundler: "webpack", so
  # rewriting it to "webpack" would be a no-op. This relies on that shipped default.
  return false if assets_bundler_to_install == "webpack"
  return true if conflict_option[:force]

  !config_preexisting
end

.update_transpiler_config?(transpiler_to_install:, conflict_option:, config_preexisting:) ⇒ Boolean

Preserve existing shakapacker.yml when SKIP mode is active, but still update newly-copied files on fresh installs.

Returns:

  • (Boolean)


25
26
27
28
29
30
31
# File 'lib/shakapacker/install/env.rb', line 25

def update_transpiler_config?(transpiler_to_install:, conflict_option:, config_preexisting:)
  return false if transpiler_to_install == "swc"
  return true if conflict_option[:force]
  return true unless conflict_option[:skip]

  !config_preexisting
end