Class: Importmap::Packager

Inherits:
Object
  • Object
show all
Defined in:
lib/importmap/packager.rb

Constant Summary collapse

Error =
Class.new(StandardError)
HTTPError =
Class.new(Error)
ServiceError =
Error.new(Error)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(importmap_path = "config/importmap.rb", vendor_path: "vendor/javascript") ⇒ Packager

Returns a new instance of Packager.



15
16
17
18
# File 'lib/importmap/packager.rb', line 15

def initialize(importmap_path = "config/importmap.rb", vendor_path: "vendor/javascript")
  @importmap_path = Pathname.new(importmap_path)
  @vendor_path    = Pathname.new(vendor_path)
end

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint.



10
11
12
# File 'lib/importmap/packager.rb', line 10

def endpoint
  @endpoint
end

#vendor_pathObject (readonly)

Returns the value of attribute vendor_path.



13
14
15
# File 'lib/importmap/packager.rb', line 13

def vendor_path
  @vendor_path
end

Instance Method Details

#download(package, url) ⇒ Object



54
55
56
57
58
# File 'lib/importmap/packager.rb', line 54

def download(package, url)
  ensure_vendor_directory_exists
  remove_existing_package_file(package)
  download_package_file(package, url)
end

#import(*packages, env: "production", from: "jspm") ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/importmap/packager.rb', line 20

def import(*packages, env: "production", from: "jspm")
  response = post_json({
    "install"      => Array(packages),
    "flattenScope" => true,
    "env"          => [ "browser", "module", env ],
    "provider"     => normalize_provider(from)
  })

  case response.code
  when "200"        then extract_parsed_imports(response)
  when "404", "401" then nil
  else                   handle_failure_response(response)
  end
end

#packaged?(package) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/importmap/packager.rb', line 50

def packaged?(package)
  importmap.match(/^pin ["']#{package}["'].*$/)
end

#pin_for(package, url) ⇒ Object



35
36
37
# File 'lib/importmap/packager.rb', line 35

def pin_for(package, url)
  %(pin "#{package}", to: "#{url}")
end

#remove(package) ⇒ Object



60
61
62
63
# File 'lib/importmap/packager.rb', line 60

def remove(package)
  remove_existing_package_file(package)
  remove_package_from_importmap(package)
end

#vendored_pin_for(package, url) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/importmap/packager.rb', line 39

def vendored_pin_for(package, url)
  filename = package_filename(package)
  version  = extract_package_version_from(url)

  if "#{package}.js" == filename
    %(pin "#{package}" # #{version})
  else
    %(pin "#{package}", to: "#{filename}" # #{version})
  end
end