Created
January 25, 2018 20:54
-
-
Save jstanley0/7d4e37fe49413f3f93a9c46e2b83baed to your computer and use it in GitHub Desktop.
detect webpack brokenness
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'digest' | |
SIG_FILE = File.expand_path("~/.sigcheck") | |
def read_sig_file | |
$sig_hashes = {} | |
if File.exists?(SIG_FILE) | |
File.open(SIG_FILE, 'r') do |file| | |
file.each_line do |line| | |
hashes = line.split | |
sig = hashes.shift | |
$sig_hashes[sig] = hashes | |
end | |
end | |
end | |
STDERR.puts "loaded #{$sig_hashes.size} signatures" | |
end | |
def write_sig_file | |
File.open(SIG_FILE, 'w') do |file| | |
$sig_hashes.each_key do |sig| | |
line = [sig] + $sig_hashes[sig] | |
file.puts line.join(' ') | |
end | |
end | |
STDERR.puts "saved #{$sig_hashes.size} signatures" | |
end | |
def add_sigs(search_path, check) | |
mismatches = 0 | |
Dir[File.join(search_path, '**/*')].each do |file| | |
next unless file =~ /-([0-9A-Fa-f]{10,32}).js$/ | |
sig = $1 | |
hash = Digest::MD5.hexdigest(File.binread(file)) | |
$sig_hashes[sig] ||= [] | |
if check && $sig_hashes[sig].any? { |h| h != hash } | |
mismatches += 1 | |
puts "#{file} sig=#{sig} md5=#{hash} does not match previous hash #{($sig_hashes[sig] - [hash]).join(', ')}" | |
end | |
$sig_hashes[sig] << hash unless $sig_hashes[sig].include?(hash) | |
end | |
mismatches | |
end | |
if ARGV.length != 2 || !['add', 'check'].include?(ARGV[0]) | |
STDERR.puts "usage: sigcheck (add|check) path/to/webpack-folder" | |
exit 1 | |
end | |
action = ARGV[0] | |
search_path = File.expand_path(ARGV[1]) | |
unless Dir.exist?(search_path) | |
STDERR.puts "#{search_path} not found" | |
end | |
read_sig_file | |
result = add_sigs(search_path, action == 'check') | |
write_sig_file | |
exit(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment