Class: LicenseFinder::Yarn

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

Constant Summary collapse

SHELL_COMMAND =
'yarn licenses list --json'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PackageManager

#active?, #command_exists?, #current_packages_with_relations, #detected_package_path, id, #initialize, #installed?, #project_root?

Constructor Details

This class inherits a constructor from LicenseFinder::PackageManager

Class Method Details

.takes_priority_overObject



53
54
55
# File 'lib/license_finder/package_managers/yarn.rb', line 53

def self.takes_priority_over
  NPM
end

Instance Method Details

#current_packagesObject



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
38
39
40
41
42
# File 'lib/license_finder/package_managers/yarn.rb', line 11

def current_packages
  # the licenses plugin supports the classic production flag
  cmd = "#{Yarn::SHELL_COMMAND}#{classic_yarn_production_flag}"
  if yarn_version == 1
    cmd += ' --no-progress'
    cmd += " --cwd #{project_path}" unless project_path.nil?
  end

  stdout, stderr, status = Cmd.run(cmd)
  raise "Command '#{cmd}' failed to execute: #{stderr}" 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

#package_management_commandObject



57
58
59
# File 'lib/license_finder/package_managers/yarn.rb', line 57

def package_management_command
  'yarn'
end

#possible_package_pathsObject



7
8
9
# File 'lib/license_finder/package_managers/yarn.rb', line 7

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

#prepareObject



44
45
46
47
48
49
50
51
# File 'lib/license_finder/package_managers/yarn.rb', line 44

def prepare
  prep_cmd = prepare_command.to_s
  _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

#prepare_commandObject



61
62
63
64
65
66
67
# File 'lib/license_finder/package_managers/yarn.rb', line 61

def prepare_command
  if yarn_version == 1
    classic_yarn_prepare_command
  else
    yarn_prepare_command
  end
end