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'

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



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

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

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

#package_management_commandObject



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

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



41
42
43
44
45
46
47
48
# File 'lib/license_finder/package_managers/yarn.rb', line 41

def prepare
  prep_cmd = "#{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

#prepare_commandObject



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

def prepare_command
  'yarn install --ignore-engines --ignore-scripts'
end