Skip to content

Commit 699a9bb

Browse files
authoredJan 23, 2024
Add package information in vendored package file (#227)
Now that the packages are downloaded by default, apart from the output of the `pin` command we lose the track of the version and from where the package was downloaded. To track the version the solution was use a comment in bin/importmap next to the package version but nothing for the package origin so the proposal is add a comment in the first line of the downloaded package displaying the package name, version and from where it was downloaded.
1 parent be74dea commit 699a9bb

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed
 

‎lib/importmap/packager.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,16 @@ def download_package_file(package, url)
113113
response = Net::HTTP.get_response(URI(url))
114114

115115
if response.code == "200"
116-
save_vendored_package(package, response.body)
116+
save_vendored_package(package, url, response.body)
117117
else
118118
handle_failure_response(response)
119119
end
120120
end
121121

122-
def save_vendored_package(package, source)
122+
def save_vendored_package(package, url, source)
123123
File.open(vendored_package_path(package), "w+") do |vendored_package|
124+
vendored_package.write "// #{package}#{extract_package_version_from(url)} downloaded from #{url}\n\n"
125+
124126
vendored_package.write remove_sourcemap_comment_from(source).force_encoding("UTF-8")
125127
end
126128
end

‎test/packager_integration_test.rb

+11-6
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,17 @@ class Importmap::PackagerIntegrationTest < ActiveSupport::TestCase
2929
Rails.root.join("config/importmap.rb"),
3030
vendor_path: Pathname.new(vendor_dir)
3131

32-
@packager.download("@github/webauthn-json",
33-
"https://ga.jspm.io/npm:@github/webauthn-json@0.5.7/dist/main/webauthn-json.js")
34-
assert File.exist?(Pathname.new(vendor_dir).join("@github--webauthn-json.js"))
35-
36-
@packager.download("react", "https://ga.jspm.io/npm:react@17.0.2/index.js")
37-
assert File.exist?(Pathname.new(vendor_dir).join("react.js"))
32+
package_url = "https://ga.jspm.io/npm:@github/webauthn-json@0.5.7/dist/main/webauthn-json.js"
33+
@packager.download("@github/webauthn-json", package_url)
34+
vendored_package_file = Pathname.new(vendor_dir).join("@github--webauthn-json.js")
35+
assert File.exist?(vendored_package_file)
36+
assert_equal "// @github/webauthn-json@0.5.7 downloaded from #{package_url}", File.readlines(vendored_package_file).first.strip
37+
38+
package_url = "https://ga.jspm.io/npm:react@17.0.2/index.js"
39+
vendored_package_file = Pathname.new(vendor_dir).join("react.js")
40+
@packager.download("react", package_url)
41+
assert File.exist?(vendored_package_file)
42+
assert_equal "// react@17.0.2 downloaded from #{package_url}", File.readlines(vendored_package_file).first.strip
3843

3944
@packager.remove("react")
4045
assert_not File.exist?(Pathname.new(vendor_dir).join("react.js"))

0 commit comments

Comments
 (0)
Please sign in to comment.