Created
June 14, 2018 21:50
-
-
Save wagenet/377637f2ffd681a9cf3a5f78c92c83d8 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
require 'json' | |
def run_cmd(cmd) | |
puts cmd | |
unless ENV['VERBOSE'] | |
cmd = "#{cmd} > /dev/null" | |
end | |
system(cmd) | |
end | |
$original_yarn_lock = File.read("yarn.lock") | |
output = `yarn outdated --json` | |
lines = output.split("\n") | |
json = lines.map{|l| JSON.parse(l) } | |
table = json.find{|l| l["type"] == "table" } | |
columns = table["data"]["head"] | |
packages = table["data"]["body"].map do |row| | |
Hash[*row.each_with_index.map{|item, index| [columns[index], item] }.flatten] | |
end | |
# TODO: Figure out how we can handle exotic packages | |
$wanted_upgrade = packages.select{|p| p["Wanted"] != p["Current"] && p["Wanted"] != "exotic" } | |
$test_cmd = ENV['TEST_CMD'] || 'yarn test' | |
def try_batch(to_try) | |
puts "*** TRY_BATCH ***" | |
puts to_try.map{|p| p} | |
File.write("yarn.lock", $original_yarn_lock) | |
# Merge in known successful | |
upgrades = to_try | $wanted_upgrade.select{|p| p["success"] } | |
# Get names | |
package_list = upgrades.map{|p| p["Package"] }.join(' ') | |
unless run_cmd("yarn upgrade #{package_list}") | |
raise "Unable to upgrade packages" | |
end | |
if run_cmd($test_cmd) | |
puts "Success!" | |
puts to_try.map{|p| p["Package"] } | |
# It worked! | |
to_try.each{|p| p["success"] = true } | |
else | |
puts "Failed!" | |
if to_try.length == 1 | |
puts "Found bad upgrade: #{to_try.first["Package"]}" | |
# If it failed, and we only have one, then we know that one is bad | |
to_try.first["success"] = false | |
else | |
puts "Narrowing it down" | |
# Bisect until we find the bad one | |
batch_size = (to_try.length.to_f / 2).ceil | |
try_batch(to_try[0...batch_size]) | |
try_batch(to_try[batch_size..-1]) | |
end | |
end | |
true | |
rescue SystemExit, Interrupt | |
File.write("yarn.lock", $original_yarn_lock) | |
end | |
if $wanted_upgrade.empty? | |
abort "Nothing to upgrade" | |
end | |
try_batch($wanted_upgrade) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment