Class: Iconmap::Commands

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/iconmap/commands.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean



11
12
13
# File 'lib/iconmap/commands.rb', line 11

def self.exit_on_failure?
  false
end

Instance Method Details

#auditObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/iconmap/commands.rb', line 60

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

#outdatedObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/iconmap/commands.rb', line 81

def outdated
  if (outdated_packages = npm.outdated_packages).any?
    table = [%w[Icon Current Latest]]
    outdated_packages.each { |p| table << [p.icon_path, p.current_version, p.latest_version || p.error] }

    puts_table(table)
    packages = 'icon'.pluralize(outdated_packages.size)
    puts "  #{outdated_packages.size} outdated #{packages} found"

    exit 1
  else
    puts 'No outdated icons found'
  end
end

#packagesObject



108
109
110
111
112
113
114
115
116
# File 'lib/iconmap/commands.rb', line 108

def packages
  # Print each package only once (multiple pins may reference the same npm package)
  seen = {}
  npm.packages_with_versions.each do |package, version, _|
    seen[package] ||= version
  end

  puts(seen.map { |package, version| "#{package} #{version}" })
end

#pin(*packages) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/iconmap/commands.rb', line 16

def pin(*packages)
  packages.each do |package|
    pin_line = packager.pin(package)
    parsed = packager.parse_icon_path(package)
    package_with_path = "#{parsed[:package]}/#{parsed[:path]}"

    puts %(Pinning "#{package_with_path}" to #{packager.vendor_path}/#{packager.vendored_filename(parsed[:package], parsed[:path])})

    if packager.packaged?(package_with_path)
      gsub_file('config/iconmap.rb', /^pin ['"]#{Regexp.escape(package_with_path)}['"].*$/, pin_line, verbose: false)
    else
      append_to_file('config/iconmap.rb', "#{pin_line}\n", verbose: false)
    end
  end
end

#pristineObject



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/iconmap/commands.rb', line 46

def pristine
  npm.packages_with_versions.each do |package, version|
    # Find all pins for this package to get full paths
    iconmap_content = File.read('config/iconmap.rb')
    iconmap_content.scan(%r{^pin ['"]#{Regexp.escape(package)}/([^'"]+)['"].*#\s*@#{Regexp.escape(version)}}).each do |path,|
      package_with_path = "#{package}/#{path}"
      url = Iconmap::Jsdelivr.new.download_url(package, version, path)
      puts %(Downloading "#{package_with_path}" to #{packager.vendor_path}/#{packager.vendored_filename(package, path)} from #{url})
      packager.download(package, version, path, url)
    end
  end
end

#unpin(*packages) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/iconmap/commands.rb', line 33

def unpin(*packages)
  packages.each do |package|
    parsed = packager.parse_icon_path(package)
    package_with_path = "#{parsed[:package]}/#{parsed[:path]}"

    if packager.packaged?(package_with_path)
      puts %(Unpinning and removing "#{package_with_path}")
      packager.remove(package_with_path)
    end
  end
end

#updateObject



97
98
99
100
101
102
103
104
105
# File 'lib/iconmap/commands.rb', line 97

def update
  if (outdated_packages = npm.outdated_packages).any?
    outdated_packages.each do |pkg|
      pin(pkg.icon_path)
    end
  else
    puts 'No outdated icons found'
  end
end