Skip to content

Instantly share code, notes, and snippets.

@ahoward
Created September 7, 2016 15:17
Show Gist options
  • Select an option

  • Save ahoward/b966e265815b3a15a31e27117e07df5a to your computer and use it in GitHub Desktop.

Select an option

Save ahoward/b966e265815b3a15a31e27117e07df5a to your computer and use it in GitHub Desktop.
#
require 'fileutils'
require 'dato'
require 'map'
require 'coerce'
require 'pry'
#
token = ARGV.shift
base_url = 'https://director-rat-37530.netlify.com/'
#
client = Dato::Site::Client.new(token)
#
repo_site = Dato::Site::Repo::Site.new(client)
repo_site_config = Map.for(repo_site.find)
locales = Coerce.list_of_symbols(repo_site_config[:locales])
#
local_site = Dato::Local::Site.new(client)
local_site.load
#
items_repo = local_site.items_repo
collections_by_type = Map.for(items_repo.collections_by_type)
#
I18n.enforce_available_locales = false
if locales && locales.empty?
has_locales = false
locales = [I18n.locale]
else
has_locales = true
end
default_locale = locales.first
#
data = Map.new
#
def expand_attributes(attrs = {})
map = Map.for(attrs)
to_expand = {}
map.depth_first_each do |key, val|
case
when val.is_a?(Dato::Local::FieldType::Image)
uri = ::URI.parse(val.url)
url = ::Imgix::Client.new(:host => uri.host, :secure => true).path(uri.path).to_url.split('?', 2).first
to_expand[key] = url
when val.is_a?(Dato::Local::FieldType::Seo)
title = val.title
description = val.description
image = val.image
url = image.file.to_url.split('?', 2).first
to_expand[key] = {:title => title, :description => description, :image => url}
end
end
to_expand.each{|key, val| map.set(key, val)}
map.to_hash
end
#
collections_by_type.each do |collection_name, item_or_collection|
was_array = item_or_collection.is_a?(Array)
list = [item_or_collection].flatten
list.each do |item|
attributes = Map.new
attributes[:id] = item.id
locales.each do |locale|
current_locale = I18n.locale
begin
I18n.locale = locale
attrs = Map.new
attrs[:slug] = item.slug(:prefix_with_id => false)
attrs.update(item.attributes)
expand_attributes(item.attributes).each do |key, val|
attrs[key] = val
end
if locale == default_locale
attributes.update(attrs)
end
if has_locales
attributes.set(:locales, locale, attrs)
end
ensure
I18n.locale = current_locale
end
end
if was_array
data[collection_name] ||= []
data[collection_name].push(attributes.to_hash)
else
data[collection_name] = attributes.to_hash
end
end
end
#
data[:global_seo] = expand_attributes(repo_site_config['global_seo'])
#
puts data.to_hash.to_yaml
#
data.each do |name, value|
path = "./data/#{ name }.yml"
FileUtils.mkdir_p(File.dirname(path))
FileUtils.rm_rf(path)
yaml = value.to_yaml
IO.binwrite(path, yaml)
end
#
p repo_site_config
# TODO -[X] all locales
# TODO -[X] all collections
# TODO -[X] all items
# TODO -[X] all fields
# TODO -[X] images ?
# TODO -[X] global_seo
# TODO -[?] seo settings (blowing up in admin)
#
# TODO -[?] favicon
# TODO -[?] favicon_helper
# TODO -[?] imgix_url helper
# TODO -[?] og helper
# broken global seo UI: https://cl.ly/hLpu/Image%202016-09-03%20at%208.01.00%20AM.png
__END__
site_config = {"id"=>"203",
"name"=>"clickclean-site",
"locales"=>["en", "es"],
"theme_hue"=>135,
"domain"=>nil,
"internal_domain"=>"purple-frog-4429.admin.datocms.com",
"global_seo"=>{"en"=>nil, "es"=>nil},
"favicon"=>nil,
"no_index"=>false,
"account"=>"101",
"menu_items"=>["1592", "1591", "1590"],
"item_types"=>["1400", "1401", "1399"]}
I18n.locale #=> :en
Imgix::Client.new(:host => 'dato-images.imgix.net', :secure => true).path('/203/1472871532-balls.jpg')
Imgix::Client.new(:host => 'dato-images.imgix.net', :secure => true).path('/203/1472871532-balls.jpg').width(50).to_url
WEBHOOK_URL
https://api.netlify.com/build_hooks/57c9ec60d6865d4e41de6c82
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment