Class: InstantRecord::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/instant_record/install/install_generator.rb

Overview

Prepares a Rails app for InstantRecord's browser runtime: wasmify environment, PGlite database config, and a PWA shell wired with the InstantRecord sync driver.

Instance Method Summary collapse

Instance Method Details

#configure_pglite_databaseObject



19
20
21
22
23
# File 'lib/generators/instant_record/install/install_generator.rb', line 19

def configure_pglite_database
  gsub_file "config/database.yml", '{ "nulldb" }', '{ "pglite" }'
  inject_into_file "config/database.yml", "  js_interface: pglite4rails\n",
    after: /^wasm:\n  adapter: .*\n/
end

#generate_pwa_shellObject



15
16
17
# File 'lib/generators/instant_record/install/install_generator.rb', line 15

def generate_pwa_shell
  rake "wasmify:pwa"
end

#install_sync_driverObject



25
26
27
28
29
30
31
# File 'lib/generators/instant_record/install/install_generator.rb', line 25

def install_sync_driver
  copy_file "rails.sw.js", "pwa/rails.sw.js", force: true
  copy_file "database.js", "pwa/database.js", force: true
  # Auto-booting splash: registers the worker and reloads into the app.
  # (wasmify's boot.html stays available as a debug page.)
  copy_file "index.html", "pwa/index.html", force: true
end

#run_wasmify_installObject



11
12
13
# File 'lib/generators/instant_record/install/install_generator.rb', line 11

def run_wasmify_install
  rake "wasmify:install"
end

#show_next_stepsObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/generators/instant_record/install/install_generator.rb', line 39

def show_next_steps
  say <<~MSG

    InstantRecord is set up. Next:

      1. Mark the gems your app needs in the browser with `group: [:default, :wasm]` in the Gemfile
      2. bin/rails db:migrate                # engine tables (outbox, change log, ...)
      3. bin/rails instant_record:build      # compile + pack the browser bundle
      4. cd pwa && yarn install && yarn dev  # boot it

  MSG
end

#use_pglite_in_pwaObject



33
34
35
36
37
# File 'lib/generators/instant_record/install/install_generator.rb', line 33

def use_pglite_in_pwa
  gsub_file "pwa/package.json", /"wasmify-rails": "\^[\d.]+"/, '"wasmify-rails": "^0.2.3"'
  gsub_file "pwa/package.json", %r{"@sqlite\.org/sqlite-wasm": "[^"]+"}, '"@electric-sql/pglite": "^0.3.0"'
  gsub_file "pwa/vite.config.js", %r{exclude: \["@sqlite\.org/sqlite-wasm"\]}, 'exclude: ["@electric-sql/pglite"]'
end