Skip to content

Commit

Permalink
Add support for collections_dir configuration (#74)
Browse files Browse the repository at this point in the history
Merge pull request 74
  • Loading branch information
ashmaroli authored and jekyllbot committed Oct 31, 2018
1 parent 1c07f92 commit 942d46e
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 17 deletions.
3 changes: 2 additions & 1 deletion lib/jekyll-compose/arg_parser.rb
Expand Up @@ -33,7 +33,8 @@ def force?
end

def source
config["source"].gsub(%r!^#{Regexp.quote(Dir.pwd)}!, "")
File.join(config["source"], config["collections_dir"])
.gsub(%r!^#{Regexp.quote(Dir.pwd)}/*!, "")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/jekyll-compose/file_creator.rb
Expand Up @@ -30,7 +30,7 @@ def validate_should_write!

def ensure_directory_exists
dir = File.dirname file_path
Dir.mkdir(dir) unless Dir.exist?(dir)
FileUtils.mkdir_p(dir) unless Dir.exist?(dir)
end

def write_file
Expand Down
2 changes: 1 addition & 1 deletion lib/jekyll-compose/movement_arg_parser.rb
Expand Up @@ -8,7 +8,7 @@ def validate!
end

def path
args.join " "
File.join(source, args.join(" ")).sub(%r!\A/!, "")
end
end
end
Expand Down
23 changes: 23 additions & 0 deletions spec/draft_spec.rb
Expand Up @@ -139,6 +139,29 @@
end
end
end

context "and collections_dir is set" do
let(:collections_dir) { "my_collections" }
let(:drafts_dir) { Pathname.new source_dir("site", collections_dir, "_drafts") }
let(:config_data) do
%(
source: site
collections_dir: #{collections_dir}
)
end

it "should create drafts at the correct location" do
expect(path).not_to exist
capture_stdout { described_class.process(args) }
expect(path).to exist
end

it "should write a helpful message when successful" do
output = capture_stdout { described_class.process(args) }
generated_path = File.join("site", collections_dir, "_drafts", "a-test-post.md").cyan
expect(output).to include("New draft created at #{generated_path}")
end
end
end

context "when source option is set" do
Expand Down
23 changes: 23 additions & 0 deletions spec/post_spec.rb
Expand Up @@ -150,6 +150,29 @@
end
end
end

context "and collections_dir is set" do
let(:collections_dir) { "my_collections" }
let(:posts_dir) { Pathname.new source_dir("site", collections_dir, "_posts") }
let(:config_data) do
%(
source: site
collections_dir: #{collections_dir}
)
end

it "should create posts at the correct location" do
expect(path).not_to exist
capture_stdout { described_class.process(args) }
expect(path).to exist
end

it "should write a helpful message when successful" do
output = capture_stdout { described_class.process(args) }
generated_path = File.join("site", collections_dir, "_posts", filename).cyan
expect(output).to include("New post created at #{generated_path}")
end
end
end

context "when source option is set" do
Expand Down
13 changes: 6 additions & 7 deletions spec/publish_spec.rb
Expand Up @@ -102,14 +102,15 @@
let(:config) { source_dir("_config.yml") }
let(:drafts_dir) { Pathname.new source_dir("site", "_drafts") }
let(:posts_dir) { Pathname.new source_dir("site", "_posts") }

let(:args) { ["site/_drafts/#{draft_to_publish}"] }
let(:config_data) do
%(
source: site
)
end

before(:each) do
File.open(config, "w") do |f|
f.write(%(
source: site
))
f.write(config_data)
end
end

Expand All @@ -129,8 +130,6 @@
let(:drafts_dir) { Pathname.new source_dir("site", "_drafts") }
let(:posts_dir) { Pathname.new source_dir("site", "_posts") }

let(:args) { ["site/_drafts/#{draft_to_publish}"] }

it "should use source directory set by command line option" do
expect(post_path).not_to exist
expect(draft_path).to exist
Expand Down
39 changes: 32 additions & 7 deletions spec/unpublish_spec.rb
Expand Up @@ -87,14 +87,15 @@
let(:config) { source_dir("_config.yml") }
let(:drafts_dir) { Pathname.new(source_dir("site", "_drafts")) }
let(:posts_dir) { Pathname.new(source_dir("site", "_posts")) }

let(:args) { ["site/_posts/#{post_filename}"] }
let(:config_data) do
%(
source: site
)
end

before(:each) do
File.open(config, "w") do |f|
f.write(%(
source: site
))
f.write(config_data)
end
end

Expand All @@ -109,14 +110,38 @@
expect(post_path).not_to exist
expect(draft_path).to exist
end

context "and collections_dir is set" do
let(:collections_dir) { "my_collections" }
let(:drafts_dir) { Pathname.new(source_dir("site", collections_dir, "_drafts")) }
let(:posts_dir) { Pathname.new(source_dir("site", collections_dir, "_posts")) }
let(:config_data) do
%(
source: site
collections_dir: #{collections_dir}
)
end

it "should move posts to the correct location" do
expect(post_path).to exist
expect(draft_path).not_to exist
capture_stdout { described_class.process(args) }
expect(draft_path).to exist
end

it "should write a helpful message when successful" do
output = capture_stdout { described_class.process(args) }
post_filepath = File.join("site", collections_dir, "_posts", post_filename)
draft_filepath = File.join("site", collections_dir, "_drafts", post_name)
expect(output).to include("Post #{post_filepath} was moved to #{draft_filepath}")
end
end
end

context "when source option is set" do
let(:drafts_dir) { Pathname.new(source_dir("site", "_drafts")) }
let(:posts_dir) { Pathname.new(source_dir("site", "_posts")) }

let(:args) { ["site/_posts/#{post_filename}"] }

it "should use source directory set by command line option" do
expect(post_path).to exist
expect(draft_path).not_to exist
Expand Down

0 comments on commit 942d46e

Please sign in to comment.