Class: Extension::Tasks::FindNpmPackages

Inherits:
Object
  • Object
show all
Includes:
ShopifyCLI::MethodObject
Defined in:
lib/project_types/extension/tasks/find_npm_packages.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ShopifyCLI::MethodObject

included, #to_proc

Class Method Details

.all(*package_names, **config) ⇒ Object



13
14
15
# File 'lib/project_types/extension/tasks/find_npm_packages.rb', line 13

def self.all(*package_names, **config)
  new(**config).all(*package_names)
end

.at_least_one_of(*package_names, **config) ⇒ Object



9
10
11
# File 'lib/project_types/extension/tasks/find_npm_packages.rb', line 9

def self.at_least_one_of(*package_names, **config)
  new(**config).at_least_one_of(*package_names)
end

.exactly_one_of(*package_names, **config) ⇒ Object



17
18
19
# File 'lib/project_types/extension/tasks/find_npm_packages.rb', line 17

def self.exactly_one_of(*package_names, **config)
  new(**config).exactly_one_of(*package_names)
end

Instance Method Details

#all(*package_names) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/project_types/extension/tasks/find_npm_packages.rb', line 21

def all(*package_names)
  call(*package_names) do |found_packages|
    found_package_names = found_packages.map(&:name)
    next found_packages if Set.new(found_package_names) == Set.new(package_names)
    raise PackageResolutionFailed, format(
      "Missing packages: %s",
      (package_names - found_package_names).join(", ")
    )
  end
end

#at_least_one_of(*package_names) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/project_types/extension/tasks/find_npm_packages.rb', line 32

def at_least_one_of(*package_names)
  call(*package_names) do |found_packages|
    found_package_names = found_packages.map(&:name)
    next found_packages unless (found_package_names & package_names).empty?
    raise PackageResolutionFailed, format(
      "Expected at least one of the following packages: %s",
      package_names.join(", ")
    )
  end
end

#call(*package_names, &validate) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/project_types/extension/tasks/find_npm_packages.rb', line 65

def call(*package_names, &validate)
  validate ||= ->(found_packages) { found_packages }

  unless package_names.all? { |name| name.is_a?(String) }
    raise ArgumentError, "Expected a list of package names"
  end

  ShopifyCLI::Result
    .call(&method(:list_packages))
    .then(&method(:search_packages).curry[package_names])
    .then(&method(:filter_duplicates))
    .then(&validate)
end

#exactly_one_of(*package_names) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/project_types/extension/tasks/find_npm_packages.rb', line 43

def exactly_one_of(*package_names)
  call(*package_names) do |found_packages|
    case found_packages.count
    when 0
      raise PackageResolutionFailed, format(
        "Expected one of the following packages: %s",
        package_names.join(", ")
      )
    when 1
      found_packages.first.tap do |found_package|
        next found_package if package_names.include?(found_package.name)
        raise PackageResolutionFailed, format(
          "Expected the following package: %s",
          found_package.name
        )
      end
    else
      raise PackageResolutionFailed, "Found more than one package"
    end
  end
end

#filter_duplicates(packages) ⇒ Object



101
102
103
# File 'lib/project_types/extension/tasks/find_npm_packages.rb', line 101

def filter_duplicates(packages)
  packages.reject { |p| p.version.match(/deduped/) }.uniq
end

#list_packagesObject



79
80
81
82
83
84
# File 'lib/project_types/extension/tasks/find_npm_packages.rb', line 79

def list_packages
  result, error, status =
    js_system.call(yarn: yarn_list, npm: npm_list, capture_response: true)
  raise error unless status.success?
  result
end

#npm_listObject



90
91
92
# File 'lib/project_types/extension/tasks/find_npm_packages.rb', line 90

def npm_list
  production_only? ? %w[list --prod --depth=0] : %w[list]
end

#search_packages(packages, package_list) ⇒ Object



94
95
96
97
98
99
# File 'lib/project_types/extension/tasks/find_npm_packages.rb', line 94

def search_packages(packages, package_list)
  pattern = /(#{packages.join("|")})@(\d.*)$/
  package_list.scan(pattern).map do |(name, version)|
    Models::NpmPackage.new(name: name, version: version.strip)
  end
end

#yarn_listObject



86
87
88
# File 'lib/project_types/extension/tasks/find_npm_packages.rb', line 86

def yarn_list
  production_only? ? %w[list --production --depth=0] : %w[list]
end