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?, command_exists?, #current_packages_with_relations, #detected_package_path, #initialize, installed?

Constructor Details

This class inherits a constructor from LicenseFinder::PackageManager

Class Method Details

.package_management_commandObject



51
52
53
# File 'lib/license_finder/package_managers/yarn.rb', line 51

def self.package_management_command
  'yarn'
end

.prepare_commandObject



55
56
57
# File 'lib/license_finder/package_managers/yarn.rb', line 55

def self.prepare_command
  'yarn install'
end

.takes_priority_overObject



47
48
49
# File 'lib/license_finder/package_managers/yarn.rb', line 47

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
34
35
36
37
# File 'lib/license_finder/package_managers/yarn.rb', line 9

def current_packages
  cmd = "#{Yarn::SHELL_COMMAND}#{production_flag}"
  suffix = " --cwd #{project_path}" unless project_path.nil?
  cmd += suffix unless suffix.nil?

  stdout, _stderr, status = Cmd.run(cmd)
  return [] unless status.success?

  packages = []
  incompatible_packages = []

  json_strings = stdout.encode('ASCII', invalid: :replace, undef: :replace, replace: '?').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 = YarnPackage.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

#prepareObject



39
40
41
42
43
44
45
# File 'lib/license_finder/package_managers/yarn.rb', line 39

def prepare
  prep_cmd = "#{Yarn.prepare_command}#{production_flag}"
  _stdout, stderr, status = Dir.chdir(project_path) { Cmd.run(prep_cmd) }
  return if status.success?
  log_errors stderr
  raise "Prepare command '#{prep_cmd}' failed" unless @prepare_no_fail
end