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)
|
||||
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']
|
||||
|
||||
if opts['watch']
|
||||
self.watch(site, source, destination)
|
||||
self.watch(site, opts)
|
||||
else
|
||||
self.build(site, source, destination)
|
||||
self.build(site, opts)
|
||||
end
|
||||
end
|
||||
|
||||
# Private: Build the site from source into destination.
|
||||
#
|
||||
# site - A Jekyll::Site instance
|
||||
# source - A String of the source path
|
||||
# destination - A String of the destination path
|
||||
# options - A Hash of options passed to the command
|
||||
#
|
||||
# Returns nothing.
|
||||
def self.build(site, source, destination)
|
||||
def self.build(site, options)
|
||||
source = options['source']
|
||||
destination = options['destination']
|
||||
puts "Building site: #{source} -> #{destination}"
|
||||
begin
|
||||
site.process
|
||||
|
@ -44,13 +45,15 @@ module Jekyll
|
|||
# Private: Watch for file changes and rebuild the site.
|
||||
#
|
||||
# site - A Jekyll::Site instance
|
||||
# source - A String of the source path
|
||||
# destination - A String of the destination path
|
||||
# options - A Hash of options passed to the command
|
||||
#
|
||||
# Returns nothing.
|
||||
def self.watch(site, source, destination)
|
||||
def self.watch(site, options)
|
||||
require 'directory_watcher'
|
||||
|
||||
source = options['source']
|
||||
destination = options['destination']
|
||||
|
||||
puts "Auto-Regenerating enabled: #{source} -> #{destination}"
|
||||
|
||||
dw = DirectoryWatcher.new(source)
|
||||
|
@ -65,12 +68,14 @@ module Jekyll
|
|||
|
||||
dw.start
|
||||
|
||||
trap("SIGINT") do
|
||||
puts "Stopping auto-regeneration..."
|
||||
exit 0
|
||||
end
|
||||
unless options['serving']
|
||||
loop { sleep 1000 }
|
||||
|
||||
loop { sleep 1000 }
|
||||
trap("INT") do
|
||||
puts "Stopping auto-regeneration..."
|
||||
exit 0
|
||||
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