Class: Dato::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/dato/cli.rb

Instance Method Summary collapse

Instance Method Details

#checkObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/dato/cli.rb', line 52

def check
  exit 0 if ENV['DATO_API_TOKEN']

  say 'Site token is not specified!'
  token = ask "Please paste your DatoCMS site read-only API token:\n>"

  if !token || token.empty?
    puts 'Missing token'
    exit 1
  end

  File.open('.env', 'a') do |file|
    file.puts "DATO_API_TOKEN=#{token}"
  end

  say 'Token added to .env file.'

  exit 0
end

#dumpObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dato/cli.rb', line 18

def dump
  config_file = File.expand_path(options[:config])
  watch_mode = options[:watch]

  client = Dato::Site::Client.new(
    options[:token],
    extra_headers: {
      'X-Reason' => 'dump',
      'X-SSG' => Dump::SsgDetector.new(Dir.pwd).detect
    }
  )

  if watch_mode
    site_id = client.request(:get, '/site')['data']['id']

    semaphore = Mutex.new

    thread_safe_dump(semaphore, config_file, client)

    Dato::Watch::SiteChangeWatcher.new(site_id).connect do
      thread_safe_dump(semaphore, config_file, client)
    end

    watch_config_file(config_file) do
      thread_safe_dump(semaphore, config_file, client)
    end

    sleep
  else
    Dump::Runner.new(config_file, client).run
  end
end

#migrate_slugsObject



75
76
77
78
79
80
81
82
83
84
# File 'lib/dato/cli.rb', line 75

def migrate_slugs
  client = Dato::Site::Client.new(
    options[:token],
    extra_headers: {
      'X-Reason' => 'migrate-slugs'
    }
  )

  MigrateSlugs::Runner.new(client, options[:skip_id_prefix]).run
end