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

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

Constant Summary collapse

DEFAULT_BROWSERSYNC_PATH =
'node_modules/.bin/browser-sync'
COMMAND_OPTIONS =
{
  "https"              => ["--https", "Use HTTPS"],
  "host"               => ["host", "-H", "--host [HOST]", "Host to bind to"],
  "open_url"           => ["-o", "--open-url", "Launch your site in a browser"],
  "port"               => ["-P", "--port [PORT]", "Port to listen on"],
  "show_dir_listing"   => ["--show-dir-listing",
    "Show a directory listing instead of loading your index file."],
  "skip_initial_build" => ["skip_initial_build", "--skip-initial-build",
    "Skips the initial site build which occurs before the server is started."],
  "ui_port"            => ["--ui-port [PORT]",
    "The port for Browsersync UI to run on"],
  "browsersync"        => ["--browser-sync [PATH]",
    "Specify the path to the Browsersync binary if in custom location."],
}.freeze

Class Method Summary collapse

Class Method Details

.init_with_program(prog) ⇒ Object



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
53
54
55
# File 'lib/jekyll-browsersync/command.rb', line 24

def init_with_program(prog)
  prog.command("browsersync") do |cmd|
    cmd.syntax "browsersync [options]"
    cmd.description 'Serve a Jekyll site using Browsersync.'
    cmd.alias :'browser-sync'
    cmd.alias :bs

    add_build_options(cmd)

    COMMAND_OPTIONS.each do |key, val|
      cmd.option key, *val
    end

    cmd.action do |args, opts|
      puts args.inspect
      opts['serving'] = true
      opts['watch'] = true unless opts.key?('watch')
      opts['incremental'] = true unless opts.key?('incremental')
      opts['port'] = 4000 unless opts.key?('port')
      opts['host'] = '127.0.0.1' unless opts.key?('host')
      opts['ui_port'] = 3001 unless opts.key?('ui_port')
      opts['browsersync'] = locate_browsersync unless opts.key?('browsersync')

      validate!(opts)

      config = opts['config']
      ::Jekyll::Commands::Build.process(opts)
      opts['config'] = config
      Command.process(opts, args)
    end
  end
end

.process(opts, args) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/jekyll-browsersync/command.rb', line 57

def process(opts, args)
  opts = configuration_from_options(opts)
  destination = opts['destination']

  args << "--server #{destination}"
  args << "--files #{destination}"
  args << "--port #{opts['port']}"
  args << "--host #{opts['host']}"
  args << "--ui-port #{opts['ui_port']}"
  args << '--https' if opts['https']
  args << '--no-open' unless opts['open_url']
  args << '--directory' if opts['show_dir_listing']

  cmd = "#{opts['browsersync']} start #{args.join(' ')}"

  PTY.spawn(cmd) do |stdout, stdin, pid|
    trap("INT") { Process.kill 'INT', pid }

    ::Jekyll.logger.info "Server address:", server_address(opts)
    ::Jekyll.logger.info "UI address:", server_address(opts, 'ui')

    begin
      stdout.each { |line| ::Jekyll.logger.debug line.rstrip }
    rescue
    end
  end
end