Class: Danger::DangerWCC::Util::YarnInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/wcc/util/yarn_info.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plugin, options = nil) ⇒ YarnInfo

Returns a new instance of YarnInfo.



38
39
40
41
# File 'lib/wcc/util/yarn_info.rb', line 38

def initialize(plugin, options = nil)
  @plugin = plugin
  @options = options || {}
end

Instance Attribute Details

#pluginObject (readonly)

Returns the value of attribute plugin.



36
37
38
# File 'lib/wcc/util/yarn_info.rb', line 36

def plugin
  @plugin
end

Instance Method Details

#find_index_in_lockfile(package, version) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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_dependenciesObject



32
33
34
# File 'lib/wcc/util/yarn_info.rb', line 32

def modified_yarn_dependencies
  @modified_yarn_dependencies ||= find_modified_yarn_packages
end

#package_json_changesObject



28
29
30
# File 'lib/wcc/util/yarn_info.rb', line 28

def package_json_changes
  @package_json_changes ||= find_package_json_changes
end

#package_json_dependenciesObject

rubocop:disable Metrics/AbcSize



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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) ⇒ Object



58
59
60
61
# 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_lockObject



5
6
7
# File 'lib/wcc/util/yarn_info.rb', line 5

def yarn_lock
  @yarn_lock ||= File.readlines(@options[:lockfile] || 'yarn.lock')
end