2020-07-10 15:09:07 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
module Packages
|
|
|
|
module Npm
|
|
|
|
class PackageFinder
|
|
|
|
delegate :find_by_version, to: :execute
|
2021-02-17 18:09:19 +00:00
|
|
|
delegate :last, to: :execute
|
2020-07-10 15:09:07 +00:00
|
|
|
|
2021-02-17 18:09:19 +00:00
|
|
|
def initialize(package_name, project: nil, namespace: nil)
|
2020-07-10 15:09:07 +00:00
|
|
|
@package_name = package_name
|
2021-02-17 18:09:19 +00:00
|
|
|
@project = project
|
|
|
|
@namespace = namespace
|
2020-07-10 15:09:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2021-02-17 18:09:19 +00:00
|
|
|
base.npm
|
|
|
|
.with_name(@package_name)
|
|
|
|
.last_of_each_version
|
|
|
|
.preload_files
|
2020-07-10 15:09:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-02-17 18:09:19 +00:00
|
|
|
def base
|
|
|
|
if @project
|
|
|
|
packages_for_project
|
|
|
|
elsif @namespace
|
|
|
|
packages_for_namespace
|
|
|
|
else
|
|
|
|
::Packages::Package.none
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def packages_for_project
|
|
|
|
@project.packages
|
|
|
|
end
|
|
|
|
|
|
|
|
def packages_for_namespace
|
|
|
|
projects = ::Project.in_namespace(@namespace.self_and_descendants.select(:id))
|
|
|
|
::Packages::Package.for_projects(projects.select(:id))
|
2020-07-10 15:09:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|