Last active
February 7, 2017 22:26
-
-
Save ryankshaw/ea5aee804656142b4338679d3dd189b0 to your computer and use it in GitHub Desktop.
g
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 'net/http' | |
require 'uri' | |
require 'digest' | |
TEMP_DIR = '/tmp/customer_js_that_uses_require' | |
UNIQUE_JS_FILES = {} | |
def collect_js_urls | |
Account.active.joins(:brand_config).find_each do |acct| | |
url = acct.brand_config.try(:js_overrides) | |
next unless url.present? | |
contents = Net::HTTP.get(URI(url)) rescue nil | |
next unless contents.present? && contents =~ /(require|define)[\s]*\(/ | |
content_md5 = Digest::MD5.hexdigest(contents) | |
unless UNIQUE_JS_FILES.key?(content_md5) | |
UNIQUE_JS_FILES[content_md5] ||= [] | |
puts "writing #{url}" | |
File.write("#{TEMP_DIR}/#{content_md5}.js", contents.force_encoding('ISO-8859-1')) | |
end | |
UNIQUE_JS_FILES[content_md5] << { | |
shard: Shard.current.id, | |
root_account: { | |
id: acct.root_account.id, | |
name: acct.root_account.name | |
}, | |
account: { | |
id: acct.id, | |
name: acct.name | |
}, | |
url: url | |
} | |
end | |
end | |
FileUtils.mkdir_p(TEMP_DIR) | |
Shard.with_each_shard { collect_js_urls }; nil | |
File.write("#{TEMP_DIR}/unique_js_files.json", UNIQUE_JS_FILES.to_json) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment