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.



294
295
296
297
298
# File 'lib/react_on_rails/version_checker.rb', line 294

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.



270
271
272
# File 'lib/react_on_rails/version_checker.rb', line 270

def package_json
  @package_json
end

#package_lockObject (readonly)

Returns the value of attribute package_lock.



270
271
272
# File 'lib/react_on_rails/version_checker.rb', line 270

def package_lock
  @package_lock
end

#yarn_lockObject (readonly)

Returns the value of attribute yarn_lock.



270
271
272
# File 'lib/react_on_rails/version_checker.rb', line 270

def yarn_lock
  @yarn_lock
end

Class Method Details

.buildObject



272
273
274
# File 'lib/react_on_rails/version_checker.rb', line 272

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

.package_json_pathObject



276
277
278
# File 'lib/react_on_rails/version_checker.rb', line 276

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

.package_lock_pathObject



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

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



280
281
282
283
284
285
# File 'lib/react_on_rails/version_checker.rb', line 280

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)


373
374
375
376
377
378
# File 'lib/react_on_rails/version_checker.rb', line 373

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



338
339
340
341
342
# File 'lib/react_on_rails/version_checker.rb', line 338

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

  "react-on-rails"
end

#partsObject



386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/react_on_rails/version_checker.rb', line 386

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). " \
          "#{ReactOnRails::DOCTOR_RECOMMENDATION}"
  end

  match.captures.compact
end

#range_operator?Boolean

Returns:

  • (Boolean)


365
366
367
# File 'lib/react_on_rails/version_checker.rb', line 365

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

#range_syntax?Boolean

Returns:

  • (Boolean)


369
370
371
# File 'lib/react_on_rails/version_checker.rb', line 369

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

#rawObject



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/react_on_rails/version_checker.rb', line 300

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)


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

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

#react_on_rails_pro_package?Boolean

Returns:

  • (Boolean)


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

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

#semver_wildcard?Boolean

Returns:

  • (Boolean)


344
345
346
347
348
349
350
351
# File 'lib/react_on_rails/version_checker.rb', line 344

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)


353
354
355
# File 'lib/react_on_rails/version_checker.rb', line 353

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

#wildcard_or_x_range?Boolean

Returns:

  • (Boolean)


357
358
359
360
361
362
363
# File 'lib/react_on_rails/version_checker.rb', line 357

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

#workspace_protocol?Boolean

Returns:

  • (Boolean)


380
381
382
383
384
# File 'lib/react_on_rails/version_checker.rb', line 380

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