class Danger::DangerWCC::Util::YarnInfo

Attributes

plugin[R]

Public Class Methods

new(plugin, options = nil) click to toggle source
# File lib/wcc/util/yarn_info.rb, line 38
def initialize(plugin, options = nil)
  @plugin = plugin
  @options = options || {}
end

Public Instance Methods

find_index_in_lockfile(package, version) click to toggle source
# File lib/wcc/util/yarn_info.rb, line 43
def find_index_in_lockfile(package, version)
  return 0 unless version

  re = Regexp.new("^\"?#{Regexp.escape(package)}@", Regexp::IGNORECASE)
  indexes =
    yarn_lock.each_with_index
      .select { |l, _i| re.match(l) }
      .map { |pair| pair[1] }
  idx =
    indexes.find do |i|
      yarn_lock[i + 1].include?("version \"#{version}\"")
    end
  (idx || -1) + 1
end
modified_yarn_dependencies() click to toggle source
# File lib/wcc/util/yarn_info.rb, line 32
def modified_yarn_dependencies
  @modified_yarn_dependencies ||= find_modified_yarn_packages
end
package_json_changes() click to toggle source
# File lib/wcc/util/yarn_info.rb, line 28
def package_json_changes
  @package_json_changes ||= find_package_json_changes
end
package_json_dependencies() click to toggle source
# File lib/wcc/util/yarn_info.rb, line 9
def package_json_dependencies # rubocop:disable Metrics/AbcSize
  @package_json_dependencies ||=
    begin
      root = JSON.parse(File.read('package.json'))
      (root['dependencies']&.keys || []).tap do |deps|
        workspaces = root['workspaces']
        next unless workspaces && !workspaces.empty?

        # Add in deps from all workspaces
        workspaces.each do |ws|
          Dir.glob(File.join(ws, 'package.json')).each do |package_file|
            d = JSON.parse(File.read(package_file))['dependencies']&.keys
            deps.concat(d || [])
          end
        end
      end
    end
end
parse_yarn_semver(line) click to toggle source
# File lib/wcc/util/yarn_info.rb, line 58
def parse_yarn_semver(line)
  match = /(?<package>\S+)\@(?<version>\S+)/.match(line)
  [match['package'], Gem::Version.new(match['version'])] if match
end
yarn_lock() click to toggle source
# File lib/wcc/util/yarn_info.rb, line 5
def yarn_lock
  @yarn_lock ||= File.readlines(@options[:lockfile] || 'yarn.lock')
end

Private Instance Methods

find_modified_yarn_packages() click to toggle source
# File lib/wcc/util/yarn_info.rb, line 74
def find_modified_yarn_packages # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  diff = plugin.run_and_diff(
    'NODE_ENV=production yarn list --depth 0 2>/dev/null'
  )
  diff = GitDiff.from_string(diff)

  {}.tap do |modified_packages|
    plugin.each_file_in_diff(diff) do |file, _diff|
      file.hunks.each do |hunk|
        deleted, added =
          %i[deletion? addition?].map do |type|
            Hash[hunk.lines.select { |l| l.public_send(type) }
              .map { |l| parse_yarn_semver(l.content) }
              .compact]
          end
        deleted.each do |(package, version)|
          modified_packages[package] =
            [version, added[package]]
        end
      end
    end
  end
end
find_package_json_changes() click to toggle source
# File lib/wcc/util/yarn_info.rb, line 65
def find_package_json_changes
  return [] unless file = plugin.find_file_in_diff('package.json')

  adds = file.hunks.flat_map { |h| h.lines.select(&:addition?) }
  adds.map { |l| /\"(?<package>\S+)\"\: \"\S+\"/.match(l.content) }
    .compact
    .map { |match| match['package'] }
end