Module: Rails::Generators::JsPackageManager

Included in:
ActionText::Generators::InstallGenerator, ChannelGenerator
Defined in:
railties/lib/rails/generators/js_package_manager.rb

Overview

:nodoc:

Constant Summary collapse

MANAGERS =
{
  bun: {
    add: "bun add %s",
    install: "bun install --frozen-lockfile",
    lockfile: "bun.lockb",
    audit: nil
  },
  pnpm: {
    add: "pnpm add %s",
    install: "pnpm install --frozen-lockfile",
    lockfile: "pnpm-lock.yaml",
    audit: "pnpm audit"
  },
  npm: {
    add: "npm install %s",
    install: "npm ci",
    lockfile: "package-lock.json",
    audit: "npm audit"
  },
  yarn: {
    add: "yarn add %s",
    install: "yarn install --immutable",
    lockfile: "yarn.lock",
    audit: "yarn audit"
  }
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.detect(root) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'railties/lib/rails/generators/js_package_manager.rb', line 35

def self.detect(root)
  if root.join("bun.lock").exist? || root.join("bun.lockb").exist? || root.join("bun.config.js").exist?
    :bun
  elsif root.join("pnpm-lock.yaml").exist?
    :pnpm
  elsif root.join("package-lock.json").exist?
    :npm
  else
    :yarn
  end
end

Instance Method Details

#package_add_command(package) ⇒ Object



55
56
57
# File 'railties/lib/rails/generators/js_package_manager.rb', line 55

def package_add_command(package)
  MANAGERS.dig(package_manager, :add) % package
end

#package_install_commandObject



59
60
61
# File 'railties/lib/rails/generators/js_package_manager.rb', line 59

def package_install_command
  MANAGERS.dig(package_manager, :install)
end

#package_lockfileObject



63
64
65
# File 'railties/lib/rails/generators/js_package_manager.rb', line 63

def package_lockfile
  MANAGERS.dig(package_manager, :lockfile)
end

#package_managerObject



47
48
49
# File 'railties/lib/rails/generators/js_package_manager.rb', line 47

def package_manager
  @package_manager ||= JsPackageManager.detect(project_root)
end

#using_js_runtime?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'railties/lib/rails/generators/js_package_manager.rb', line 51

def using_js_runtime?
  @using_js_runtime ||= package_json_path.exist?
end