Class: Licensed::Sources::Yarn

Inherits:
Source
  • Object
show all
Defined in:
lib/licensed/sources/yarn.rb

Constant Summary collapse

YARN_NAME_HEAD =

‘yarn licenses list –json` returns data in a table format with header ordering specified in the output. Look for these specific headers and use their indices to get data from the table body

"Name".freeze
YARN_VERSION_HEAD =
"Version".freeze
YARN_URL_HEAD =
"URL".freeze

Instance Attribute Summary

Attributes inherited from Source

#config

Instance Method Summary collapse

Methods inherited from Source

#dependencies, #ignored?, inherited, #initialize, type

Constructor Details

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

Instance Method Details

#dependency_urlsObject

Returns a mapping of unique dependency identifiers to urls



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/licensed/sources/yarn.rb', line 92

def dependency_urls
  @dependency_urls ||= begin
    table = yarn_licenses_command.lines
                                 .map(&:strip)
                                 .map(&JSON.method(:parse))
                                 .find { |json| json["type"] == "table" }
    return [] if table.nil?

    head = table.dig("data", "head")
    return [] if head.nil?

    name_index = head.index YARN_NAME_HEAD
    version_index = head.index YARN_VERSION_HEAD
    url_index = head.index YARN_URL_HEAD
    return [] if name_index.nil? || version_index.nil? || url_index.nil?

    body = table.dig("data", "body")
    return [] if body.nil?

    body.each_with_object({}) do |row, hsh|
      id = "#{row[name_index]}@#{row[version_index]}"
      hsh[id] = row[url_index]
    end
  end
end

#enabled?Boolean

Returns:

  • (Boolean)


14
15
16
17
18
# File 'lib/licensed/sources/yarn.rb', line 14

def enabled?
  return unless Licensed::Shell.tool_available?("yarn")

  config.pwd.join("package.json").exist? && config.pwd.join("yarn.lock").exist?
end

#enumerate_dependenciesObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/licensed/sources/yarn.rb', line 20

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

#include_non_production?Boolean

Returns whether to include non production dependencies based on the licensed configuration settings

Returns:

  • (Boolean)


133
134
135
# File 'lib/licensed/sources/yarn.rb', line 133

def include_non_production?
  config.dig("yarn", "production_only") == false
end

#packagesObject

Finds packages that the current project relies on



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/licensed/sources/yarn.rb', line 36

def packages
  return [] if yarn_package_tree.nil?
  all_dependencies = {}
  recursive_dependencies(config.pwd, yarn_package_tree).each do |name, results|
    results.uniq! { |package| package["version"] }
    if results.size == 1
      # if there is only one package for a name, reference it by name
      all_dependencies[name] = results[0]
    else
      # if there is more than one package for a name, reference each by
      # "<name>-<version>"
      results.each do |package|
        all_dependencies["#{name}-#{package["version"]}"] = package
      end
    end
  end

  all_dependencies
end

#recursive_dependencies(path, dependencies, result = {}) ⇒ Object

Recursively parse dependency JSON data. Returns a hash mapping the package name to it’s metadata



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/licensed/sources/yarn.rb', line 58

def recursive_dependencies(path, dependencies, result = {})
  dependencies.each do |dependency|
    # "shadow" indicate a dependency requirement only, not a
    # resolved package identifier
    next if dependency["shadow"]
    name, _, version = dependency["name"].rpartition("@")

    # the dependency should be found under the parent's "node_modules" path
    dependency_path = path.join("node_modules", name)
    (result[name] ||= []) << {
      "id" => dependency["name"],
      "name" => name,
      "version" => version,
      "path" => dependency_path
    }
    recursive_dependencies(dependency_path, dependency["children"], result)
  end
  result
end

#yarn_licenses_commandObject

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



126
127
128
129
130
# File 'lib/licensed/sources/yarn.rb', line 126

def yarn_licenses_command
  args = %w(--json -s --no-progress)
  args << "--production" unless include_non_production?
  Licensed::Shell.execute("yarn", "licenses", "list", *args, allow_failure: true)
end

#yarn_list_commandObject

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



119
120
121
122
123
# File 'lib/licensed/sources/yarn.rb', line 119

def yarn_list_command
  args = %w(--json -s --no-progress)
  args << "--production" unless include_non_production?
  Licensed::Shell.execute("yarn", "list", *args, allow_failure: true)
end

#yarn_package_treeObject

Finds and returns the yarn package tree listing from ‘yarn list` output



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/licensed/sources/yarn.rb', line 79

def yarn_package_tree
  return @yarn_package_tree if defined?(@yarn_package_tree)
  @yarn_package_tree = begin
    # parse all lines of output to json and find one that is "type": "tree"
    tree = yarn_list_command.lines
                            .map(&:strip)
                            .map(&JSON.method(:parse))
                            .find { |json| json["type"] == "tree" }
    tree&.dig("data", "trees")
  end
end