Skip to content

Commit

Permalink
3 - Add watch & version commands (#3)
Browse files Browse the repository at this point in the history
* 3 - Add watch & version commands

* lint

* lint

* lint
  • Loading branch information
carlwiedemann authored Jan 5, 2024
1 parent 74d8fe6 commit 67133d8
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 9 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]

TBD
## [1.3.0] - 2024-01-05

### Added

[#3](https://github.com/carlwiedemann/foresite/pull/3) version & watch commands (not sure how to test these best right now).

## [1.2.0] - 2023-02-11

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ In this example, the `index.html` will contain:
</ul>
```

### 5. Watch files and build automatically

Run `foresite watch` to detect changes to markdown or ERB files, build will run automatically. Useful for previewing content locally.

## Development

1. Clone
Expand Down
1 change: 1 addition & 0 deletions foresite.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Gem::Specification.new do |spec|
spec.add_dependency "kramdown", "~> 2.4"
spec.add_dependency "thor", "~> 1.2"
spec.add_dependency "zeitwerk", "~> 2.6"
spec.add_dependency "filewatcher", "~> 2.1"

spec.add_development_dependency "rspec", "~> 3.2"
spec.add_development_dependency "standard", "~> 1.3"
Expand Down
3 changes: 2 additions & 1 deletion lib/foresite.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# frozen_string_literal: true

require "erb"
require "thor"
require "filewatcher"
require "kramdown"
require "thor"
require "zeitwerk"

loader = Zeitwerk::Loader.for_gem
Expand Down
28 changes: 28 additions & 0 deletions lib/foresite/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ def self.exit_on_failure?
true
end

desc "version", "Displays version"
def version
$stdout.puts(Foresite::VERSION)
end

desc "init", "Initializes foresite in current directory"
long_desc <<-LONGDESC
Initializes foresite in the current directory.
Expand Down Expand Up @@ -117,5 +122,28 @@ def build

$stdout.puts("Created #{Foresite.relative_path(index_html_path)}")
end

desc "watch", "Watches markdown and templates files and runs `build` when they change"
long_desc <<-LONGDESC
See `build` help for more information
LONGDESC

# @todo How might we test this?
def watch
dirs_to_watch = [
Foresite::DIRNAME_MARKDOWN,
Foresite::DIRNAME_ERB
]

$stdout.puts("Watching #{dirs_to_watch.map { "./#{_1}" }.join(", ")} for changes... (Ctrl+C to exit)")

Filewatcher.new(dirs_to_watch).watch do |changes|
changes.each do |filename, event|
$stdout.puts("#{File.basename(filename)} #{event}")
end

build
end
end
end
end
2 changes: 1 addition & 1 deletion lib/foresite/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def initialize(path, vars)
@path = path
vars.each do |k, v|
if k.is_a?(Symbol)
instance_variable_set("@#{k}".to_sym, v)
instance_variable_set(:"@#{k}", v)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/foresite/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Foresite
VERSION = "1.2.0"
VERSION = "1.3.0"
end
10 changes: 5 additions & 5 deletions spec/foresite/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
expected_stderr = ForesiteRSpec.cli_line("Nonexistent directory foo")
expected_exit_code = 1

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

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

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

Expand Down Expand Up @@ -80,7 +80,7 @@
exptected_stderr = ForesiteRSpec.cli_line("Missing subdirectories, try running `foresite init`")
expected_exit_code = 1

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

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

expect { Foresite::Cli.new.invoke(:build) }.to output(exptected_stderr).to_stderr \
expect { Foresite::Cli.new.invoke(:build) }.to output(exptected_stderr).to_stderr
.and(raise_error(SystemExit) { |error| expect(error.status).to eq(expected_exit_code) })
end
end
Expand Down

0 comments on commit 67133d8

Please sign in to comment.