-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathcommands.rb
135 lines (112 loc) · 4.29 KB
/
commands.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
require "thor"
require "importmap/packager"
require "importmap/npm"
class Importmap::Commands < Thor
include Thor::Actions
def self.exit_on_failure?
false
end
desc "pin [*PACKAGES]", "Pin new packages"
option :env, type: :string, aliases: :e, default: "production"
option :from, type: :string, aliases: :f, default: "jspm"
def pin(*packages)
if imports = packager.import(*packages, env: options[:env], from: options[:from])
imports.each do |package, url|
puts %(Pinning "#{package}" to #{packager.vendor_path}/#{package}.js via download from #{url})
packager.download(package, url)
pin = packager.vendored_pin_for(package, url)
if packager.packaged?(package)
gsub_file("config/importmap.rb", /^pin "#{package}".*$/, pin, verbose: false)
else
append_to_file("config/importmap.rb", "#{pin}\n", verbose: false)
end
end
else
puts "Couldn't find any packages in #{packages.inspect} on #{options[:from]}"
end
end
desc "unpin [*PACKAGES]", "Unpin existing packages"
option :env, type: :string, aliases: :e, default: "production"
option :from, type: :string, aliases: :f, default: "jspm"
def unpin(*packages)
if imports = packager.import(*packages, env: options[:env], from: options[:from])
imports.each do |package, url|
if packager.packaged?(package)
puts %(Unpinning and removing "#{package}")
packager.remove(package)
end
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")
puts Rails.application.importmap.to_json(resolver: ActionController::Base.helpers)
end
desc "audit", "Run a security audit"
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
desc "outdated", "Check for outdated packages"
def outdated
outdated_packages = npm.outdated_packages
if 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
desc "packages", "Print out packages with version numbers"
def packages
puts npm.packages_with_versions.map { |x| x.join(' ') }
end
private
def packager
@packager ||= Importmap::Packager.new
end
def npm
@npm ||= Importmap::Npm.new
end
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
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
puts head = "+" + (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 head if row_number == 0
end
puts head
end
end
Importmap::Commands.start(ARGV)