Skip to content

Commit 63d20cb

Browse files
committed
Add --serve flag.
1 parent ccb198f commit 63d20cb

5 files changed

Lines changed: 90 additions & 2 deletions

File tree

lib/mint/commandline/parse.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ def self.parse!(argv)
2626
commandline_options[:verbose] = true
2727
end
2828

29+
cli.on "--serve", "Publish files and serve them on a local HTTP server" do
30+
commandline_options[:serve_mode] = true
31+
end
32+
33+
cli.on "--serve-port PORT", Integer, "Port for the local server (default: 4000)" do |port|
34+
commandline_options[:serve_port] = port
35+
end
36+
2937
cli.on "-t", "--template TEMPLATE", "Specify a template by name (default: default)" do |t|
3038
commandline_options[:layout_name] = t
3139
commandline_options[:style_name] = t

lib/mint/commandline/run.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require "tempfile"
22

33
require_relative "../commandline/parse"
4+
require_relative "../commandline/serve"
45
require_relative "../config"
56
require_relative "../workspace"
67

@@ -14,7 +15,11 @@ def self.run!(argv)
1415
exit 0
1516
else
1617
begin
17-
publish!(files, config: config)
18+
if config.serve_mode
19+
serve!(files, config: config)
20+
else
21+
publish!(files, config: config)
22+
end
1823
rescue ArgumentError => e
1924
$stderr.puts "Error: #{e.message}"
2025
exit 1

lib/mint/commandline/serve.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
require "tmpdir"
2+
require "webrick"
3+
require "fileutils"
4+
5+
require_relative "../config"
6+
7+
module Mint
8+
module Commandline
9+
def self.serve!(source_files, config:)
10+
config = Config.ensure_config(config)
11+
port = config.serve_port
12+
13+
tmp_dir = Dir.mktmpdir("mint-serve-")
14+
15+
begin
16+
serve_config = Config.new(config.to_h.merge(
17+
destination_directory: Pathname.new(tmp_dir),
18+
style_mode: :inline
19+
))
20+
21+
publish!(source_files, config: serve_config)
22+
23+
log = WEBrick::Log.new(nil, WEBrick::BasicLog::ERROR)
24+
server = WEBrick::HTTPServer.new(
25+
Port: port,
26+
Logger: log,
27+
AccessLog: []
28+
)
29+
30+
server.mount_proc "/" do |req, res|
31+
path = req.path.gsub(/\.\./, "")
32+
file_path = File.join(tmp_dir, path)
33+
34+
resolved =
35+
if File.file?(file_path)
36+
file_path
37+
elsif File.file?("#{file_path}.html")
38+
"#{file_path}.html"
39+
elsif File.file?(File.join(file_path, "index.html"))
40+
File.join(file_path, "index.html")
41+
end
42+
43+
if resolved
44+
res.status = 200
45+
res.content_type = WEBrick::HTTPUtils.mime_type(resolved, WEBrick::HTTPUtils::DefaultMimeTypes)
46+
res.body = File.binread(resolved)
47+
else
48+
res.status = 404
49+
res.content_type = "text/plain"
50+
res.body = "Not found: #{req.path}"
51+
end
52+
end
53+
54+
trap("INT") { server.shutdown }
55+
trap("TERM") { server.shutdown }
56+
57+
puts "Serving on http://localhost:#{port} (press Ctrl+C to stop)"
58+
server.start
59+
ensure
60+
FileUtils.rm_rf(tmp_dir)
61+
end
62+
end
63+
end
64+
end

lib/mint/config.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ class Config
55
attr_accessor :files
66
attr_accessor :stdin_mode
77
attr_accessor :stdout_mode
8+
attr_accessor :serve_mode
9+
attr_accessor :serve_port
810
attr_accessor :help
911
attr_accessor :verbose
1012
attr_accessor :layout_name
@@ -17,9 +19,11 @@ class Config
1719
attr_accessor :preserve_structure
1820
attr_accessor :autodrop
1921
attr_accessor :options
20-
22+
2123
DEFAULT_STDIN_MODE = false
2224
DEFAULT_STDOUT_MODE = false
25+
DEFAULT_SERVE_MODE = false
26+
DEFAULT_SERVE_PORT = 4000
2327
DEFAULT_VERBOSE = false
2428
DEFAULT_LAYOUT_NAME = "default"
2529
DEFAULT_STYLE_NAME = "default"
@@ -34,6 +38,8 @@ class Config
3438
def initialize(options = {})
3539
@stdin_mode = options[:stdin_mode] if options.key?(:stdin_mode)
3640
@stdout_mode = options[:stdout_mode] if options.key?(:stdout_mode)
41+
@serve_mode = options[:serve_mode] if options.key?(:serve_mode)
42+
@serve_port = options[:serve_port] if options.key?(:serve_port)
3743
@help = options[:help]
3844
@verbose = options[:verbose] if options.key?(:verbose)
3945
@layout_name = options[:layout_name] || options[:template_name]
@@ -52,6 +58,8 @@ def to_h
5258
{
5359
stdin_mode: @stdin_mode,
5460
stdout_mode: @stdout_mode,
61+
serve_mode: @serve_mode,
62+
serve_port: @serve_port,
5563
help: @help,
5664
verbose: @verbose,
5765
layout_name: @layout_name,
@@ -100,6 +108,8 @@ def self.defaults
100108
Config.new({
101109
stdin_mode: DEFAULT_STDIN_MODE,
102110
stdout_mode: DEFAULT_STDOUT_MODE,
111+
serve_mode: DEFAULT_SERVE_MODE,
112+
serve_port: DEFAULT_SERVE_PORT,
103113
verbose: DEFAULT_VERBOSE,
104114
layout_name: DEFAULT_LAYOUT_NAME,
105115
style_name: DEFAULT_STYLE_NAME,

mint.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Gem::Specification.new do |s|
1717
s.files = Dir["{bin,config,lib,man}/**/*"] + [ "README.md", "Gemfile", "LICENSE" ]
1818
s.test_files = Dir["{features,spec}/**/*"]
1919

20+
s.add_dependency "webrick", "~> 1.8"
2021
s.add_dependency "redcarpet", "~> 3.6", ">= 3.6.1"
2122
s.add_dependency "sass-embedded", "~> 1.89", ">= 1.89.2"
2223
s.add_dependency "toml", "~> 0.3"

0 commit comments

Comments
 (0)