Skip to content

Commit f959e64

Browse files
ivorisoutdoorsMatthew Loberg
authored andcommitted
Add --config and --source option to draft command
In some Jekyll setups, the site assets are in a sub-directory. The drafts command, does not process any configuration files or allow to set the site source. Add these options to the draft command and update the FileCreator class to take an optional root that will be prepended to the file.path.
1 parent 8e1db4b commit f959e64

File tree

4 files changed

+55
-8
lines changed

4 files changed

+55
-8
lines changed

lib/jekyll-compose/file_creator.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
module Jekyll
22
module Compose
33
class FileCreator
4-
attr_reader :file, :force
5-
def initialize(fileInfo, force = false)
4+
attr_reader :file, :force, :root
5+
def initialize(fileInfo, force = false, root = nil)
66
@file = fileInfo
77
@force = force
8+
@root = root
89
end
910

1011
def create!
@@ -16,20 +17,25 @@ def create!
1617
private
1718

1819
def validate_should_write!
19-
raise ArgumentError.new("A #{file.resource_type} already exists at #{file.path}") if File.exist?(file.path) and !force
20+
raise ArgumentError.new("A #{file.resource_type} already exists at #{file_path}") if File.exist?(file_path) and !force
2021
end
2122

2223
def ensure_directory_exists
23-
dir = File.dirname file.path
24+
dir = File.dirname file_path
2425
Dir.mkdir(dir) unless Dir.exist?(dir)
2526
end
2627

2728
def write_file
28-
File.open(file.path, "w") do |f|
29+
File.open(file_path, "w") do |f|
2930
f.puts(file.content)
3031
end
3132

32-
puts "New #{file.resource_type} created at #{file.path}."
33+
puts "New #{file.resource_type} created at #{file_path}."
34+
end
35+
36+
def file_path
37+
return file.path if root.nil? or root.empty?
38+
return File.join(root, file.path)
3339
end
3440
end
3541
end

lib/jekyll/commands/draft.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,24 @@ def self.options
1616
[
1717
['extension', '-x EXTENSION', '--extension EXTENSION', 'Specify the file extension'],
1818
['layout', '-l LAYOUT', '--layout LAYOUT', "Specify the draft layout"],
19-
['force', '-f', '--force', 'Overwrite a draft if it already exists']
19+
['force', '-f', '--force', 'Overwrite a draft if it already exists'],
20+
['config', '--config CONFIG_FILE[,CONFIG_FILE2,...]', Array, 'Custom configuration file'],
21+
['source', '-s', '--source SOURCE', 'Custom source directory'],
2022
]
2123
end
2224

2325

2426
def self.process(args = [], options = {})
27+
config = configuration_from_options(options)
28+
2529
params = Compose::ArgParser.new args, options
2630
params.validate!
2731

2832
draft = DraftFileInfo.new params
2933

30-
Compose::FileCreator.new(draft, params.force?).create!
34+
root = config['source'].gsub(/^#{Regexp.quote(Dir.pwd)}/, '')
35+
36+
Compose::FileCreator.new(draft, params.force?, root).create!
3137
end
3238

3339
class DraftFileInfo < Compose::FileInfo

spec/draft_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,37 @@
7373
expect(File.read(path)).to match(/layout: post/)
7474
end
7575
end
76+
77+
context 'when a configuration file exists' do
78+
let(:config) { source_dir('_config.yml') }
79+
let(:drafts_dir) { Pathname.new source_dir(File.join('site', '_drafts')) }
80+
81+
before(:each) do
82+
File.open(config, 'w') do |f|
83+
f.write(%{
84+
source: site
85+
})
86+
end
87+
end
88+
89+
after(:each) do
90+
FileUtils.rm(config)
91+
end
92+
93+
it 'should use source directory set by config' do
94+
expect(path).not_to exist
95+
capture_stdout { described_class.process(args) }
96+
expect(path).to exist
97+
end
98+
end
99+
100+
context 'when source option is set' do
101+
let(:drafts_dir) { Pathname.new source_dir(File.join('site', '_drafts')) }
102+
103+
it 'should use source directory set by command line option' do
104+
expect(path).not_to exist
105+
capture_stdout { described_class.process(args, 'source' => 'site') }
106+
expect(path).to exist
107+
end
108+
end
76109
end

spec/spec_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
Kernel.srand config.seed
2828

29+
Jekyll.logger.log_level = :error
30+
2931
###
3032
### Helper methods
3133
###

0 commit comments

Comments
 (0)