Skip to content

Commit 4aa7fb1

Browse files
authored
Merge pull request #1306 from G-Rath/use-package_json
feat: support other js package managers
2 parents 7f124c7 + d29a7a4 commit 4aa7fb1

13 files changed

+89
-25
lines changed

.github/workflows/ruby.yml

+16-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ jobs:
2020
matrix:
2121
ruby: [2.7]
2222
runs-on: ubuntu-latest
23+
env:
24+
PACKAGE_JSON_FALLBACK_MANAGER: yarn_classic
2325
steps:
2426
- uses: actions/checkout@v4
2527
with:
@@ -56,6 +58,15 @@ jobs:
5658
strategy:
5759
fail-fast: false
5860
matrix:
61+
js_package_manager:
62+
- name: npm
63+
installer: npm
64+
- name: yarn_classic
65+
installer: yarn
66+
- name: pnpm
67+
installer: pnpm
68+
- name: bun
69+
installer: bun
5970
ruby: [2.7]
6071
gemfile:
6172
# These have shakapacker:
@@ -71,12 +82,14 @@ jobs:
7182
# Workaround b/c upgrading Minitest broke some mocking expectations
7283
# having to do with automatic kwarg splatting
7384
MT_KWARGS_HACK: 1
85+
PACKAGE_JSON_FALLBACK_MANAGER: ${{ matrix.js_package_manager.name }}
86+
SHAKAPACKER_USE_PACKAGE_JSON_GEM: true
7487
steps:
7588
- uses: actions/checkout@v4
7689
with:
7790
persist-credentials: false
7891
- uses: actions/setup-node@v3
79-
- run: npm -g install yalc
92+
- run: npm -g install yalc ${{ matrix.js_package_manager.installer }}
8093
- run: yalc publish
8194
- name: Save root node_modules to cache
8295
uses: actions/cache@v3
@@ -92,14 +105,15 @@ jobs:
92105
with:
93106
bundler: 2.4.9
94107
ruby-version: ${{ matrix.ruby }}
108+
- run: ./test/bin/create-fake-js-package-managers ${{ matrix.js_package_manager.installer }}
95109
- name: Save dummy app ruby gems to cache
96110
uses: actions/cache@v3
97111
with:
98112
path: test/dummy/vendor/bundle
99113
key: dummy-app-gem-cache-${{ hashFiles('${{ github.workspace }}/gemfiles/${{ matrix.gemfile }}.gemfile.lock') }}
100114
- name: Install Ruby Gems for dummy app
101115
run: bundle lock --add-platform 'x86_64-linux' && bundle check --path=test/dummy/vendor/bundle || bundle _2.4.9_ install --frozen --path=test/dummy/vendor/bundle --jobs=4 --retry=3
102-
- run: cd test/dummy && yalc add react_ujs && yarn
116+
- run: cd test/dummy && yalc add react_ujs && ${{ matrix.js_package_manager.installer }} install
103117
- run: bundle exec rake test
104118
env:
105119
NODE_OPTIONS: --openssl-legacy-provider

Appraisals

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ end
1616

1717
appraise 'shakapacker' do
1818
gem 'rails', '~> 7.0.x'
19-
gem 'shakapacker', '7.0.2'
19+
gem 'shakapacker', '7.2.0'
2020
end
2121

2222
appraise 'base' do

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ _Please add entries here for your pull requests that are not yet released._
1111

1212
#### Changed
1313
- Make es6 and ts usable at same time. #1299
14+
- Support other JS package managers using `package_json` gem #1306
1415

1516
## [3.1.1] - 2023-08-16
1617

Gemfile.lock

+2-5
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,16 @@ GEM
102102
matrix (0.4.2)
103103
method_source (1.0.0)
104104
mini_mime (1.0.2)
105-
mini_portile2 (2.8.4)
106105
minitest (5.17.0)
107106
nenv (0.3.0)
108-
nokogiri (1.14.3)
109-
mini_portile2 (~> 2.8.0)
110-
racc (~> 1.4)
111107
nokogiri (1.14.3-x86_64-darwin)
112108
racc (~> 1.4)
113109
nokogiri (1.14.3-x86_64-linux)
114110
racc (~> 1.4)
115111
notiffany (0.1.3)
116112
nenv (~> 0.1)
117113
shellany (~> 0.0)
114+
package_json (0.1.0)
118115
pry (0.14.2)
119116
coderay (~> 1.1)
120117
method_source (~> 1.0)
@@ -166,7 +163,6 @@ GEM
166163
zeitwerk (2.6.6)
167164

168165
PLATFORMS
169-
ruby
170166
x86_64-darwin-20
171167
x86_64-linux
172168

@@ -182,6 +178,7 @@ DEPENDENCIES
182178
guard-minitest
183179
jbuilder
184180
listen (~> 3.0.0)
181+
package_json
185182
pry-byebug
186183
react-rails!
187184
selenium-webdriver

Rakefile

+7-12
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,26 @@ end
88

99
Bundler::GemHelper.install_tasks
1010

11+
require "package_json"
12+
1113
def copy_react_asset(webpack_file, destination_file)
1214
full_webpack_path = File.expand_path("../react-builds/build/#{webpack_file}", __FILE__)
1315
full_destination_path = File.expand_path("../lib/assets/react-source/#{destination_file}", __FILE__)
1416
FileUtils.cp(full_webpack_path, full_destination_path)
1517
end
1618

17-
# Move to `dirname` and execute `yarn {cmd}`
18-
def yarn_run_in(dirname, cmd)
19-
Dir.chdir(dirname) do
20-
`yarn #{cmd}`
21-
end
22-
end
23-
2419
namespace :react do
2520
desc "Run the JS build process to put files in the gem source"
2621
task update: %i[install build copy]
2722

2823
desc "Install the JavaScript dependencies"
2924
task :install do
30-
yarn_run_in("react-builds", "install")
25+
PackageJson.read("react-builds").manager.install
3126
end
3227

3328
desc "Build the JS bundles with Webpack"
3429
task :build do
35-
yarn_run_in("react-builds", "build")
30+
PackageJson.read("react-builds").manager.run("build")
3631
end
3732

3833
desc "Copy browser-ready JS files to the gem's asset paths"
@@ -51,12 +46,12 @@ namespace :ujs do
5146

5247
desc "Install the JavaScript dependencies"
5348
task :install do
54-
`yarn install`
49+
PackageJson.read.manager.install
5550
end
5651

5752
desc "Build the JS bundles with Webpack"
5853
task :build do
59-
`yarn build`
54+
PackageJson.read.manager.run("build")
6055
end
6156

6257
desc "Copy browser-ready JS files to the gem's asset paths"
@@ -87,7 +82,7 @@ task default: :test
8782

8883
task :test_setup do
8984
Dir.chdir("./test/dummy") do
90-
`yarn install`
85+
PackageJson.read.manager.install
9186
end
9287
end
9388

gemfiles/base.gemfile.lock

+2
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ GEM
174174
notiffany (0.1.3)
175175
nenv (~> 0.1)
176176
shellany (~> 0.0)
177+
package_json (0.1.0)
177178
pry (0.14.2)
178179
coderay (~> 1.1)
179180
method_source (~> 1.0)
@@ -258,6 +259,7 @@ DEPENDENCIES
258259
guard-minitest
259260
jbuilder
260261
listen (~> 3.0.0)
262+
package_json
261263
pry-byebug
262264
rails (~> 7.0.x)
263265
react-rails!

gemfiles/shakapacker.gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
source "http://rubygems.org"
44

55
gem "rails", "~> 7.0.x"
6-
gem "shakapacker", "7.0.2"
6+
gem "shakapacker", "7.2.0"
77

88
gemspec path: "../"

gemfiles/shakapacker.gemfile.lock

+6-3
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ GEM
176176
notiffany (0.1.3)
177177
nenv (~> 0.1)
178178
shellany (~> 0.0)
179+
package_json (0.1.0)
179180
pry (0.14.2)
180181
coderay (~> 1.1)
181182
method_source (~> 1.0)
@@ -185,7 +186,7 @@ GEM
185186
public_suffix (5.0.1)
186187
racc (1.6.2)
187188
rack (2.2.7)
188-
rack-proxy (0.7.6)
189+
rack-proxy (0.7.7)
189190
rack
190191
rack-test (2.1.0)
191192
rack (>= 1.3)
@@ -227,8 +228,9 @@ GEM
227228
rubyzip (>= 1.2.2, < 3.0)
228229
websocket (~> 1.0)
229230
semantic_range (3.0.0)
230-
shakapacker (7.0.2)
231+
shakapacker (7.2.0)
231232
activesupport (>= 5.2)
233+
package_json
232234
rack-proxy (>= 0.6.1)
233235
railties (>= 5.2)
234236
semantic_range (>= 2.3.0)
@@ -268,11 +270,12 @@ DEPENDENCIES
268270
guard-minitest
269271
jbuilder
270272
listen (~> 3.0.0)
273+
package_json
271274
pry-byebug
272275
rails (~> 7.0.x)
273276
react-rails!
274277
selenium-webdriver
275-
shakapacker (= 7.0.2)
278+
shakapacker (= 7.2.0)
276279
test-unit (~> 2.5)
277280

278281
BUNDLED WITH

gemfiles/sprockets_3.gemfile.lock

+2
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ GEM
178178
notiffany (0.1.3)
179179
nenv (~> 0.1)
180180
shellany (~> 0.0)
181+
package_json (0.1.0)
181182
pry (0.12.2)
182183
coderay (~> 1.1.0)
183184
method_source (~> 0.9.0)
@@ -270,6 +271,7 @@ DEPENDENCIES
270271
jbuilder
271272
listen (~> 3.0.0)
272273
mini_racer
274+
package_json
273275
pry-byebug
274276
rails (~> 7.0.x)
275277
react-rails!

gemfiles/sprockets_4.gemfile.lock

+2
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ GEM
178178
notiffany (0.1.3)
179179
nenv (~> 0.1)
180180
shellany (~> 0.0)
181+
package_json (0.1.0)
181182
pry (0.12.2)
182183
coderay (~> 1.1.0)
183184
method_source (~> 0.9.0)
@@ -270,6 +271,7 @@ DEPENDENCIES
270271
jbuilder
271272
listen (~> 3.0.0)
272273
mini_racer
274+
package_json
273275
pry-byebug
274276
rails (~> 7.0.x)
275277
react-rails!

lib/generators/react/install_generator.rb

+12-1
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,19 @@ def setup_react_sprockets
103103
ReactRailsUJS.useContext(componentRequireContext);
104104
JS
105105

106+
def require_package_json_gem
107+
require "bundler/inline"
108+
109+
gemfile(true) { gem "package_json" }
110+
111+
puts "using package_json v#{PackageJson::VERSION}"
112+
end
113+
106114
def setup_react_shakapacker
107-
`yarn add react_ujs`
115+
require_package_json_gem
116+
117+
PackageJson.read.manager.add(["react_ujs"])
118+
108119
if manifest.exist?
109120
append_file(manifest, SHAKAPACKER_SETUP_UJS)
110121
else

react-rails.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
2828
s.add_development_dependency 'selenium-webdriver'
2929
s.add_development_dependency 'test-unit', '~> 2.5'
3030
s.add_development_dependency 'pry-byebug'
31+
s.add_development_dependency 'package_json'
3132

3233

3334
s.add_dependency 'connection_pool'
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
# creates a set of fake JavaScript package managers in a temporary bin
5+
# directory for GitHub Actions, _excluding_ the one passed in as an
6+
# argument in order to assert that only that package manager is used
7+
8+
require "tmpdir"
9+
10+
# setup the bin directory we want to use
11+
bin_dir = Dir.mktmpdir("react-rails-")
12+
13+
if ENV["GITHUB_ACTIONS"]
14+
puts "adding #{bin_dir} to GITHUB_PATH..."
15+
File.write(ENV.fetch("GITHUB_PATH"), "#{bin_dir}\n", mode: "a+")
16+
end
17+
18+
managers = %w[npm yarn pnpm bun]
19+
manager_in_use = ARGV[0]
20+
21+
Dir.chdir(bin_dir) do
22+
managers.each do |manager|
23+
next if manager == manager_in_use
24+
25+
puts "creating #{bin_dir}/#{manager}..."
26+
File.write(
27+
manager,
28+
<<~CONTENTS
29+
#!/usr/bin/env node
30+
31+
throw new Error("(#{manager}) this is not the package manager you're looking...");
32+
CONTENTS
33+
)
34+
File.chmod(0o755, manager)
35+
end
36+
end

0 commit comments

Comments
 (0)