Sha256: dcd1b0cb127a1247a5ccfdd916ac1c4791818293a84cd421b7f5e1ecd72c2722

Contents?: true

Size: 858 Bytes

Versions: 1

Compression:

Stored size: 858 Bytes

Contents

require "httparty"

class Importmap::Pinner
  include HTTParty
  base_uri "https://api.jspm.io"

  def pin(*packages, env: "production", from: "jspm")
    fetch_imports(*packages, env: env, provider: from)&.tap do |imports|
      imports.each do |package, url|
        append_to_importmap package, url
      end
    end
  end

  private
    def append_to_importmap(package, url)
      Rails.root.join("config/importmap.rb").open("a") do |config|
        config.puts %(pin "#{package}", to: "#{url}")
      end
    end

    def fetch_imports(*packages, env:, provider:)
      response = self.class.post("/generate", body: {
        "install"      => Array(packages), 
        "flattenScope" => true,
        "env"          => [ "browser", env ],
        "provider"     => provider.to_s
      }.to_json)
      
      response.dig("map", "imports")
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
importmap-rails-0.4.0 lib/importmap/pinner.rb