From 67133d8ff062b6ba52576bd1388d9a66de2f9583 Mon Sep 17 00:00:00 2001 From: Carl Wiedemann Date: Thu, 4 Jan 2024 22:43:53 -0700 Subject: [PATCH] 3 - Add watch & version commands (#3) * 3 - Add watch & version commands * lint * lint * lint --- CHANGELOG.md | 6 +++++- README.md | 4 ++++ foresite.gemspec | 1 + lib/foresite.rb | 3 ++- lib/foresite/cli.rb | 28 ++++++++++++++++++++++++++++ lib/foresite/renderer.rb | 2 +- lib/foresite/version.rb | 2 +- spec/foresite/cli_spec.rb | 10 +++++----- 8 files changed, 47 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 744c289..d32b747 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index d3e9d18..73a8b7c 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,10 @@ In this example, the `index.html` will contain: ``` +### 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 diff --git a/foresite.gemspec b/foresite.gemspec index 81ace09..eb1f27f 100644 --- a/foresite.gemspec +++ b/foresite.gemspec @@ -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" diff --git a/lib/foresite.rb b/lib/foresite.rb index fbc3cb9..50c6e1a 100644 --- a/lib/foresite.rb +++ b/lib/foresite.rb @@ -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 diff --git a/lib/foresite/cli.rb b/lib/foresite/cli.rb index 4e8db52..ae0b7d4 100644 --- a/lib/foresite/cli.rb +++ b/lib/foresite/cli.rb @@ -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. @@ -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 diff --git a/lib/foresite/renderer.rb b/lib/foresite/renderer.rb index f951301..4f06509 100644 --- a/lib/foresite/renderer.rb +++ b/lib/foresite/renderer.rb @@ -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 diff --git a/lib/foresite/version.rb b/lib/foresite/version.rb index 853d8dd..6de3f37 100644 --- a/lib/foresite/version.rb +++ b/lib/foresite/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Foresite - VERSION = "1.2.0" + VERSION = "1.3.0" end diff --git a/spec/foresite/cli_spec.rb b/spec/foresite/cli_spec.rb index 9153b06..5f10325 100644 --- a/spec/foresite/cli_spec.rb +++ b/spec/foresite/cli_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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