class Importmap::Commands
Public Class Methods
Source
# File lib/importmap/commands.rb, line 8 def self.exit_on_failure? false end
Public Instance Methods
Source
# File lib/importmap/commands.rb, line 54 def audit vulnerable_packages = npm.vulnerable_packages if vulnerable_packages.any? table = [["Package", "Severity", "Vulnerable versions", "Vulnerability"]] vulnerable_packages.each { |p| table << [p.name, p.severity, p.vulnerable_versions, p.vulnerability] } puts_table(table) vulnerabilities = 'vulnerability'.pluralize(vulnerable_packages.size) severities = vulnerable_packages.map(&:severity).tally.sort_by(&:last).reverse .map { |severity, count| "#{count} #{severity}" } .join(", ") puts " #{vulnerable_packages.size} #{vulnerabilities} found: #{severities}" exit 1 else puts "No vulnerable packages found" end end
Source
# File lib/importmap/commands.rb, line 48 def json require Rails.root.join("config/environment") puts Rails.application.importmap.to_json(resolver: ActionController::Base.helpers) end
Source
# File lib/importmap/commands.rb, line 75 def outdated if (outdated_packages = npm.outdated_packages).any? table = [["Package", "Current", "Latest"]] outdated_packages.each { |p| table << [p.name, p.current_version, p.latest_version || p.error] } puts_table(table) packages = 'package'.pluralize(outdated_packages.size) puts " #{outdated_packages.size} outdated #{packages} found" exit 1 else puts "No outdated packages found" end end
Source
# File lib/importmap/commands.rb, line 107 def packages puts npm.packages_with_versions.map { |x| x.join(' ') } end
Source
# File lib/importmap/commands.rb, line 16 def pin(*packages) for_each_import(packages, env: options[:env], from: options[:from]) do |package, url| pin_package(package, url, options[:preload]) end end
Source
# File lib/importmap/commands.rb, line 37 def pristine packages = prepare_packages_with_versions for_each_import(packages, env: options[:env], from: options[:from]) do |package, url| puts %(Downloading "#{package}" to #{packager.vendor_path}/#{package}.js from #{url}) packager.download(package, url) end end
Source
# File lib/importmap/commands.rb, line 25 def unpin(*packages) for_each_import(packages, env: options[:env], from: options[:from]) do |package, url| if packager.packaged?(package) puts %(Unpinning and removing "#{package}") packager.remove(package) end end end
Source
# File lib/importmap/commands.rb, line 91 def update if (outdated_packages = npm.outdated_packages).any? package_names = outdated_packages.map(&:name) packages_with_options = packager.extract_existing_pin_options(package_names) for_each_import(package_names, env: "production", from: "jspm") do |package, url| options = packages_with_options[package] || {} pin_package(package, url, options[:preload]) end else puts "No outdated packages found" end end
Private Instance Methods
Source
# File lib/importmap/commands.rb, line 179 def for_each_import(packages, **options, &block) response = packager.import(*packages, **options) if response response[:imports].each(&block) else handle_package_not_found(packages, options[:from]) end end
Source
# File lib/importmap/commands.rb, line 140 def handle_package_not_found(packages, from) puts "Couldn't find any packages in #{packages.inspect} on #{from}" end
Source
# File lib/importmap/commands.rb, line 112 def packager @packager ||= Importmap::Packager.new end
Source
# File lib/importmap/commands.rb, line 120 def pin_package(package, url, preload) puts %(Pinning "#{package}" to #{packager.vendor_path}/#{package}.js via download from #{url}) packager.download(package, url) pin = packager.vendored_pin_for(package, url, preload) update_importmap_with_pin(package, pin) end
Source
# File lib/importmap/commands.rb, line 169 def prepare_packages_with_versions(packages = []) if packages.empty? npm.packages_with_versions.map do |p, v| v.blank? ? p : [p, v].join("@") end else packages end end
Source
# File lib/importmap/commands.rb, line 155 def puts_table(array) column_sizes = array.reduce([]) do |lengths, row| row.each_with_index.map{ |iterand, index| [lengths[index] || 0, iterand.to_s.length].max } end divider = "|" + (column_sizes.map { |s| "-" * (s + 2) }.join('|')) + '|' array.each_with_index do |row, row_number| row = row.fill(nil, row.size..(column_sizes.size - 1)) row = row.each_with_index.map { |v, i| v.to_s + " " * (column_sizes[i] - v.to_s.length) } puts "| " + row.join(" | ") + " |" puts divider if row_number == 0 end end
Source
# File lib/importmap/commands.rb, line 144 def remove_line_from_file(path, pattern) path = File.expand_path(path, destination_root) all_lines = File.readlines(path) with_lines_removed = all_lines.select { |line| line !~ pattern } File.open(path, "w") do |file| with_lines_removed.each { |line| file.write(line) } end end
Source
# File lib/importmap/commands.rb, line 130 def update_importmap_with_pin(package, pin) new_pin = "#{pin}\n" if packager.packaged?(package) gsub_file("config/importmap.rb", Importmap::Map.pin_line_regexp_for(package), pin, verbose: false) else append_to_file("config/importmap.rb", new_pin, verbose: false) end end