1
2
3
4
5
6
7
8
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# frozen_string_literal: true
module LicenseFinder
class Yarn
INCOMPATIBLE_PACKAGE_REGEX = /(?<name>[\w,\-]+)@(?<version>(\d+\.?)+)/.freeze
PHANTOM_PACKAGE_REGEX = /workspace-aggregator-[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}/.freeze
def possible_package_paths
[project_path.join('yarn.lock')]
end
def current_packages
stdout, _stderr, status = Dir.chdir(project_path) do
shell.execute(list_licenses_command)
end
return [] unless status.success?
stdout.each_line.flat_map do |line|
dependencies_from(JSON.parse(line))
end
end
def prepare
Dir.chdir(project_path) do
shell.execute([
:yarn, :install,
'--ignore-engines', '--ignore-scripts',
'--production'
])
end
end
private
def list_licenses_command
[
:yarn,
:licenses,
:list,
'--no-progress',
'--json',
'--production',
'--cwd',
project_path || Pathname.pwd
]
end
def install_path_for(name)
if project_path
project_path.join('node_modules', name)
else
Pathname.pwd.join('node_modules', name)
end
end
def map_from(hash)
name = hash['Name']
YarnPackage.new(
name,
hash['Version'],
spec_licenses: [hash['License']],
install_path: install_path_for(name).to_s,
homepage: hash['VendorUrl']
)
end
def dependencies_from(json)
case json['type']
when 'table'
from_json_table(json)
when 'info'
from_json_info(json)
else
[]
end
end
def from_json_table(json)
head = json['data']['head']
json['data']['body'].map do |array|
hash = Hash[head.zip(array)]
map_from(hash) unless PHANTOM_PACKAGE_REGEX.match(hash['Name'])
end.compact
end
def from_json_info(json)
matches = json['data'].to_s.match(INCOMPATIBLE_PACKAGE_REGEX)
return [] unless matches
[YarnPackage.new(matches['name'], matches['version'], spec_licenses: ['unknown'])]
end
end
end
|