Skip to content

Commit 97ce9b6

Browse files
alzeihjekyllbot
authored andcommitted
Check for overwrite when publishing or unpublishing (#59)
Merge pull request 59
1 parent da14e7c commit 97ce9b6

File tree

6 files changed

+76
-20
lines changed

6 files changed

+76
-20
lines changed

lib/jekyll-compose/file_mover.rb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,44 @@
33
module Jekyll
44
module Compose
55
class FileMover
6-
attr_reader :movement, :root
7-
def initialize(movement, root = nil)
6+
attr_reader :force, :movement, :root
7+
def initialize(movement, force = false, root = nil)
88
@movement = movement
9+
@force = force
910
@root = root
1011
end
1112

12-
def resource_type
13+
def resource_type_from
14+
"file"
15+
end
16+
17+
def resource_type_to
1318
"file"
1419
end
1520

1621
def move
1722
validate_source
23+
validate_should_write!
1824
ensure_directory_exists
1925
move_file
2026
end
2127

2228
def validate_source
23-
raise ArgumentError, "There was no #{resource_type} found at '#{from}'." unless File.exist? from
29+
raise ArgumentError, "There was no #{resource_type_from} found at '#{from}'." unless File.exist? from
2430
end
2531

2632
def ensure_directory_exists
2733
dir = File.dirname to
2834
Dir.mkdir(dir) unless Dir.exist?(dir)
2935
end
3036

37+
def validate_should_write!
38+
raise ArgumentError, "A #{resource_type_to} already exists at #{to}" if File.exist?(to) && !force
39+
end
40+
3141
def move_file
3242
FileUtils.mv(from, to)
33-
puts "#{resource_type.capitalize} #{from} was moved to #{to}"
43+
puts "#{resource_type_from.capitalize} #{from} was moved to #{to}"
3444
end
3545

3646
private

lib/jekyll-compose/movement_arg_parser.rb

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@
22

33
module Jekyll
44
module Compose
5-
class MovementArgParser
6-
attr_reader :args, :options, :config
7-
def initialize(args, options)
8-
@args = args
9-
@options = options
10-
@config = Jekyll.configuration(options)
11-
end
5+
class MovementArgParser < ArgParser
126

137
def validate!
148
raise ArgumentError, "You must specify a #{resource_type} path." if args.empty?
@@ -17,10 +11,6 @@ def validate!
1711
def path
1812
args.join " "
1913
end
20-
21-
def source
22-
config["source"].gsub(%r!^#{Regexp.quote(Dir.pwd)}!, "")
23-
end
2414
end
2515
end
2616
end

lib/jekyll/commands/publish.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def self.init_with_program(prog)
1010

1111
c.option "date", "-d DATE", "--date DATE", "Specify the post date"
1212
c.option "config", "--config CONFIG_FILE[,CONFIG_FILE2,...]", Array, "Custom configuration file"
13+
c.option "force", "-f", "--force", "Overwrite a post if it already exists"
1314
c.option "source", "-s", "--source SOURCE", "Custom source directory"
1415

1516
c.action do |args, options|
@@ -24,7 +25,7 @@ def self.process(args = [], options = {})
2425

2526
movement = DraftMovementInfo.new params
2627

27-
mover = DraftMover.new movement, params.source
28+
mover = DraftMover.new movement, params.force?, params.source
2829
mover.move
2930
end
3031
end
@@ -60,9 +61,12 @@ def to
6061
end
6162

6263
class DraftMover < Compose::FileMover
63-
def resource_type
64+
def resource_type_from
6465
"draft"
6566
end
67+
def resource_type_to
68+
"post"
69+
end
6670
end
6771
end
6872
end

lib/jekyll/commands/unpublish.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def self.init_with_program(prog)
99
c.description "Moves a post back into the _drafts directory"
1010

1111
c.option "config", "--config CONFIG_FILE[,CONFIG_FILE2,...]", Array, "Custom configuration file"
12+
c.option "force", "-f", "--force", "Overwrite a draft if it already exists"
1213
c.option "source", "-s", "--source SOURCE", "Custom source directory"
1314

1415
c.action do |args, options|
@@ -23,7 +24,7 @@ def self.process(args = [], options = {})
2324

2425
movement = PostMovementInfo.new params
2526

26-
mover = PostMover.new movement, params.source
27+
mover = PostMover.new movement, params.force?, params.source
2728
mover.move
2829
end
2930
end
@@ -54,9 +55,12 @@ def to
5455
end
5556

5657
class PostMover < Compose::FileMover
57-
def resource_type
58+
def resource_type_from
5859
"post"
5960
end
61+
def resource_type_to
62+
"draft"
63+
end
6064
end
6165
end
6266
end

spec/publish_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,30 @@
7474
}).to raise_error("There was no draft found at '_drafts/i-do-not-exist.markdown'.")
7575
end
7676

77+
context "when the post already exists" do
78+
let(:args) { ["_drafts/#{draft_to_publish}"] }
79+
80+
before(:each) do
81+
FileUtils.touch post_path
82+
end
83+
84+
it "raises an error" do
85+
expect(lambda {
86+
capture_stdout { described_class.process(args) }
87+
}).to raise_error("A post already exists at _posts/#{post_filename}")
88+
expect(draft_path).to exist
89+
expect(post_path).to exist
90+
end
91+
92+
it "overwrites if --force is given" do
93+
expect(lambda {
94+
capture_stdout { described_class.process(args, "force" => true) }
95+
}).not_to raise_error
96+
expect(draft_path).not_to exist
97+
expect(post_path).to exist
98+
end
99+
end
100+
77101
context "when a configuration file exists" do
78102
let(:config) { source_dir("_config.yml") }
79103
let(:drafts_dir) { Pathname.new source_dir("site", "_drafts") }

spec/unpublish_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,30 @@
5959
}).to raise_error("There was no post found at '#{weird_path}'.")
6060
end
6161

62+
context "when the draft already exists" do
63+
let(:args) { ["_posts/#{post_filename}"] }
64+
65+
before(:each) do
66+
FileUtils.touch draft_path
67+
end
68+
69+
it "raises an error" do
70+
expect(lambda {
71+
capture_stdout { described_class.process(args) }
72+
}).to raise_error("A draft already exists at _drafts/#{post_name}")
73+
expect(draft_path).to exist
74+
expect(post_path).to exist
75+
end
76+
77+
it "overwrites if --force is given" do
78+
expect(lambda {
79+
capture_stdout { described_class.process(args, "force" => true) }
80+
}).not_to raise_error
81+
expect(draft_path).to exist
82+
expect(post_path).not_to exist
83+
end
84+
end
85+
6286
context "when a configuration file exists" do
6387
let(:config) { source_dir("_config.yml") }
6488
let(:drafts_dir) { Pathname.new(source_dir("site", "_drafts")) }

0 commit comments

Comments
 (0)