Skip to content

Commit 67133d8

Browse files
3 - Add watch & version commands (#3)
* 3 - Add watch & version commands * lint * lint * lint
1 parent 74d8fe6 commit 67133d8

File tree

8 files changed

+47
-9
lines changed

8 files changed

+47
-9
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1313

1414
## [Unreleased]
1515

16-
TBD
16+
## [1.3.0] - 2024-01-05
17+
18+
### Added
19+
20+
[#3](https://github.com/carlwiedemann/foresite/pull/3) version & watch commands (not sure how to test these best right now).
1721

1822
## [1.2.0] - 2023-02-11
1923

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ In this example, the `index.html` will contain:
107107
</ul>
108108
```
109109

110+
### 5. Watch files and build automatically
111+
112+
Run `foresite watch` to detect changes to markdown or ERB files, build will run automatically. Useful for previewing content locally.
113+
110114
## Development
111115

112116
1. Clone

foresite.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Gem::Specification.new do |spec|
3838
spec.add_dependency "kramdown", "~> 2.4"
3939
spec.add_dependency "thor", "~> 1.2"
4040
spec.add_dependency "zeitwerk", "~> 2.6"
41+
spec.add_dependency "filewatcher", "~> 2.1"
4142

4243
spec.add_development_dependency "rspec", "~> 3.2"
4344
spec.add_development_dependency "standard", "~> 1.3"

lib/foresite.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# frozen_string_literal: true
22

33
require "erb"
4-
require "thor"
4+
require "filewatcher"
55
require "kramdown"
6+
require "thor"
67
require "zeitwerk"
78

89
loader = Zeitwerk::Loader.for_gem

lib/foresite/cli.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ def self.exit_on_failure?
1212
true
1313
end
1414

15+
desc "version", "Displays version"
16+
def version
17+
$stdout.puts(Foresite::VERSION)
18+
end
19+
1520
desc "init", "Initializes foresite in current directory"
1621
long_desc <<-LONGDESC
1722
Initializes foresite in the current directory.
@@ -117,5 +122,28 @@ def build
117122

118123
$stdout.puts("Created #{Foresite.relative_path(index_html_path)}")
119124
end
125+
126+
desc "watch", "Watches markdown and templates files and runs `build` when they change"
127+
long_desc <<-LONGDESC
128+
See `build` help for more information
129+
LONGDESC
130+
131+
# @todo How might we test this?
132+
def watch
133+
dirs_to_watch = [
134+
Foresite::DIRNAME_MARKDOWN,
135+
Foresite::DIRNAME_ERB
136+
]
137+
138+
$stdout.puts("Watching #{dirs_to_watch.map { "./#{_1}" }.join(", ")} for changes... (Ctrl+C to exit)")
139+
140+
Filewatcher.new(dirs_to_watch).watch do |changes|
141+
changes.each do |filename, event|
142+
$stdout.puts("#{File.basename(filename)} #{event}")
143+
end
144+
145+
build
146+
end
147+
end
120148
end
121149
end

lib/foresite/renderer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def initialize(path, vars)
1414
@path = path
1515
vars.each do |k, v|
1616
if k.is_a?(Symbol)
17-
instance_variable_set("@#{k}".to_sym, v)
17+
instance_variable_set(:"@#{k}", v)
1818
end
1919
end
2020
end

lib/foresite/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module Foresite
4-
VERSION = "1.2.0"
4+
VERSION = "1.3.0"
55
end

spec/foresite/cli_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
expected_stderr = ForesiteRSpec.cli_line("Nonexistent directory foo")
1010
expected_exit_code = 1
1111

12-
expect { Foresite::Cli.new.invoke(:init) }.to output(expected_stderr).to_stderr \
12+
expect { Foresite::Cli.new.invoke(:init) }.to output(expected_stderr).to_stderr
1313
.and(raise_error(SystemExit) { |error| expect(error.status).to eq(expected_exit_code) })
1414
end
1515

@@ -19,7 +19,7 @@
1919
expected_stderr = ForesiteRSpec.cli_line("Cannot write to directory /usr")
2020
expected_exit_code = 1
2121

22-
expect { Foresite::Cli.new.invoke(:init) }.to output(expected_stderr).to_stderr \
22+
expect { Foresite::Cli.new.invoke(:init) }.to output(expected_stderr).to_stderr
2323
.and(raise_error(SystemExit) { |error| expect(error.status).to eq(expected_exit_code) })
2424
end
2525

@@ -80,7 +80,7 @@
8080
exptected_stderr = ForesiteRSpec.cli_line("Missing subdirectories, try running `foresite init`")
8181
expected_exit_code = 1
8282

83-
expect { Foresite::Cli.new.invoke(:touch, ["something"]) }.to output(exptected_stderr).to_stderr \
83+
expect { Foresite::Cli.new.invoke(:touch, ["something"]) }.to output(exptected_stderr).to_stderr
8484
.and(raise_error(SystemExit) { |error| expect(error.status).to eq(expected_exit_code) })
8585
end
8686
end
@@ -220,7 +220,7 @@
220220
exptected_stderr = ForesiteRSpec.cli_line("Missing subdirectories, try running `foresite init`")
221221
expected_exit_code = 1
222222

223-
expect { Foresite::Cli.new.invoke(:build) }.to output(exptected_stderr).to_stderr \
223+
expect { Foresite::Cli.new.invoke(:build) }.to output(exptected_stderr).to_stderr
224224
.and(raise_error(SystemExit) { |error| expect(error.status).to eq(expected_exit_code) })
225225
end
226226
end
@@ -235,7 +235,7 @@
235235
exptected_stderr = ForesiteRSpec.cli_line("No markdown files, try running `foresite touch`")
236236
expected_exit_code = 1
237237

238-
expect { Foresite::Cli.new.invoke(:build) }.to output(exptected_stderr).to_stderr \
238+
expect { Foresite::Cli.new.invoke(:build) }.to output(exptected_stderr).to_stderr
239239
.and(raise_error(SystemExit) { |error| expect(error.status).to eq(expected_exit_code) })
240240
end
241241
end

0 commit comments

Comments
 (0)