/
npm.rb
70 lines (59 loc) · 2.08 KB
/
npm.rb
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
# frozen_string_literal: true
require "json"
module Licensed
module Source
class NPM
def initialize(config)
@config = config
end
def type
"npm"
end
def enabled?
@config.enabled?(type) && File.exist?(@config.pwd.join("package.json"))
end
def dependencies
return @dependencies if defined?(@dependencies)
locations = {}
package_location_command.lines.each do |line|
path, id = line.split(":")[0, 2]
locations[id] ||= path
end
packages = recursive_dependencies(JSON.parse(package_metadata_command)["dependencies"])
@dependencies = packages.map do |name, package|
path = package["realPath"] || locations["#{package["name"]}@#{package["version"]}"]
fail "couldn't locate #{name} under node_modules/" unless path
Dependency.new(path, {
"type" => type,
"name" => package["name"],
"version" => package["version"],
"summary" => package["description"],
"homepage" => package["homepage"]
})
end
end
# Recursively parse dependency JSON data. Returns a hash mapping the
# package name to it's metadata
def recursive_dependencies(dependencies, result = {})
dependencies.each do |name, dependency|
(result[name] ||= {}).update(dependency)
recursive_dependencies(dependency["dependencies"] || {}, result)
end
result
end
# Returns the output from running `npm list` to get package paths
def package_location_command
npm_list_command("--parseable", "--production", "--long")
end
# Returns the output from running `npm list` to get package metadata
def package_metadata_command
npm_list_command("--json", "--production", "--long")
end
# Executes an `npm list` command with the provided args and returns the
# output from stdout
def npm_list_command(*args)
Licensed::Shell.execute("npm", "list", *args)
end
end
end
end