Class: LicenseFinder::Yarn

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

Constant Summary collapse

SHELL_COMMAND =
'yarn licenses list --recursive --json'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PackageManager

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

Constructor Details

#initialize(options = {}) ⇒ Yarn

Returns a new instance of Yarn.



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

def initialize(options = {})
  super
  @yarn_options = options[:yarn_options]
end

Class Method Details

.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



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/license_finder/package_managers/yarn.rb', line 16

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?
    cmd += " #{@yarn_options}" unless @yarn_options.nil?
  end

  stdout, stderr, status = Dir.chdir(project_path) { Cmd.run(cmd) }
  raise "Command '#{cmd}' failed to execute: #{stderr}" unless status.success?

  json_strings = stdout.encode('ASCII', invalid: :replace, undef: :replace, replace: '?').split("\n")
  json_objects = json_strings.map { |json_object| JSON.parse(json_object) }

  if yarn_version == 1
    get_yarn1_packages(json_objects)
  else
    get_yarn_packages(json_objects)
  end
end

#package_management_commandObject



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

def package_management_command
  'yarn'
end

#possible_package_pathsObject



12
13
14
# File 'lib/license_finder/package_managers/yarn.rb', line 12

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

#prepareObject



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

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



55
56
57
58
59
60
61
# File 'lib/license_finder/package_managers/yarn.rb', line 55

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