Class: LicenseFinder::Yarn

Inherits:
PackageManager show all
Defined in:
lib/license_finder/package_managers/yarn.rb

Constant Summary collapse

SHELL_COMMAND =
'yarn licenses list --no-progress --json'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PackageManager

#active?, active_package_managers, active_packages, command_exists?, #current_packages_with_relations, #detected_package_path, #initialize, installed?, package_managers, #prepare

Constructor Details

This class inherits a constructor from LicenseFinder::PackageManager

Class Method Details

.package_management_commandObject



39
40
41
# File 'lib/license_finder/package_managers/yarn.rb', line 39

def self.package_management_command
  'yarn'
end

.prepare_commandObject



43
44
45
# File 'lib/license_finder/package_managers/yarn.rb', line 43

def self.prepare_command
  'yarn install'
end

.takes_priority_overObject



35
36
37
# File 'lib/license_finder/package_managers/yarn.rb', line 35

def self.takes_priority_over
  NPM
end

Instance Method Details

#current_packagesObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/license_finder/package_managers/yarn.rb', line 9

def current_packages
  stdout, _stderr, status = Cmd.run(Yarn::SHELL_COMMAND)
  return [] unless status.success?

  packages = []
  incompatible_packages = []

  json_strings = stdout.split("\n")
  json_objects = json_strings.map { |json_object| JSON.parse(json_object) }

  if json_objects.last['type'] == 'table'
    license_json = json_objects.pop['data']
    packages = packages_from_json(license_json)
  end

  json_objects.each do |json_object|
    match = /(?<name>[\w,\-]+)@(?<version>(\d+\.?)+)/ =~ json_object['data'].to_s
    if match
      package = Package.new(name, version, spec_licenses: ['unknown'])
      incompatible_packages.push(package)
    end
  end

  packages + incompatible_packages.uniq
end

#possible_package_pathsObject



5
6
7
# File 'lib/license_finder/package_managers/yarn.rb', line 5

def possible_package_paths
  [project_path.join('yarn.lock')]
end