Class: ReactOnRails::VersionChecker::NodePackageVersion

Inherits:
Object
  • Object
show all
Defined in:
lib/react_on_rails/version_checker.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(package_json, yarn_lock = nil, package_lock = nil) ⇒ NodePackageVersion

Returns a new instance of NodePackageVersion.



248
249
250
251
252
# File 'lib/react_on_rails/version_checker.rb', line 248

def initialize(package_json, yarn_lock = nil, package_lock = nil)
  @package_json = package_json
  @yarn_lock = yarn_lock
  @package_lock = package_lock
end

Instance Attribute Details

#package_jsonObject (readonly)

Returns the value of attribute package_json.



224
225
226
# File 'lib/react_on_rails/version_checker.rb', line 224

def package_json
  @package_json
end

#package_lockObject (readonly)

Returns the value of attribute package_lock.



224
225
226
# File 'lib/react_on_rails/version_checker.rb', line 224

def package_lock
  @package_lock
end

#yarn_lockObject (readonly)

Returns the value of attribute yarn_lock.



224
225
226
# File 'lib/react_on_rails/version_checker.rb', line 224

def yarn_lock
  @yarn_lock
end

Class Method Details

.buildObject



226
227
228
# File 'lib/react_on_rails/version_checker.rb', line 226

def self.build
  new(package_json_path, yarn_lock_path, package_lock_path)
end

.package_json_pathObject



230
231
232
# File 'lib/react_on_rails/version_checker.rb', line 230

def self.package_json_path
  Rails.root.join(ReactOnRails.configuration.node_modules_location, "package.json")
end

.package_lock_pathObject



241
242
243
244
245
246
# File 'lib/react_on_rails/version_checker.rb', line 241

def self.package_lock_path
  # Lockfiles are in the same directory as package.json
  # If node_modules_location is empty, use Rails.root
  base_dir = ReactOnRails.configuration.node_modules_location.presence || ""
  Rails.root.join(base_dir, "package-lock.json").to_s
end

.yarn_lock_pathObject



234
235
236
237
238
239
# File 'lib/react_on_rails/version_checker.rb', line 234

def self.yarn_lock_path
  # Lockfiles are in the same directory as package.json
  # If node_modules_location is empty, use Rails.root
  base_dir = ReactOnRails.configuration.node_modules_location.presence || ""
  Rails.root.join(base_dir, "yarn.lock").to_s
end

Instance Method Details

#local_path_or_url?Boolean

Returns:

  • (Boolean)


327
328
329
330
331
332
# File 'lib/react_on_rails/version_checker.rb', line 327

def local_path_or_url?
  # See https://docs.npmjs.com/cli/v10/configuring-npm/package-json#dependencies
  # All path and protocol "version ranges" include / somewhere,
  # but we want to make an exception for npm:@scope/pkg@version.
  !raw.nil? && raw.include?("/") && !raw.start_with?("npm:")
end

#package_nameObject



292
293
294
295
296
# File 'lib/react_on_rails/version_checker.rb', line 292

def package_name
  return "react-on-rails-pro" if react_on_rails_pro_package?

  "react-on-rails"
end

#partsObject



340
341
342
343
344
345
346
347
348
349
# File 'lib/react_on_rails/version_checker.rb', line 340

def parts
  return if local_path_or_url? || workspace_protocol?

  match = raw.match(VERSION_PARTS_REGEX)
  unless match
    raise ReactOnRails::Error, "Cannot parse version number '#{raw}' (only exact versions are supported)"
  end

  match.captures.compact
end

#range_operator?Boolean

Returns:

  • (Boolean)


319
320
321
# File 'lib/react_on_rails/version_checker.rb', line 319

def range_operator?
  raw.start_with?(/[~^><*]/)
end

#range_syntax?Boolean

Returns:

  • (Boolean)


323
324
325
# File 'lib/react_on_rails/version_checker.rb', line 323

def range_syntax?
  raw.include?(" - ") || raw.include?(" || ")
end

#rawObject



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/react_on_rails/version_checker.rb', line 254

def raw
  return @raw if defined?(@raw)

  return @raw = nil unless File.exist?(package_json)

  parsed = parsed_package_contents
  return @raw = nil unless parsed.key?("dependencies")

  deps = parsed["dependencies"]

  # Check for react-on-rails-pro first (Pro takes precedence)
  if deps.key?("react-on-rails-pro")
    @raw = resolve_version(deps["react-on-rails-pro"], "react-on-rails-pro")
    return @raw
  end

  # Fall back to react-on-rails
  if deps.key?("react-on-rails")
    @raw = resolve_version(deps["react-on-rails"], "react-on-rails")
    return @raw
  end

  # Neither package found
  msg = "No 'react-on-rails' or 'react-on-rails-pro' entry in the dependencies of " \
        "#{NodePackageVersion.package_json_path}, which is the expected location according to " \
        "ReactOnRails.configuration.node_modules_location"
  Rails.logger.warn(msg)
  @raw = nil
end

#react_on_rails_package?Boolean

Returns:

  • (Boolean)


284
285
286
# File 'lib/react_on_rails/version_checker.rb', line 284

def react_on_rails_package?
  package_installed?("react-on-rails")
end

#react_on_rails_pro_package?Boolean

Returns:

  • (Boolean)


288
289
290
# File 'lib/react_on_rails/version_checker.rb', line 288

def react_on_rails_pro_package?
  package_installed?("react-on-rails-pro")
end

#semver_wildcard?Boolean

Returns:

  • (Boolean)


298
299
300
301
302
303
304
305
# File 'lib/react_on_rails/version_checker.rb', line 298

def semver_wildcard?
  # See https://docs.npmjs.com/cli/v10/configuring-npm/package-json#dependencies
  # We want to disallow all expressions other than exact versions
  # and the ones allowed by local_path_or_url?
  return true if raw.blank?

  special_version_string? || wildcard_or_x_range? || range_operator? || range_syntax?
end

#special_version_string?Boolean

Returns:

  • (Boolean)


307
308
309
# File 'lib/react_on_rails/version_checker.rb', line 307

def special_version_string?
  %w[latest next canary beta alpha rc].include?(raw.downcase)
end

#wildcard_or_x_range?Boolean

Returns:

  • (Boolean)


311
312
313
314
315
316
317
# File 'lib/react_on_rails/version_checker.rb', line 311

def wildcard_or_x_range?
  raw == "*" ||
    raw =~ /^[xX*]$/ ||
    raw =~ /^[xX*]\./ ||
    raw =~ /\.[xX*]\b/ ||
    raw =~ /\.[xX*]$/
end

#workspace_protocol?Boolean

Returns:

  • (Boolean)


334
335
336
337
338
# File 'lib/react_on_rails/version_checker.rb', line 334

def workspace_protocol?
  # pnpm workspace protocol: workspace:* or workspace:^
  # Used for monorepo internal dependencies
  !raw.nil? && raw.start_with?("workspace:")
end