Class: JekyllStats::Command

Inherits:
Jekyll::Command
  • Object
show all
Defined in:
lib/jekyll-stats/command.rb

Class Method Summary collapse

Class Method Details

.init_with_program(prog) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/jekyll-stats/command.rb', line 9

def init_with_program(prog)
  prog.command(:stats) do |c|
    c.syntax "stats [options]"
    c.description "Display site statistics"
    c.option "save", "--save", "Save stats to _data/stats.json"
    c.option "json", "--json", "Output raw JSON to stdout"
    c.option "drafts", "-D", "--drafts", "Include drafts in calculations"
    c.option "config", "--config CONFIG_FILE[,CONFIG_FILE2,...]", Array, "Custom configuration file"
    c.option "source", "-s", "--source SOURCE", "Custom source directory"
    c.option "destination", "-d", "--destination DESTINATION", "Custom destination directory"

    c.action do |_args, options|
      process(options)
    end
  end
end

.process(options) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/jekyll-stats/command.rb', line 26

def process(options)
  options = configuration_from_options(options)
  site = Jekyll::Site.new(options)

  Jekyll.logger.info "Loading site..."
  site.reset
  site.read

  calculator = StatsCalculator.new(site, include_drafts: options["drafts"])
  stats = calculator.calculate

  if options["json"]
    puts JSON.pretty_generate(stats)
  else
    formatter = Formatter.new(stats)
    puts formatter.to_terminal

    if options["save"]
      save_stats(site, stats)
    end
  end
end

.save_stats(site, stats) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/jekyll-stats/command.rb', line 49

def save_stats(site, stats)
  data_dir = File.join(site.source, "_data")
  FileUtils.mkdir_p(data_dir)

  path = File.join(data_dir, "stats.json")
  File.write(path, JSON.pretty_generate(stats))
  Jekyll.logger.info "Stats saved to #{path}"
end