inertia_cable 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d3eef5ebaf526671cbdfa7efd381c2487f9ec19633a4dd6478bd3f399f262c1c
4
- data.tar.gz: 064c84423d1c5742e146404d410581d5610223e916082e94c71c214336ab3247
3
+ metadata.gz: 0e8633ccfd90aba7b7ae8a00203730d29755a65c1fc2f825ad9bde1225445387
4
+ data.tar.gz: 43f041337383fd933d2e7a6b2b9bafa17e8876f22b0bccc9a0b48ffd3b8bd007
5
5
  SHA512:
6
- metadata.gz: b16694b36fc8c42c3661a66d6ef94329b306a4b40a183f866993ed43460d3538ae120b68b5ec5115d83523cb252204fc34721c52fd93ba93e376a3246287caeb
7
- data.tar.gz: '01828651d1f2f96acbe07e85c5b02cd64153a7f7089a99e77c8d054e6694bd71c8514e6a8a7f864735ca0fac42f7c0358681194c0f7de914206f9ed3577e0799'
6
+ metadata.gz: ae7f293c9a1988f85bb2589119dd9cca2752ad4a81ebabc4c3bc2e656e6ee74d6808cc1068c7c026d8a9fa3aa47b95f55cf49e31681a4612b485d2d6633988f4
7
+ data.tar.gz: 239981663473b24453c8029787f2f869e9f39da6af222943b36adc1d6d58084d8c7f17fa5e5ca153e2635efe496d1c7415e2b8bdc25e59db07b7475d857d35be
@@ -1,40 +1,105 @@
1
1
  module InertiaCable
2
2
  module Generators
3
3
  class InstallGenerator < Rails::Generators::Base
4
- source_root File.expand_path("templates", __dir__)
5
-
6
4
  desc "Install InertiaCable into your Rails application"
7
5
 
8
- def copy_cable_setup
9
- template "cable_setup.ts", "app/javascript/channels/inertia_cable.ts"
6
+ def patch_inertia_entrypoint
7
+ entrypoint = detect_entrypoint
8
+ unless entrypoint
9
+ say "Could not find Inertia entrypoint — you'll need to add InertiaCableProvider manually.", :yellow
10
+ return
11
+ end
12
+
13
+ say "Patching #{entrypoint}...", :green
14
+
15
+ # Add import after the createInertiaApp import
16
+ if File.read(entrypoint).include?("@inertia-cable/react")
17
+ say " Import already present, skipping", :yellow
18
+ else
19
+ inject_into_file entrypoint, after: /^import \{ createInertiaApp \} from .+\n/ do
20
+ "import { InertiaCableProvider } from '@inertia-cable/react'\n"
21
+ end
22
+ end
23
+
24
+ content = File.read(entrypoint)
25
+
26
+ if content.include?("InertiaCableProvider")
27
+ say " InertiaCableProvider already present, skipping", :yellow
28
+ return
29
+ end
30
+
31
+ # Pattern 1: createElement style
32
+ # createRoot(el).render(createElement(App, props))
33
+ if content.match?(/createRoot\(el\)\.render\(\s*createElement\(App,\s*props\)\s*\)/)
34
+ gsub_file entrypoint,
35
+ /createRoot\(el\)\.render\(\s*createElement\(App,\s*props\)\s*\)/,
36
+ "createRoot(el).render(\n createElement(InertiaCableProvider, null, createElement(App, props)),\n )"
37
+ say " Wrapped render in InertiaCableProvider (createElement style)", :green
38
+
39
+ # Pattern 2: JSX with StrictMode
40
+ # createRoot(el).render(<StrictMode><App {...props} /></StrictMode>)
41
+ elsif content.match?(/createRoot\(el\)\.render\(\s*\n?\s*<StrictMode>\s*\n?\s*<App\s+\{\.\.\.props\}\s*\/>\s*\n?\s*<\/StrictMode>/)
42
+ gsub_file entrypoint,
43
+ /<StrictMode>\s*\n?\s*<App\s+\{\.\.\.props\}\s*\/>\s*\n?\s*<\/StrictMode>/,
44
+ "<StrictMode>\n <InertiaCableProvider>\n <App {...props} />\n </InertiaCableProvider>\n </StrictMode>"
45
+ say " Wrapped render in InertiaCableProvider (JSX + StrictMode style)", :green
46
+
47
+ # Pattern 3: JSX without StrictMode
48
+ # createRoot(el).render(<App {...props} />)
49
+ elsif content.match?(/createRoot\(el\)\.render\(\s*\n?\s*<App\s+\{\.\.\.props\}\s*\/>/)
50
+ gsub_file entrypoint,
51
+ /<App\s+\{\.\.\.props\}\s*\/>/,
52
+ "<InertiaCableProvider>\n <App {...props} />\n </InertiaCableProvider>"
53
+ say " Wrapped render in InertiaCableProvider (JSX style)", :green
54
+
55
+ else
56
+ say " Could not detect render pattern — add InertiaCableProvider manually.", :yellow
57
+ say " See: https://github.com/cole-robertson/inertia_cable#inertiaCableProvider"
58
+ end
10
59
  end
11
60
 
12
- def show_instructions
61
+ def show_next_steps
13
62
  say ""
14
63
  say "InertiaCable installed!", :green
15
64
  say ""
16
65
  say "Next steps:"
17
- say " 1. Add the npm package to your frontend:"
18
- say " npm install @inertia-cable/react @rails/actioncable"
19
66
  say ""
20
- say " 2. Ensure ActionCable is configured in config/cable.yml"
21
- say " (use redis adapter for production)"
67
+ say " 1. Install the npm package:"
68
+ say " npm install @inertia-cable/react @rails/actioncable"
22
69
  say ""
23
- say " 3. Add broadcasts_refreshes_to to your models:"
70
+ say " 2. Add broadcasts to your models:"
24
71
  say " class Message < ApplicationRecord"
25
72
  say " belongs_to :chat"
26
- say " broadcasts_refreshes_to :chat"
73
+ say " broadcasts_to :chat"
27
74
  say " end"
28
75
  say ""
29
- say " 4. Pass cable_stream prop from your controller:"
76
+ say " 3. Pass cable_stream prop from your controller:"
30
77
  say " render inertia: 'Chats/Show', props: {"
31
78
  say " cable_stream: inertia_cable_stream(chat)"
32
79
  say " }"
33
80
  say ""
34
- say " 5. Use the hook in your React component:"
35
- say " useInertiaCable(cable_stream, { only: ['messages'] })"
81
+ say " 4. Use the hook in your React component:"
82
+ say " import { useInertiaCable } from '@inertia-cable/react'"
83
+ say ""
84
+ say " const { connected } = useInertiaCable(cable_stream, { only: ['messages'] })"
36
85
  say ""
37
86
  end
87
+
88
+ private
89
+
90
+ def detect_entrypoint
91
+ candidates = %w[
92
+ app/frontend/entrypoints/inertia.ts
93
+ app/frontend/entrypoints/inertia.tsx
94
+ app/frontend/entrypoints/inertia.js
95
+ app/frontend/entrypoints/inertia.jsx
96
+ app/javascript/entrypoints/inertia.ts
97
+ app/javascript/entrypoints/inertia.tsx
98
+ app/javascript/entrypoints/inertia.js
99
+ app/javascript/entrypoints/inertia.jsx
100
+ ]
101
+ candidates.find { |f| File.exist?(Rails.root.join(f)) }
102
+ end
38
103
  end
39
104
  end
40
105
  end
@@ -1,3 +1,3 @@
1
1
  module InertiaCable
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inertia_cable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cole Robertson
@@ -75,7 +75,6 @@ files:
75
75
  - README.md
76
76
  - app/channels/inertia_cable/stream_channel.rb
77
77
  - lib/generators/inertia_cable/install/install_generator.rb
78
- - lib/generators/inertia_cable/install/templates/cable_setup.ts
79
78
  - lib/inertia_cable.rb
80
79
  - lib/inertia_cable/broadcast_job.rb
81
80
  - lib/inertia_cable/broadcastable.rb
@@ -1,7 +0,0 @@
1
- // InertiaCable - ActionCable setup for Inertia.js
2
- //
3
- // This file is auto-generated by `rails g inertia_cable:install`.
4
- // You can customize the cable URL below if needed.
5
-
6
- export { useInertiaCable } from '@inertia-cable/react'
7
- export { InertiaCableProvider } from '@inertia-cable/react'