Class: Dato::Cli

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

Instance Method Summary collapse

Instance Method Details

#checkObject



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

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
50
51
52
# File 'lib/dato/cli.rb', line 18

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

  client = Dato::Site::Client.new(
    options[:token],
    environment: options[:environment],
    extra_headers: {
      'X-Reason' => 'dump',
      'X-SSG' => Dump::SsgDetector.new(Dir.pwd).detect
    }
  )
  loader = Dato::Local::Loader.new(client, preview_mode)
  print 'Fetching content from DatoCMS... '
  loader.load

  if watch_mode
    semaphore = Mutex.new

    thread_safe_dump(semaphore, config_file, client, preview_mode, loader)

    loader.watch do
      thread_safe_dump(semaphore, config_file, client, preview_mode, loader)
    end

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

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