Skip to content

Instantly share code, notes, and snippets.

@fotinakis
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fotinakis/c30f7abfef6ca4f5f688 to your computer and use it in GitHub Desktop.
Save fotinakis/c30f7abfef6ca4f5f688 to your computer and use it in GitHub Desktop.
Upload a static website folder to Percy
#!/usr/bin/env ruby
#
# Usage:
# chmod +x percy-uploader.rb
# PERCY_TOKEN=<PERCY-REPO-WRITE-TOKEN> ./percy-uploader.rb
require 'percy'
require 'find'
require 'digest'
STATIC_EXTENSIONS = [
'.html', '.css', '.jpg', '.jpeg', '.gif', '.ico', '.png', '.bmp', '.pict', '.tif', '.tiff',
'.ttf', '.eot', '.woff', '.otf', '.svg', '.svgz', '.webp', '.ps',
].freeze
build = Percy.create_build(Percy.client.config.repo)
resources = []
Find.find('.') do |path|
# Skip directories.
next if !FileTest.file?(path)
# Skip all dot files and anything that's not a renderable static file.
next if path.match(/\/\./) || !STATIC_EXTENSIONS.include?(File.extname(path))
sha = Digest::SHA256.hexdigest(File.read(path))
resources << Percy::Client::Resource.new(path[1..-1], sha: sha)
end
html_resources = resources.select { |r| r.resource_url.match(/\.html$/) }
other_resources = resources - html_resources
html_resources.each do |html_resource|
puts "Processing... #{html_resource.resource_url}"
html_resource.is_root = true
# Create the snapshot for this page. For simplicity, include all non-HTML resources in the
# snapshot as related resources. May seem inefficient, but they will only ever be uploaded once.
snapshot = Percy.create_snapshot(build['data']['id'], [html_resource] + other_resources)
# Upload the content for any missing resources.
snapshot['data']['relationships']['missing-resources']['data'].each do |missing_resource|
missing_resource_sha = missing_resource['id']
resource = resources.find { |r| r.sha == missing_resource_sha }
path = resource.resource_url
puts "Uploading... #{path}"
Percy.upload_resource(build['data']['id'], File.read(".#{path}"))
end
end
Percy.finalize_build(build['data']['id'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment