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.



278
279
280
281
282
# File 'lib/react_on_rails/version_checker.rb', line 278

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.



254
255
256
# File 'lib/react_on_rails/version_checker.rb', line 254

def package_json
  @package_json
end

#package_lockObject (readonly)

Returns the value of attribute package_lock.



254
255
256
# File 'lib/react_on_rails/version_checker.rb', line 254

def package_lock
  @package_lock
end

#yarn_lockObject (readonly)

Returns the value of attribute yarn_lock.



254
255
256
# File 'lib/react_on_rails/version_checker.rb', line 254

def yarn_lock
  @yarn_lock
end

Class Method Details

.buildObject



256
257
258
# File 'lib/react_on_rails/version_checker.rb', line 256

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

.package_json_pathObject



260
261
262
# File 'lib/react_on_rails/version_checker.rb', line 260

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

.package_lock_pathObject



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

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



264
265
266
267
268
269
# File 'lib/react_on_rails/version_checker.rb', line 264

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)


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

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



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

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

  "react-on-rails"
end

#partsObject



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

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)


349
350
351
# File 'lib/react_on_rails/version_checker.rb', line 349

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

#range_syntax?Boolean

Returns:

  • (Boolean)


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

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

#rawObject



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/react_on_rails/version_checker.rb', line 284

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)


314
315
316
# File 'lib/react_on_rails/version_checker.rb', line 314

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

#react_on_rails_pro_package?Boolean

Returns:

  • (Boolean)


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

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

#semver_wildcard?Boolean

Returns:

  • (Boolean)


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

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)


337
338
339
# File 'lib/react_on_rails/version_checker.rb', line 337

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

#wildcard_or_x_range?Boolean

Returns:

  • (Boolean)


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

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

#workspace_protocol?Boolean

Returns:

  • (Boolean)


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

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