Skip to content

Commit a126be8

Browse files
committed
feat: use package_json for interacting with package.json
1 parent 7f124c7 commit a126be8

File tree

5 files changed

+40
-18
lines changed

5 files changed

+40
-18
lines changed

.github/workflows/ruby.yml

Lines changed: 2 additions & 0 deletions
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:

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33
source "http://rubygems.org"
44

55
gemspec
6+
7+
gem "package_json", github: "G-Rath/package_json"
8+
69
# This is an optional dev-dependency, required whenever sprockets is required

Gemfile.lock

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
GIT
2+
remote: https://github.com/G-Rath/package_json.git
3+
revision: d48f2c0db14bb1305f249fffb52fd2e0065678ee
4+
specs:
5+
package_json (0.1.0)
6+
17
PATH
28
remote: .
39
specs:
@@ -102,12 +108,8 @@ GEM
102108
matrix (0.4.2)
103109
method_source (1.0.0)
104110
mini_mime (1.0.2)
105-
mini_portile2 (2.8.4)
106111
minitest (5.17.0)
107112
nenv (0.3.0)
108-
nokogiri (1.14.3)
109-
mini_portile2 (~> 2.8.0)
110-
racc (~> 1.4)
111113
nokogiri (1.14.3-x86_64-darwin)
112114
racc (~> 1.4)
113115
nokogiri (1.14.3-x86_64-linux)
@@ -166,7 +168,6 @@ GEM
166168
zeitwerk (2.6.6)
167169

168170
PLATFORMS
169-
ruby
170171
x86_64-darwin-20
171172
x86_64-linux
172173

@@ -182,6 +183,7 @@ DEPENDENCIES
182183
guard-minitest
183184
jbuilder
184185
listen (~> 3.0.0)
186+
package_json!
185187
pry-byebug
186188
react-rails!
187189
selenium-webdriver

Rakefile

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,22 @@ def copy_react_asset(webpack_file, destination_file)
1414
FileUtils.cp(full_webpack_path, full_destination_path)
1515
end
1616

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-
2417
namespace :react do
2518
desc "Run the JS build process to put files in the gem source"
2619
task update: %i[install build copy]
2720

2821
desc "Install the JavaScript dependencies"
2922
task :install do
30-
yarn_run_in("react-builds", "install")
23+
require "package_json"
24+
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+
require "package_json"
31+
32+
PackageJson.read("react-builds").manager.run("build")
3633
end
3734

3835
desc "Copy browser-ready JS files to the gem's asset paths"
@@ -51,12 +48,16 @@ namespace :ujs do
5148

5249
desc "Install the JavaScript dependencies"
5350
task :install do
54-
`yarn install`
51+
require "package_json"
52+
53+
PackageJson.read.manager.install
5554
end
5655

5756
desc "Build the JS bundles with Webpack"
5857
task :build do
59-
`yarn build`
58+
require "package_json"
59+
60+
PackageJson.read.manager.run("build")
6061
end
6162

6263
desc "Copy browser-ready JS files to the gem's asset paths"
@@ -87,7 +88,9 @@ task default: :test
8788

8889
task :test_setup do
8990
Dir.chdir("./test/dummy") do
90-
`yarn install`
91+
require "package_json"
92+
93+
PackageJson.read.manager.install
9194
end
9295
end
9396

lib/generators/react/install_generator.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,20 @@ def setup_react_sprockets
103103
ReactRailsUJS.useContext(componentRequireContext);
104104
JS
105105

106+
# TODO: replace with standard "require" call once gem is published
107+
def require_package_json_gem
108+
require "bundler/inline"
109+
110+
gemfile { gem "package_json", github: "G-Rath/package_json" }
111+
112+
puts "using package_json v#{PackageJson::VERSION}"
113+
end
114+
106115
def setup_react_shakapacker
107-
`yarn add react_ujs`
116+
require_package_json_gem
117+
118+
PackageJson.read.manager.add(["react_ujs"])
119+
108120
if manifest.exist?
109121
append_file(manifest, SHAKAPACKER_SETUP_UJS)
110122
else

0 commit comments

Comments
 (0)