11require "net/http"
22require "uri"
33require "json"
4+ require "importmap/package"
45
56class Importmap ::Packager
67 Error = Class . new ( StandardError )
@@ -10,7 +11,7 @@ class Importmap::Packager
1011 singleton_class . attr_accessor :endpoint
1112 self . endpoint = URI ( "https://api.jspm.io/generate" )
1213
13- attr_reader :vendor_path
14+ attr_reader :vendor_path , :importmap_path
1415
1516 def initialize ( importmap_path = "config/importmap.rb" , vendor_path : "vendor/javascript" )
1617 @importmap_path = Pathname . new ( importmap_path )
@@ -32,34 +33,29 @@ def import(*packages, env: "production", from: "jspm")
3233 end
3334 end
3435
35- def pin_for ( package , url )
36- %( pin " #{ package } ", to: " #{ url } " )
36+ def packaged? ( package_name )
37+ importmap . match ( /^ pin ["'] #{ package_name } ["'].*$/ )
3738 end
3839
39- def vendored_pin_for ( package , url )
40- filename = package_filename ( package )
41- version = extract_package_version_from ( url )
40+ def remove_package_from_importmap ( package_name )
41+ all_lines = File . readlines ( @importmap_path )
42+ with_lines_removed = all_lines . grep_v ( /pin ["'] #{ package_name } ["']/ )
4243
43- if "#{ package } .js" == filename
44- %(pin "#{ package } " # #{ version } )
45- else
46- %(pin "#{ package } ", to: "#{ filename } " # #{ version } )
44+ File . open ( @importmap_path , "w" ) do |file |
45+ with_lines_removed . each { |line | file . write ( line ) }
4746 end
4847 end
4948
50- def packaged? ( package )
51- importmap . match ( /^pin ["']#{ package } ["'].*$/ )
52- end
53-
54- def download ( package , url )
55- ensure_vendor_directory_exists
56- remove_existing_package_file ( package )
57- download_package_file ( package , url )
49+ def pin_package_in_importmap ( package_name , pin )
50+ if packaged? ( package_name )
51+ gsub_file ( @importmap_path , /^pin "#{ package_name } ".*$/ , pin , verbose : false )
52+ else
53+ File . write ( @importmap_path , "#{ pin } \n " , mode : 'a+' )
54+ end
5855 end
5956
60- def remove ( package )
61- remove_existing_package_file ( package )
62- remove_package_from_importmap ( package )
57+ def ensure_vendor_directory_exists
58+ FileUtils . mkdir_p @vendor_path
6359 end
6460
6561 private
@@ -74,7 +70,22 @@ def normalize_provider(name)
7470 end
7571
7672 def extract_parsed_imports ( response )
77- JSON . parse ( response . body ) . dig ( "map" , "imports" )
73+ parsed_response = JSON . parse ( response . body )
74+
75+ imports = parsed_response . dig ( "map" , "imports" )
76+ static_dependencies = parsed_response [ "staticDeps" ] || [ ]
77+ dynamic_dependencies = parsed_response [ "dynamicDeps" ] || [ ]
78+
79+ dependencies = static_dependencies + dynamic_dependencies
80+
81+ imports . map do |package , url |
82+ Importmap ::Package . new (
83+ unfiltered_dependencies : dependencies ,
84+ package_name : package ,
85+ main_url : url ,
86+ packager : self
87+ )
88+ end
7889 end
7990
8091 def handle_failure_response ( response )
@@ -94,56 +105,4 @@ def parse_service_error(response)
94105 def importmap
95106 @importmap ||= File . read ( @importmap_path )
96107 end
97-
98-
99- def ensure_vendor_directory_exists
100- FileUtils . mkdir_p @vendor_path
101- end
102-
103- def remove_existing_package_file ( package )
104- FileUtils . rm_rf vendored_package_path ( package )
105- end
106-
107- def remove_package_from_importmap ( package )
108- all_lines = File . readlines ( @importmap_path )
109- with_lines_removed = all_lines . grep_v ( /pin ["']#{ package } ["']/ )
110-
111- File . open ( @importmap_path , "w" ) do |file |
112- with_lines_removed . each { |line | file . write ( line ) }
113- end
114- end
115-
116- def download_package_file ( package , url )
117- response = Net ::HTTP . get_response ( URI ( url ) )
118-
119- if response . code == "200"
120- save_vendored_package ( package , url , response . body )
121- else
122- handle_failure_response ( response )
123- end
124- end
125-
126- def save_vendored_package ( package , url , source )
127- File . open ( vendored_package_path ( package ) , "w+" ) do |vendored_package |
128- vendored_package . write "// #{ package } #{ extract_package_version_from ( url ) } downloaded from #{ url } \n \n "
129-
130- vendored_package . write remove_sourcemap_comment_from ( source ) . force_encoding ( "UTF-8" )
131- end
132- end
133-
134- def remove_sourcemap_comment_from ( source )
135- source . gsub ( /^\/ \/ # sourceMappingURL=.*/ , "" )
136- end
137-
138- def vendored_package_path ( package )
139- @vendor_path . join ( package_filename ( package ) )
140- end
141-
142- def package_filename ( package )
143- package . gsub ( "/" , "--" ) + ".js"
144- end
145-
146- def extract_package_version_from ( url )
147- url . match ( /@\d +\. \d +\. \d +/ ) &.to_a &.first
148- end
149108end
0 commit comments