Class: Homebrew::Bundle::Npm Private

Inherits:
Extension show all
Defined in:
bundle/extensions/npm.rb

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Extension

add_supported?, check, cleanup!, cleanup_disable_description, cleanup_disable_env, cleanup_item_name, cleanup_items, cleanup_supported?, disable_predicate_method, dump, dump_disable_description, dump_disable_env, dump_disable_predicate_method, dump_disable_supported?, dump_entry, dump_name, dump_output, dump_supported?, dump_with, ensure_package_manager_installed!, entry, flag, inherited, install_supported?, legacy_check_step, package_description, package_installed?, package_manager_executable!, package_manager_installed?, package_record, predicate_method, quote, remove_supported?, switch_description, with_package_manager_env

Methods included from Utils::Output::Mixin

#issue_reporting_message, #odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #opoo_once, #opoo_outside_github_actions, #opoo_without_github_actions_annotation, #pretty_cannot_install, #pretty_deprecated, #pretty_disabled, #pretty_duration, #pretty_install_status, #pretty_installed, #pretty_uninstalled, #pretty_unmarked, #pretty_upgradable, #pretty_warning

Methods inherited from PackageType

check, #checkable_entries, dump, dump_output, dump_supported?, #exit_early_check, #find_actionable, #format_checkable, #full_check, inherited, install_supported?

Class Method Details

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



18
# File 'bundle/extensions/npm.rb', line 18

def banner_name = "npm packages"

.batch_installable?(_name, _options = {}) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (Boolean)


86
87
88
# File 'bundle/extensions/npm.rb', line 86

def batch_installable?(_name, _options = {})
  true
end

.check_label ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



15
# File 'bundle/extensions/npm.rb', line 15

def check_label = "npm Package"

.cleanup_heading ⇒ String?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



28
29
30
# File 'bundle/extensions/npm.rb', line 28

def cleanup_heading
  banner_name
end

.install!(name, with: nil, preinstall: true, no_upgrade: false, verbose: false, force: false, **options) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • name (String)
  • with (Array<String>, nil) (defaults to: nil)
  • preinstall (Boolean) (defaults to: true)
  • no_upgrade (Boolean) (defaults to: false)
  • verbose (Boolean) (defaults to: false)
  • force (Boolean) (defaults to: false)
  • options (Homebrew::Bundle::EntryOption)

Returns:

  • (Boolean)


78
79
80
81
82
83
# File 'bundle/extensions/npm.rb', line 78

def install!(name, with: nil, preinstall: true, no_upgrade: false, verbose: false, force: false, **options)
  return super if !preinstall || !package_installed?(name, with:)

  puts "Upgrading #{name} #{package_description}. It is installed but not up-to-date." if verbose
  install_package!(name, with:, verbose:)
end

.install_batch!(entries, verbose: false) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (Boolean)


91
92
93
94
# File 'bundle/extensions/npm.rb', line 91

def install_batch!(entries, verbose: false)
  Bundle.system(package_manager_executable!.to_s, "install", *Language::Node.npm_install_security_args,
                "-g", *entries.map(&:name), verbose:)
end

.install_package!(name, with: nil, verbose: false) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • name (String)
  • with (Array<String>, nil) (defaults to: nil)
  • verbose (Boolean) (defaults to: false)

Returns:

  • (Boolean)


119
120
121
122
123
124
125
# File 'bundle/extensions/npm.rb', line 119

def install_package!(name, with: nil, verbose: false)
  _ = with

  npm = package_manager_executable!

  Bundle.system(npm.to_s, "install", *Language::Node.npm_install_security_args, "-g", name, verbose:)
end

.install_verb(name = "", _options = {}) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



48
49
50
# File 'bundle/extensions/npm.rb', line 48

def install_verb(name = "", _options = {})
  package_installed?(name) ? "Upgrading" : "Installing"
end

.installed_packages ⇒ Array<String>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



128
129
130
# File 'bundle/extensions/npm.rb', line 128

def installed_packages
  @installed_packages ||= packages.dup
end

.inventory_json(npm, command) ⇒ Array<(SystemCommand::Result, Hash{String => Object})>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'bundle/extensions/npm.rb', line 149

def inventory_json(npm, command)
  result = SystemCommand.run(npm, args: [command, "-g", "--depth=0", "--json"], print_stderr: false)
  result.assert_success! unless [0, 1].include?(result.exit_status)

  begin
    json = JSON.parse(result.stdout)
  rescue JSON::ParserError
    result.assert_success!
    raise "Invalid npm #{command} inventory: expected JSON"
  end

  unless json.is_a?(Hash)
    result.assert_success!
    raise "Invalid npm #{command} inventory: unexpected JSON structure"
  end

  [result, json]
end

.listed_package_names(npm) ⇒ Array<String>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'bundle/extensions/npm.rb', line 170

def listed_package_names(npm)
  result, json = inventory_json(npm, "list")
  dependencies = json.fetch("dependencies", {})
  valid = if result.exit_status == 1
    error = json["error"]
    json["dependencies"].is_a?(Hash) && error.is_a?(Hash) && error["code"] == "ELSPROBLEMS"
  else
    !json.key?("error")
  end

  if !dependencies.is_a?(Hash) || !valid
    result.assert_success!
    raise "Invalid npm list inventory: unexpected JSON structure"
  end

  dependencies = dependencies.reject do |_, |
    .is_a?(Hash) && ["missing"] == true
  end

  dependencies.keys.reject { |name| name == "npm" }
end

.outdated_package_names(npm) ⇒ Array<String>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'bundle/extensions/npm.rb', line 194

def outdated_package_names(npm)
  result, json = inventory_json(npm, "outdated")
  result.assert_success! if json.empty?

  valid = json.each_value.all? do |value|
    records = value.is_a?(Array) ? value : [value]
    next false if records.empty?

    records.all? do |record|
      next false unless record.is_a?(Hash)

      record.values_at("wanted", "latest").all?(String)
    end
  end

  unless valid
    result.assert_success!
    raise "Invalid npm outdated inventory: unexpected JSON structure"
  end

  json.keys.reject { |name| name == "npm" }
end

.outdated_packages ⇒ Array<String>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



133
134
135
136
137
138
139
140
141
# File 'bundle/extensions/npm.rb', line 133

def outdated_packages
  outdated_packages = @outdated_packages
  return outdated_packages if outdated_packages
  return [] unless (npm = package_manager_executable)

  @outdated_packages = with_env(package_manager_env(npm)) do
    outdated_package_names(npm)
  end
end

.package_manager_env(executable) ⇒ Hash{String => String}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



43
44
45
# File 'bundle/extensions/npm.rb', line 43

def package_manager_env(executable)
  { "PATH" => "#{executable.dirname}:#{ORIGINAL_PATHS.join(":")}" }
end

.package_manager_executable ⇒ Pathname?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



38
39
40
# File 'bundle/extensions/npm.rb', line 38

def package_manager_executable
  which("npm", ORIGINAL_PATHS)
end

.package_manager_name ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



33
34
35
# File 'bundle/extensions/npm.rb', line 33

def package_manager_name
  "node"
end

.packages ⇒ Array<String>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'bundle/extensions/npm.rb', line 97

def packages
  packages = @packages
  return packages if packages

  @packages = if (npm = package_manager_executable) &&
                 (!npm.to_s.start_with?("/") || npm.exist?)
    with_env(package_manager_env(npm)) do
      listed_package_names(npm)
    end
  end
  return [] if @packages.nil?

  @packages
end

.preinstall!(name, with: nil, no_upgrade: false, verbose: false, **options) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (Boolean)


61
62
63
64
65
# File 'bundle/extensions/npm.rb', line 61

def preinstall!(name, with: nil, no_upgrade: false, verbose: false, **options)
  return true if !no_upgrade && package_installed?(name, with:) && outdated_packages.include?(name)

  super
end

.reset! ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



21
22
23
24
25
# File 'bundle/extensions/npm.rb', line 21

def reset!
  @packages = T.let(nil, T.nilable(T::Array[String]))
  @installed_packages = T.let(nil, T.nilable(T::Array[String]))
  @outdated_packages = T.let(nil, T.nilable(T::Array[String]))
end

.type ⇒ Symbol

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



12
# File 'bundle/extensions/npm.rb', line 12

def type = :npm

.uninstall_package!(name, executable: Pathname.new("")) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (Boolean)


144
145
146
# File 'bundle/extensions/npm.rb', line 144

def uninstall_package!(name, executable: Pathname.new(""))
  Bundle.system(executable.to_s, "uninstall", "-g", name, verbose: false)
end

Instance Method Details

#failure_reason(package, no_upgrade:) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • package (Object)
  • no_upgrade (Boolean)

Returns:



220
221
222
223
224
# File 'bundle/extensions/npm.rb', line 220

def failure_reason(package, no_upgrade:)
  return super if no_upgrade || !self.class.package_installed?(self.class.dump_name(package))

  "#{self.class.check_label} #{self.class.dump_name(package)} needs to be upgraded."
end

#installed_and_up_to_date?(package, no_upgrade: false) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • package (Object)
  • no_upgrade (Boolean) (defaults to: false)

Returns:

  • (Boolean)


227
228
229
# File 'bundle/extensions/npm.rb', line 227

def installed_and_up_to_date?(package, no_upgrade: false)
  super && (no_upgrade || self.class.outdated_packages.exclude?(self.class.dump_name(package)))
end