Last active
March 19, 2019 09:29
-
-
Save phund/3d315ab697030f136ba2a02df340d2e2 to your computer and use it in GitHub Desktop.
Run lighthouse on a domain
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
defaults: &defaults | |
URL: https://example.com | |
development: | |
<<: *defaults | |
test: | |
<<: *defaults | |
staging: | |
<<: *defaults | |
production: | |
<<: *defaults |
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 'yaml' | |
require 'optparse' | |
require 'json' | |
require 'fileutils' | |
require 'uri/http' | |
require "csv" | |
options = {} | |
parser = OptionParser.new do |opts| | |
opts.banner = "Usage: main.rb [options]" | |
opts.on("-e", "--env=ENV", "Set environment") do |v| | |
options[:env] = v | |
end | |
opts.on("-p", "--port=PORT", "Set debug port") do |p| | |
options[:port] = p | |
end | |
opts.on("-f", "--function=FUNCTION", "Set run function") do |f| | |
options[:func] = f | |
end | |
end.parse! | |
env = options[:env] || 'development' | |
config = YAML.load_file('config.yml')[env] | |
@host_url = config['URL'] | |
@src = options[:src] | |
@port = options[:port].to_i | |
@func = options[:func] | |
if !['run_not_require_logins', 'run_require_logins', 'create_csv'].include?(@func) | |
p "Function wrong. Only accept: run_not_require_logins / run_require_logins / create_csv" | |
return | |
end | |
uri = URI.parse(@host_url) | |
@domain = uri.host.downcase | |
@results = {} | |
@routes = {} | |
@run_urls = [] | |
@date = Time.new.strftime("%Y-%m-%d") | |
def write_file(data, name, mode = 'w') | |
file_path = "#{@domain}/#{name}" | |
f = File.new(file_path, mode) | |
f.write(data) | |
f.write("\n") if mode == 'a' | |
f.close | |
end | |
def lighthouse_run(url, is_logined = false) | |
p "Running #{url}..." | |
json_string = `lighthouse #{@host_url}#{url} --quiet --port #{@port} --output json` | |
json = JSON.parse(json_string) | |
if !json['runtimeError'] | |
if json['requestedUrl'] == json['finalUrl'] | |
categories = json['categories'] | |
@results[url] = { } if !@results[url] | |
['performance', 'accessibility', 'best-practices', 'seo', 'pwa'].each do |c| | |
@results[url][c] = (categories[c]['score'] * 100).round | |
end | |
name = url.gsub('/', '_') | |
name += '(logined)' if is_logined | |
File.open("#{@domain}/#{@date}/#{name}.json","w") do |f| | |
f.write(json_string) | |
end | |
else | |
@routes['redirects'] << url unless @routes['redirects'].include?(url) | |
end | |
else | |
@routes['errors'] = [] if !@routes['errors'] | |
@routes['errors'] << url unless @routes['errors'].include?(url) | |
end | |
@run_urls << url unless @run_urls.include?(url) | |
write_file(@run_urls.to_json, 'run_urls.txt') | |
write_file(@routes.to_json, 'routes.txt') | |
write_file(@results.to_json, "#{@date}/results.txt") | |
end | |
def prepare_test_folder | |
FileUtils.mkdir_p "#{@domain}" unless File.directory?("#{@domain}") | |
FileUtils.mkdir_p "#{@domain}/#{@date}" unless File.directory?("{@domain}/#{@date}") | |
end | |
def read_routes_file | |
routes_file = "#{@domain}/routes.txt" | |
if !File.exist?(routes_file) | |
@routes = { | |
'redirects' => [], | |
'errors' => [] | |
} | |
return | |
end | |
File.open(routes_file, "r") do |f| | |
f.each_line do |line| | |
@routes = JSON.parse(line) | |
end | |
end | |
end | |
def read_run_urls_file | |
run_urls_file = "#{@domain}/run_urls.txt" | |
if !File.exist?(run_urls_file) | |
@run_urls = [] | |
return | |
end | |
File.open(run_urls_file, "r") do |f| | |
f.each_line do |line| | |
@run_urls = JSON.parse(line) | |
end | |
end | |
end | |
def read_results_file(name = 'results.txt') | |
resuls_file = "#{@domain}/#{@date}/#{name}" | |
if !File.exist?(resuls_file) | |
return {} | |
end | |
results = {} | |
File.open(resuls_file, "r") do |f| | |
f.each_line do |line| | |
results = JSON.parse(line) | |
end | |
end | |
results | |
end | |
def prepare_data | |
read_run_urls_file | |
read_results_file | |
end | |
def run_not_require_logins | |
file = "#{@domain}/not_require_logins.txt" | |
run_urls = @results.keys + @run_urls | |
File.open(file, "r") do |f| | |
f.each_line do |line| | |
unless run_urls.include?(line.strip) | |
lighthouse_run(line.strip, false) | |
end | |
end | |
end | |
end | |
def run_require_logins | |
file = "#{@domain}/require_logins.txt" | |
if !@port || !File.exist?(file) | |
p "Not yet set debug port / No data to run" | |
return | |
end | |
run_urls = @results.keys + @run_urls | |
File.open(file, "r") do |f| | |
f.each_line do |line| | |
unless run_urls.include?(line.strip) | |
lighthouse_run(line.strip, true) | |
end | |
end | |
end | |
end | |
def create_csv | |
csv_name = "#{@domain}_#{@date}.csv" | |
rs = read_results_file('not_loggin_results.txt') | |
rs2 = read_results_file('results.txt') | |
rs2.each do |k,v| | |
if !rs[k] | |
rs[k] = v | |
else | |
key = k + "(logined)" | |
rs[key] = v | |
end | |
end | |
CSV.open(csv_name, "wb") do |csv| | |
csv << ["Date", "Page", "Performance", "Accessibility", "Best Practices", "SEO", "PWA"] | |
rs.each do |k,v| | |
csv << ["#{@date}", k, v['performance'], v['accessibility'], v['best-practices'], v['seo'], v['pwa']] | |
end | |
end | |
end | |
if @domain | |
prepare_test_folder | |
prepare_data | |
send(@func) | |
end |
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
/users/sign_in | |
/users/forgot_password |
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
/home | |
/conversations |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment