Add initial serve command
The `ServeCommand` will let you serve your site locally for development. You can specify `--port`, `--host` and `--baseurl` options if you wish to change the defaults. Additionally the `BuildCommand` will be called before the processing of the serve command, this makes sure that the site is actually built. This means you are able to pass the `--watch` option to auto-regenerate your site, even while serving it locally.
This commit is contained in:
parent
14766497c8
commit
3b4feb41f0
29
bin/jekyll2
29
bin/jekyll2
|
@ -29,3 +29,32 @@ command :build do |c|
|
||||||
Jekyll::BuildCommand.process(options)
|
Jekyll::BuildCommand.process(options)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Serve command
|
||||||
|
#
|
||||||
|
# Args:
|
||||||
|
# --source
|
||||||
|
# --destination
|
||||||
|
# --watch
|
||||||
|
#
|
||||||
|
# --port
|
||||||
|
# --host
|
||||||
|
# --baseurl
|
||||||
|
command :serve do |c|
|
||||||
|
c.syntax = 'jekyll serve [options]'
|
||||||
|
c.description = 'Serve...'
|
||||||
|
c.option '-w', '--watch', 'Watch for changes and rebuild'
|
||||||
|
c.option '-p', '--port [PORT]', 'Port to listen on'
|
||||||
|
c.option '-h', '--host [HOST]', 'Host to bind to'
|
||||||
|
c.option '-b', '--baseurl [URL]', 'Base URL'
|
||||||
|
c.action do |args, options|
|
||||||
|
options.default :watch => false,
|
||||||
|
:port => '4000',
|
||||||
|
:host => '0.0.0.0',
|
||||||
|
:baseurl => '/',
|
||||||
|
:serving => true
|
||||||
|
|
||||||
|
Jekyll::BuildCommand.process(options)
|
||||||
|
Jekyll::ServeCommand.process(options)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
|
@ -14,20 +14,21 @@ module Jekyll
|
||||||
destination = opts['destination']
|
destination = opts['destination']
|
||||||
|
|
||||||
if opts['watch']
|
if opts['watch']
|
||||||
self.watch(site, source, destination)
|
self.watch(site, opts)
|
||||||
else
|
else
|
||||||
self.build(site, source, destination)
|
self.build(site, opts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Private: Build the site from source into destination.
|
# Private: Build the site from source into destination.
|
||||||
#
|
#
|
||||||
# site - A Jekyll::Site instance
|
# site - A Jekyll::Site instance
|
||||||
# source - A String of the source path
|
# options - A Hash of options passed to the command
|
||||||
# destination - A String of the destination path
|
|
||||||
#
|
#
|
||||||
# Returns nothing.
|
# Returns nothing.
|
||||||
def self.build(site, source, destination)
|
def self.build(site, options)
|
||||||
|
source = options['source']
|
||||||
|
destination = options['destination']
|
||||||
puts "Building site: #{source} -> #{destination}"
|
puts "Building site: #{source} -> #{destination}"
|
||||||
begin
|
begin
|
||||||
site.process
|
site.process
|
||||||
|
@ -44,13 +45,15 @@ module Jekyll
|
||||||
# Private: Watch for file changes and rebuild the site.
|
# Private: Watch for file changes and rebuild the site.
|
||||||
#
|
#
|
||||||
# site - A Jekyll::Site instance
|
# site - A Jekyll::Site instance
|
||||||
# source - A String of the source path
|
# options - A Hash of options passed to the command
|
||||||
# destination - A String of the destination path
|
|
||||||
#
|
#
|
||||||
# Returns nothing.
|
# Returns nothing.
|
||||||
def self.watch(site, source, destination)
|
def self.watch(site, options)
|
||||||
require 'directory_watcher'
|
require 'directory_watcher'
|
||||||
|
|
||||||
|
source = options['source']
|
||||||
|
destination = options['destination']
|
||||||
|
|
||||||
puts "Auto-Regenerating enabled: #{source} -> #{destination}"
|
puts "Auto-Regenerating enabled: #{source} -> #{destination}"
|
||||||
|
|
||||||
dw = DirectoryWatcher.new(source)
|
dw = DirectoryWatcher.new(source)
|
||||||
|
@ -65,12 +68,14 @@ module Jekyll
|
||||||
|
|
||||||
dw.start
|
dw.start
|
||||||
|
|
||||||
trap("SIGINT") do
|
unless options['serving']
|
||||||
puts "Stopping auto-regeneration..."
|
loop { sleep 1000 }
|
||||||
exit 0
|
|
||||||
end
|
|
||||||
|
|
||||||
loop { sleep 1000 }
|
trap("INT") do
|
||||||
|
puts "Stopping auto-regeneration..."
|
||||||
|
exit 0
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
module Jekyll
|
||||||
|
|
||||||
|
class ServeCommand < Command
|
||||||
|
def self.process(options)
|
||||||
|
require 'webrick'
|
||||||
|
include WEBrick
|
||||||
|
|
||||||
|
destination = options.destination
|
||||||
|
|
||||||
|
FileUtils.mkdir_p(destination)
|
||||||
|
|
||||||
|
mime_types = WEBrick::HTTPUtils::DefaultMimeTypes
|
||||||
|
mime_types.store 'js', 'application/javascript'
|
||||||
|
|
||||||
|
s = HTTPServer.new(
|
||||||
|
:Port => options.port,
|
||||||
|
:BindAddress => options.host,
|
||||||
|
:MimeTypes => mime_types
|
||||||
|
)
|
||||||
|
|
||||||
|
s.mount(options.baseurl, HTTPServlet::FileHandler, destination)
|
||||||
|
t = Thread.new { s.start }
|
||||||
|
trap("INT") { s.shutdown }
|
||||||
|
t.join()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
Loading…
Reference in New Issue