зеркало из https://github.com/github/licensed.git
improve glob when finding dependency package.json
suggested by ljharb as an improvement by ignoring package.json files that don't represent installed packages this also moves the dependency_paths method to the shared class to consolidate the functionality across both source versions
This commit is contained in:
Родитель
3057176ba6
Коммит
2c8ca11a74
|
@ -23,6 +23,22 @@ module Licensed
|
|||
def yarn_version
|
||||
Gem::Version.new(Licensed::Shell.execute("yarn", "-v"))
|
||||
end
|
||||
|
||||
# Returns a hash that maps all dependency names to their location on disk
|
||||
# by parsing every package.json file under node_modules.
|
||||
def dependency_paths
|
||||
@dependency_paths ||= Dir.glob(config.pwd.join("**/node_modules/*/package.json")).each_with_object({}) do |file, hsh|
|
||||
begin
|
||||
dirname = File.dirname(file)
|
||||
json = JSON.parse(File.read(file))
|
||||
hsh["#{json["name"]}@#{json["version"]}"] = dirname
|
||||
rescue JSON::ParserError
|
||||
# don't crash execution if there is a problem parsing a package.json file
|
||||
# if the bad package.json file relates to a package that licensed should be reporting on
|
||||
# then this will still result in an error about a missing package
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -32,7 +32,7 @@ module Licensed
|
|||
mapped_packages = yarn_info.reduce({}) do |accum, package|
|
||||
name, _ = package["value"].rpartition("@")
|
||||
version = package.dig("children", "Version")
|
||||
id = "#{name}-#{version}"
|
||||
id = "#{name}@#{version}"
|
||||
|
||||
accum[name] ||= []
|
||||
accum[name] << {
|
||||
|
@ -59,22 +59,6 @@ module Licensed
|
|||
end
|
||||
end
|
||||
|
||||
# Returns a hash that maps all dependency names to their location on disk
|
||||
# by parsing every package.json file under node_modules.
|
||||
def dependency_paths
|
||||
@dependency_paths ||= Dir.glob(config.pwd.join("node_modules/**/package.json")).each_with_object({}) do |file, hsh|
|
||||
begin
|
||||
dirname = File.dirname(file)
|
||||
json = JSON.parse(File.read(file))
|
||||
hsh["#{json["name"]}-#{json["version"]}"] = dirname
|
||||
rescue JSON::ParserError
|
||||
# don't crash execution if there is a problem parsing a package.json file
|
||||
# if the bad package.json file relates to a package that licensed should be reporting on
|
||||
# then this will still result in an error about a missing package
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Returns the output from running `yarn list` to get project dependencies
|
||||
def yarn_info_command
|
||||
args = %w(--json --manifest --recursive --all)
|
||||
|
|
|
@ -73,22 +73,6 @@ module Licensed
|
|||
result
|
||||
end
|
||||
|
||||
# Returns a hash that maps all dependency names to their location on disk
|
||||
# by parsing every package.json file under node_modules.
|
||||
def dependency_paths
|
||||
@dependency_paths ||= Dir.glob(config.pwd.join("node_modules/**/package.json")).each_with_object({}) do |file, hsh|
|
||||
begin
|
||||
dirname = File.dirname(file)
|
||||
json = JSON.parse(File.read(file))
|
||||
hsh["#{json["name"]}@#{json["version"]}"] = dirname
|
||||
rescue JSON::ParserError
|
||||
# don't crash execution if there is a problem parsing a package.json file
|
||||
# if the bad package.json file relates to a package that licensed should be reporting on
|
||||
# then this will still result in an error about a missing package
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Finds and returns the yarn package tree listing from `yarn list` output
|
||||
def yarn_package_tree
|
||||
return @yarn_package_tree if defined?(@yarn_package_tree)
|
||||
|
|
|
@ -84,7 +84,7 @@ if Licensed::Shell.tool_available?("yarn")
|
|||
graceful_fs_dependencies = source.dependencies.select { |dep| dep.name =~ /graceful-fs/ }
|
||||
assert_equal 2, graceful_fs_dependencies.size
|
||||
graceful_fs_dependencies.each do |dependency|
|
||||
assert_equal "#{dependency.record["name"]}-#{dependency.version}", dependency.name
|
||||
assert_equal "#{dependency.record["name"]}@#{dependency.version}", dependency.name
|
||||
assert dependency.exist?
|
||||
end
|
||||
end
|
||||
|
|
Загрузка…
Ссылка в новой задаче