Skip to content

Commit

Permalink
Add rake tasks for livereload enabling/disabling
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillplatonov committed Dec 4, 2021
1 parent eca549e commit 76212b3
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The JavaScript for Hotwire::Livereload is installed via asset pipeline, which is

1. Add `hotwire-livereload` gem to your Gemfile: `gem 'hotwire-livereload'`
2. Run `./bin/bundle install`
3. Run `./bin/rails hotwire_livereload:install`
3. Run `./bin/rails livereload:install`

## Configuration

Expand All @@ -25,6 +25,20 @@ Rails.application.configure do
end
```

## Disable livereload

To temporarily disable livereload use:
```bash
bin/rails livereload:disable
```

To re-enable:
```bash
bin/rails livereload:enable
```

No server restart is required. Disabling is managed by `tmp/livereload-disabled.txt` file.

## Development

To get started:
Expand Down
1 change: 1 addition & 0 deletions lib/hotwire/livereload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

module Hotwire
module Livereload
DISABLE_FILE = "tmp/livereload-disabled.txt"
end
end
2 changes: 1 addition & 1 deletion lib/hotwire/livereload/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Engine < ::Rails::Engine
config.after_initialize do |app|
if Rails.env.development?
@listener = Listen.to(*app.config.hotwire_livereload.listen_paths) do |modified, added, removed|
unless File.exists?(Rails.root.join("tmp/livereload-disable.txt")
unless File.exists?(DISABLE_FILE)
if (modified.any? || removed.any? || added.any?)
content = { modified: modified, removed: removed, added: added }
ActionCable.server.broadcast("hotwire-reload", content)
Expand Down
6 changes: 0 additions & 6 deletions lib/tasks/hotwire/livereload/install.rake

This file was deleted.

21 changes: 21 additions & 0 deletions lib/tasks/livereload_tasks.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace :livereload do
desc "Install Hotwire::Livereload into the app"
task :install do
system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../install/install.rb", __dir__)}"
end

desc "Disable Hotwire::Livereload"
task :disable do
FileUtils.mkdir_p("tmp")
FileUtils.touch Hotwire::Livereload::DISABLE_FILE
puts "Livereload disabled."
end

desc "Enable Hotwire::Livereload"
task :enable do
if File.exist?(Hotwire::Livereload::DISABLE_FILE)
File.delete Hotwire::Livereload::DISABLE_FILE
end
puts "Livereload enabled."
end
end

0 comments on commit 76212b3

Please sign in to comment.