Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Adds bin/importmap pristine which redownloads pinned packages #271

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/importmap/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ def unpin(*packages)
end
end

desc "pristine [*PACKAGES]", "Redownload all pinned packages"
option :env, type: :string, aliases: :e, default: "production"
option :from, type: :string, aliases: :f, default: "jspm"
def pristine(*packages)
packages = npm.packages_with_versions.map do |p, v|
v.blank? ? p : [p, v].join("@")
end

if imports = packager.import(*packages, env: options[:env], from: options[:from])
imports.each do |package, url|
puts %(Downloading "#{package}" to #{packager.vendor_path}/#{package}.js from #{url})
packager.download(package, url)
end
else
puts "Couldn't find any packages in #{packages.inspect} on #{options[:from]}"
end
end

desc "json", "Show the full importmap in json"
def json
require Rails.root.join("config/environment")
Expand Down
19 changes: 19 additions & 0 deletions test/commands_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ class CommandsTest < ActiveSupport::TestCase
assert_includes out, "Pinning"
end

test "pristine command redownloads all pinned packages" do
@tmpdir = Dir.mktmpdir
FileUtils.cp_r("#{__dir__}/dummy", @tmpdir)
Dir.chdir("#{@tmpdir}/dummy")
FileUtils.cp("#{__dir__}/fixtures/files/outdated_import_map.rb", "#{@tmpdir}/dummy/config/importmap.rb")
FileUtils.cp("#{__dir__}/../lib/install/bin/importmap", "bin")
out, _err = run_importmap_command("pin", "md5@2.2.0")

assert_includes out, 'Pinning "md5" to vendor/javascript/md5.js via download from https://ga.jspm.io/npm:md5@2.2.0/md5.js'

original = File.read("#{@tmpdir}/dummy/vendor/javascript/md5.js")
File.write("#{@tmpdir}/dummy/vendor/javascript/md5.js", "corrupted")

out, _err = run_importmap_command("pristine")

assert_includes out, 'Downloading "md5" to vendor/javascript/md5.js from https://ga.jspm.io/npm:md5@2.2.0'
assert_equal original, File.read("#{@tmpdir}/dummy/vendor/javascript/md5.js")
end

private
def run_importmap_command(command, *args)
capture_subprocess_io { system("bin/importmap", command, *args, exception: true) }
Expand Down
Loading