Class: Mlo::Jekyll::BrowserSync::Command

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

Class Method Summary collapse

Class Method Details

.init_with_program(prog) ⇒ Object



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

def self.init_with_program(prog)
  prog.command(:browsersync) do |c|
    c.syntax "browsersync [options]"
    c.description "Serve the site using Browsersync"

    options.each { |opt| c.option(*opt) }

    add_build_options(c)

    c.action { |args, options| process(args, options) }
  end
end

.optionsObject



23
24
25
26
27
28
29
30
# File 'lib/jekyll-browsersync/command.rb', line 23

def self.options
  [
    ["host", "-H", "--host [HOST]", "Host to bind to"],
    ["port", "-P", "--port [PORT]", "Port to listen on"],
    ["open", "-o", "--open", "Launch your site in a browser"],
    ["browsersync", "-e", "--cli [PATH]", "Path to browsersync CLI"],
  ]
end

.process(args = [], options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/jekyll-browsersync/command.rb', line 32

def self.process(args = [], options = {})
  config = configuration_from_options(options)
  config["serving"] = true
  config["watch"] = true unless config.key?("watch")

  cli = config["browsersync"] || self.browsersync()
  args << "--server #{config["destination"]}"
  args << "--files #{config["destination"]}"
  args << "--port #{config["port"]}"
  args << "--host #{config["host"]}"
  args << "--no-open" unless config["open"]
  cmd = "#{cli} start #{args.join(" ")}"

  if `#{cli} --version 2>/dev/null`.empty?
    raise "Unable to locate browser-sync binary."
  end

  ::Jekyll::Commands::Build.process(config)

  PTY.spawn(cmd) do |stdout, stdin, pid|
    trap("INT") { Process.kill "INT", pid }
    
    begin
      stdout.each { |line| ::Jekyll.logger.info(line.rstrip) }
    rescue
    end
  end
end