Class: Licensed::Sources::Yarn::Berry

Inherits:
Source
  • Object
show all
Includes:
Licensed::Sources::Yarn
Defined in:
lib/licensed/sources/yarn/berry.rb

Instance Attribute Summary

Attributes inherited from Source

#config

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Licensed::Sources::Yarn

#dependency_paths, #enabled?, included, #yarn_version

Methods inherited from Source

#dependencies, #enabled?, full_type, #ignored?, inherited, #initialize, register_source, require_matched_dependency_version, #source_config, type, type_and_version

Constructor Details

This class inherits a constructor from Licensed::Sources::Source

Class Method Details

.version_requirementObject



9
10
11
# File 'lib/licensed/sources/yarn/berry.rb', line 9

def self.version_requirement
  Gem::Requirement.new(">= 2.0")
end

Instance Method Details

#enumerate_dependenciesObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/licensed/sources/yarn/berry.rb', line 13

def enumerate_dependencies
  packages.map do |name, package|
    Dependency.new(
      name: name,
      version: package["version"],
      path: package["path"],
      metadata: {
        "type"     => self.class.type,
        "name"     => package["name"],
        "homepage" => package["homepage"]
      }
    )
  end
end

#packagesObject

Finds packages that the current project relies on based on the output from ‘yarn info`



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/licensed/sources/yarn/berry.rb', line 29

def packages
  # parse all lines of output to json and find one that is "type": "tree"
  yarn_info = JSON.parse("[#{yarn_info_command.lines.join(",")}]")
  mapped_packages = yarn_info.reduce({}) do |accum, package|
    name, _ = package["value"].rpartition("@")
    version = package.dig("children", "Version")
    id = "#{name}@#{version}"

    accum[name] ||= []
    accum[name] << {
      "id" => id,
      "name" => name,
      "version" => version,
      "homepage" => package.dig("children", "Manifest", "Homepage"),
      "path" => dependency_paths[id]
    }
    accum
  end

  mapped_packages.each_with_object({}) do |(name, results), hsh|
    results.uniq! { |package| package["version"] }
    if results.size == 1
      # if there is only one package for a name, reference it by name
      hsh[name] = results[0]
    else
      # if there is more than one package for a name, reference each by id
      results.each do |package|
        hsh[package["id"]] = package
      end
    end
  end
end

#yarn_info_commandObject

Returns the output from running ‘yarn list` to get project dependencies



63
64
65
66
# File 'lib/licensed/sources/yarn/berry.rb', line 63

def yarn_info_command
  args = %w(--json --manifest --recursive --all)
  Licensed::Shell.execute("yarn", "info", *args)
end