Skip to content

Commit f273945

Browse files
authored
Adds bin/importmap pristine which redownloads pinned packages (#271)
1 parent abba7c8 commit f273945

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

lib/importmap/commands.rb

+18
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,24 @@ def unpin(*packages)
4646
end
4747
end
4848

49+
desc "pristine [*PACKAGES]", "Redownload all pinned packages"
50+
option :env, type: :string, aliases: :e, default: "production"
51+
option :from, type: :string, aliases: :f, default: "jspm"
52+
def pristine(*packages)
53+
packages = npm.packages_with_versions.map do |p, v|
54+
v.blank? ? p : [p, v].join("@")
55+
end
56+
57+
if imports = packager.import(*packages, env: options[:env], from: options[:from])
58+
imports.each do |package, url|
59+
puts %(Downloading "#{package}" to #{packager.vendor_path}/#{package}.js from #{url})
60+
packager.download(package, url)
61+
end
62+
else
63+
puts "Couldn't find any packages in #{packages.inspect} on #{options[:from]}"
64+
end
65+
end
66+
4967
desc "json", "Show the full importmap in json"
5068
def json
5169
require Rails.root.join("config/environment")

test/commands_test.rb

+19
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,25 @@ class CommandsTest < ActiveSupport::TestCase
3636
assert_includes out, "Pinning"
3737
end
3838

39+
test "pristine command redownloads all pinned packages" do
40+
@tmpdir = Dir.mktmpdir
41+
FileUtils.cp_r("#{__dir__}/dummy", @tmpdir)
42+
Dir.chdir("#{@tmpdir}/dummy")
43+
FileUtils.cp("#{__dir__}/fixtures/files/outdated_import_map.rb", "#{@tmpdir}/dummy/config/importmap.rb")
44+
FileUtils.cp("#{__dir__}/../lib/install/bin/importmap", "bin")
45+
out, _err = run_importmap_command("pin", "[email protected]")
46+
47+
assert_includes out, 'Pinning "md5" to vendor/javascript/md5.js via download from https://ga.jspm.io/npm:[email protected]/md5.js'
48+
49+
original = File.read("#{@tmpdir}/dummy/vendor/javascript/md5.js")
50+
File.write("#{@tmpdir}/dummy/vendor/javascript/md5.js", "corrupted")
51+
52+
out, _err = run_importmap_command("pristine")
53+
54+
assert_includes out, 'Downloading "md5" to vendor/javascript/md5.js from https://ga.jspm.io/npm:[email protected]'
55+
assert_equal original, File.read("#{@tmpdir}/dummy/vendor/javascript/md5.js")
56+
end
57+
3958
private
4059
def run_importmap_command(command, *args)
4160
capture_subprocess_io { system("bin/importmap", command, *args, exception: true) }

0 commit comments

Comments
 (0)